Now that we understand different types of wildcards, let's use them to perform common file operations such as copying, moving, and removing files.
-
First, make sure you're in the wildcard_practice
directory:
cd ~/project/wildcard_practice
-
Let's create a backup directory to store copies of our files:
mkdir backup
-
Copy all text files to the backup directory using the asterisk wildcard:
cp *.txt backup/
-
Let's verify that the files were copied correctly:
ls backup/
You should see all the text files in the backup directory:
file-a.txt file-b.txt file-c.txt file1.txt file2.txt file3.txt
-
Now, let's create another directory for organizing different file types:
mkdir csv_files pdf_files log_files
-
Move all CSV files to the csv_files directory:
mv *.csv csv_files/
-
Move all PDF files to the pdf_files directory:
mv *.pdf pdf_files/
-
Move all log files to the log_files directory:
mv *.log log_files/
-
Let's check that each directory contains the correct files:
echo "CSV Files:"
ls csv_files/
echo "PDF Files:"
ls pdf_files/
echo "Log Files:"
ls log_files/
The output should show the appropriate files in each directory.
-
Finally, let's clean up by removing all backup text files that have a number in their name:
rm backup/file[0-9].txt
-
Verify that only the letter-named text files remain in the backup directory:
ls backup/
The output should show:
file-a.txt file-b.txt file-c.txt
These examples demonstrate how wildcards can greatly simplify file management tasks that would otherwise require multiple commands or manual handling of individual files.