Copying Files with Patterns
One of the most common use cases for Linux wildcards is copying files that match specific patterns. The cp
command, combined with wildcards, allows you to selectively copy files based on their names.
Copying Files with Wildcards
The general syntax for copying files with wildcards is:
cp source_pattern destination_directory
Here's an example of copying all .jpg
files from the current directory to the ~/images/
directory:
cp *.jpg ~/images/
You can also use more complex patterns to select specific files. For instance, to copy all files with a .txt
extension that start with "report":
cp report*.txt ~/documents/
Copying Files with Bracket Patterns
Wildcards can also be used with bracket patterns to match specific characters within the file names. For example, to copy all files that have a two-digit number in their names:
cp file[0-9][0-9].txt ~/backups/
This will copy all files like file01.txt
, file42.txt
, and file99.txt
to the ~/backups/
directory.
Copying Files Recursively
If you need to copy files with patterns from subdirectories, you can use the -r
(recursive) option with the cp
command:
cp -r *.pdf ~/documents/
This will copy all PDF files from the current directory and its subdirectories to the ~/documents/
directory.
By understanding how to use wildcards with the cp
command, you can streamline your file copying tasks and save time when working with large numbers of files in Linux.