1 | 2 | 3 | 4 | 5 | 6 | Current Level: 7
Learn how to copy, move, and list multiple files efficiently using wildcards and filename patterns in the Unix shell.
Use cp
with multiple filenames and a target directory:
mkdir backup
cp creatures/minotaur.dat creatures/unicorn.dat backup/
Result: Both files are copied into the backup/
directory.
You can also list filenames directly:
cd creatures
cp minotaur.dat unicorn.dat basilisk.dat ../backup/
Asterisk *
matches zero or more characters:
ls *.pdb # Matches all files ending in .pdb
ls p*.pdb # Matches files starting with "p" and ending in .pdb
Question mark ?
matches exactly one character:
ls ?ethane.pdb # Matches methane.pdb
ls *ethane.pdb # Matches both methane.pdb and ethane.pdb
Combine wildcards:
ls ???ane.pdb # Matches cubane.pdb, ethane.pdb, octane.pdb
Given a directory of .pdb
files, which of the following commands list ethane.pdb
and methane.pdb
?
ls *t*ane.pdb
ls *t?ne.*
ls *t??ne.pdb
ls ethane.*
Try each one to see which patterns match.
Sam wants to organize a set of dated .txt
files. She uses wildcard patterns to copy selected files into subdirectories:
cp *dataset* backup/datasets
cp *calibration.txt backup/calibration
cp 2015-11-23-* send_to_bob/all_november_files/
cp *10-23-dataset* send_to_bob/all_datasets_created_on_a_23rd/
cp *11-23-dataset* send_to_bob/all_datasets_created_on_a_23rd/
backup/
, send_to_bob/
) exist before running these commands.Jamie has disorganized files:
ls -F
# analyzed/ fructose.dat raw/ sucrose.dat
To tidy up:
mv fructose.dat sucrose.dat analyzed/
Now:
ls -F
# analyzed/ raw/
ls analyzed
# fructose.dat sucrose.dat
Goal: Copy folder structure (not files) from 2016-05-18
to 2016-05-20
.
Best command:
mkdir -p 2016-05-20/data/raw
mkdir -p 2016-05-20/data/processed
Other options (multi-line versions) also work, but are more verbose:
mkdir 2016-05-20
mkdir 2016-05-20/data
mkdir 2016-05-20/data/raw
mkdir 2016-05-20/data/processed
cp
, mv
, and rm
?