Wildcards

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,527
Reaction score
3,294
Credits
31,550

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​

  1. Copy (cp)
    • Copy all .txt files:
      Code:
      cp *.txt /destination/directory/

    • Copy files starting with Jan:
      Code:
      cp Jan* /destination/directory/
  2. Move (mv)
    • Move all .jpg files:
      Code:
      mv *.jpg /destination/directory/

    • Move files ending with 01.mp3:
      Code:
      mv *01.mp3 /destination/directory/
  3. Remove (rm)
    • Remove all .log files:
      Code:
      rm *.log

    • Remove files starting with Feb:
      Code:
      rm Feb*
  4. List (ls)
    • List all .png files:
      Code:
      ls *.png

    • List files ending with 02.txt:
      Code:
      ls *02.txt

Wildcard Examples​

  1. Wildcard at the Beginning
    • Copy files ending with .doc:
      Code:
      cp *.doc /destination/directory/
  2. Wildcard at the End
    • Move files starting with Report:
      Code:
      mv Report* /destination/directory/
  3. Double Wildcards
    • Remove files containing data in the middle:
      Code:
      rm *data*

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
To copy all files that contain Boston in the middle to another directory, you can use double wildcards:

Code:
cp *Boston* /destination/directory/

Advantages of Command Line Over GUI​

While GUI file managers allow sorting files by name, date, and type, they often lack the flexibility to sort and manage files based on complex patterns involving multiple criteria. Command-line tools, with the help of wildcards, provide powerful and efficient ways to handle such tasks, making them invaluable for managing large sets of files with specific naming conventions.
 


Using ? Wildcard​

The ? wildcard represents exactly one character. Here are some examples:

  1. Copy (cp)
    • Copy files with names like file1.txt, file2.txt, etc.:
      Code:
      cp file?.txt /destination/directory/
  2. Move (mv)
    • Move files with names like a1.jpg, b2.jpg, etc.:
      Code:
      mv ?.jpg /destination/directory/
  3. Remove (rm)
    • Remove files with names like log1, log2, etc.:
      Code:
      rm log?
  4. List (ls)
    • List files with names like data1.csv, data2.csv, etc.:
      Code:
      ls data?.csv

Using [] Wildcard​

The [] wildcard allows you to specify a set of characters. Here are some examples:

  1. Copy (cp)
    • Copy files with names like file1.txt, file2.txt, file3.txt:
      Code:
      cp file[123].txt /destination/directory/
  2. Move (mv)
    • Move files with names like a.jpg, b.jpg, c.jpg:
      Code:
      mv [abc].jpg /destination/directory/
  3. Remove (rm)
    • Remove files with names like log1, log2, log3:
      Code:
      rm log[123]
  4. List (ls)
    • List files with names like dataA.csv, dataB.csv, dataC.csv:
      Code:
      ls data[A-C].csv

Combining Wildcards​

You can also combine these wildcards for more complex patterns:

  1. Copy (cp)
    • Copy files with names like fileA1.txt, fileB2.txt, etc.:
      Code:
      cp file[A-Z]?.txt /destination/directory/
  2. Move (mv)
    • Move files with names like a1.jpg, b2.jpg, c3.jpg:
      Code:
      mv [a-c]?.jpg /destination/directory/
  3. Remove (rm)
    • Remove files with names like logA1, logB2, logC3:
      Code:
      rm log[A-C]?[1-3]
  4. List (ls)
    • List files with names like dataA1.csv, dataB2.csv, dataC3.csv:
      Code:
      ls data[A-C]?[1-3].csv
These examples should help you understand how to use the ? and [] wildcards effectively.
 

Using ? Wildcard​

The ? wildcard represents exactly one character. Here are some examples:

  1. Copy (cp)
    • Copy files with names like file1.txt, file2.txt, etc.:
      Code:
      cp file?.txt /destination/directory/
  2. Move (mv)
    • Move files with names like a1.jpg, b2.jpg, etc.:
      Code:
      mv ?.jpg /destination/directory/
  3. Remove (rm)
    • Remove files with names like log1, log2, etc.:
      Code:
      rm log?
  4. List (ls)
    • List files with names like data1.csv, data2.csv, etc.:
      Code:
      ls data?.csv

Using [] Wildcard​

The [] wildcard allows you to specify a set of characters. Here are some examples:

  1. Copy (cp)
    • Copy files with names like file1.txt, file2.txt, file3.txt:
      Code:
      cp file[123].txt /destination/directory/
  2. Move (mv)
    • Move files with names like a.jpg, b.jpg, c.jpg:
      Code:
      mv [abc].jpg /destination/directory/
  3. Remove (rm)
    • Remove files with names like log1, log2, log3:
      Code:
      rm log[123]
  4. List (ls)
    • List files with names like dataA.csv, dataB.csv, dataC.csv:
      Code:
      ls data[A-C].csv

Combining Wildcards​

You can also combine these wildcards for more complex patterns:

  1. Copy (cp)
    • Copy files with names like fileA1.txt, fileB2.txt, etc.:
      Code:
      cp file[A-Z]?.txt /destination/directory/
  2. Move (mv)
    • Move files with names like a1.jpg, b2.jpg, c3.jpg:
      Code:
      mv [a-c]?.jpg /destination/directory/
  3. Remove (rm)
    • Remove files with names like logA1, logB2, logC3:
      Code:
      rm log[A-C]?[1-3]
  4. List (ls)
    • List files with names like dataA1.csv, dataB2.csv, dataC3.csv:
      Code:
      ls data[A-C]?[1-3].csv
These examples should help you understand how to use the ? and [] wildcards effectively.
Very helpful dos2unix.

I've never used wildcards before. so now I can thanks for the write up with examples.
 



Top