Dealing with multiple files
You've now learned to traverse directories and handle individual files. The next step in this tutorial is learning to deal with files in groups. Almost all UNIX commands can handle file lists as opposed to individual files. You can enter file lists by explicitly typing the name of each file you want to use or by using a wildcard to indicate that you want to use all files with a common name trait.
The most common method of dealing with multiple files is to use
*, the wildcard character.
You can select a list of files by using
to represent any character or any number of characters. To create a few
more files for a demonstration, type these commands:
$ cp example.txt example2.txt $ cp example.txt script.sh
Now, type the following:
$ ls *.txt
You should just see the files with the .txt extension. Next, type:
$ ls exa*
Again, you should see the two example files, but you should not see script.sh.
The wildcard character can be used with any command-line application that can deal with multiple files.
Many command-line applications that deal with files have the
-R option. When
-R is used, the application
recursively enters a directory and any subdirectories, and it performs the
desired command on each file. For instance, you can go back in your
home directory and copy the entire TUTORIAL directory:
$ cd ~ $ cp -R TUTORIAL /tmp/. $ ls /tmp/TUTORIAL/
Now, remove that directory to tidy up:
$ rm -R /tmp/TUTORIAL/ $ ls /tmp/
The entire directory is removed, including all the files contained
in it. Be careful: You could easily delete much more data than
you plan to, especially when combining a wildcard with