Using Wildcards in Linux Filenames
What is a Wildcard?
A wildcard is a symbol used in file management commands to represent one or more characters. They allow you to perform operations on multiple files that match a certain pattern without specifying each file individually. The most common wildcards are:- * (asterisk): Represents zero or more characters.
- ? (question mark): Represents exactly one character.
- [] (square brackets): Represents any one of the enclosed characters.
Example Commands
- Copy (cp)
- Copy all .txt files:
Code:
cp *.txt /destination/directory/
- Copy files starting with Jan:
Code:
cp Jan* /destination/directory/
- Copy all .txt files:
- Move (mv)
- Move all .jpg files:
Code:
mv *.jpg /destination/directory/
- Move files ending with 01.mp3:
Code:
mv *01.mp3 /destination/directory/
- Move all .jpg files:
- Remove (rm)
- Remove all .log files:
Code:
rm *.log
- Remove files starting with Feb:
Code:
rm Feb*
- Remove all .log files:
- List (ls)
- List all .png files:
Code:
ls *.png
- List files ending with 02.txt:
Code:
ls *02.txt
- List all .png files:
Wildcard Examples
- Wildcard at the Beginning
- Copy files ending with .doc:
Code:
cp *.doc /destination/directory/
- Copy files ending with .doc:
- Wildcard at the End
- Move files starting with Report:
Code:
mv Report* /destination/directory/
- Move files starting with Report:
- Double Wildcards
- Remove files containing data in the middle:
Code:
rm *data*
- Remove files containing data in the middle:
Specific Example: Copying Files with Specific Patterns
Imagine you have a directory with hundreds of files, most of which begin with a three-letter month abbreviation (e.g., Jan, Feb, Mar) and have various suffixes like .jpg, .mp3, .txt, etc. For example:- JanChicago01.mp3
- SepAtlanta06.doc
- FebAtlanta02.txt
- MayBoston05.jpg
- OctBoston07.png
Code:
cp *Boston* /destination/directory/