As you begin working with larger numbers of applications and files on a regular basis, you build a library of data, reports, and files-in-progress. Commands you've already learned such as mv, cp, rm, and ls are certainly helpful for organizing files. As your workload in Linux increases, however, you will need the additional tools provided by the shell and a few more powerful commands to help you navigate your growing file collection. You can group files to make finding and accessing them faster and more efficient. The following sections explain how to group files in Linux for more efficient file management. Grouping Files on the Command LineIn Chapter 6, "Working with Files in the Shell," you learned to perform a number of common file management tasks by entering commands and filenames, one or two at a time, at the shell prompt. Sometimes it is helpful to be able to refer to many files at once at the shell prompt, without having to type all their names. The shell provides a tool for grouping similar filenames to save you the trouble of having to type them one by one. To illustrate, consider the following problem. Suppose you want to create three new empty files called myfirstfile.txt, mysecondfile.txt, and mythirdfile.txt and then move them into a new directory called firstfiles. Using the skills you learned in Chapter 6, you first create the files with touch, the directory firstfiles with mkdir, and then you use mv to move the files by name: [you@workstation20 ~]$ touch myfirstfile.txt mysecondfile.txt mythirdfile.txt [you@workstation20 ~]$ mkdir firstfiles [you@workstation20 ~]$ mv myfirstfile.txt mysecondfile.txt mythirdfile.txt Âfirstfiles [you@workstation20 ~]$ But that's a lot of typing just to move three text files to a more convenient location. It seems as though there should be an easier way, a way to simply tell the shell to move all the text files that begin with my and end with .txt into the new directoryand there is. When used at the shell prompt, the asterisk (*) is one of a special group of characters that you can use for filename expansiona way of grouping files logically so that you don't have to type all their names to manipulate all of them at once. Filename expansion is as much an art as a science; to use it, you employ specific pattern-matching tools to try to collect files into a group and then pass that group on to the shell. This procedure sounds complicated, but it's actually simple and powerful. Here's an example of using filename expansion to perform the task you just saw demonstrated. Try using the following mv command instead of the longer mv command used previously: [you@workstation20 ~]$ mv my*.txt firstfiles Let's make sure that the command had the desired effect: [you@workstation20 ~]$ ls firstfiles myfirstfile.txt mysecondfile.txt mythirdfile.txt [you@workstation20 ~]$ Obviously, the new, simplified command produced the desired result. The phrase my*.txt has grouped together all files that begin with the two letters my and end with the four characters .txt. We'll go over other examples, but first, take a look at Table 19.1, which lists the patterns commonly used for filename expansion.
Working through a few examples can help you better understand what these patterns mean and how they can help you work with groups of files in Linux. Let's create a table of examples. Suppose you were going to use rm on a group of files in your home directory. Table 19.2 shows some sample rm arguments and files that each would (or wouldn't) match.
You can use filename expansion in most circumstances to make your life at the console easier by reducing the amount of reading and typing you have to do when managing large numbers of files. Preventing Filename ExpansionSometimes you don't want the shell to perform filename expansionfor example, when you want to use special pattern-matching characters such as the asterisk or question mark in a file's name or as an argument to a command. Suppose, for example, that you want to create an additional directory called *txt* in your new firstfiles directory, to hold some interesting text files you've been working on. There is nothing illegal in Linux about having the asterisk in a file or directory name. However, if you try to create such a directory while files that end in .txt are present, the operation doesn't work: [you@workstation20 ~]$ cd firstfiles [you@workstation20 firstfiles]$ mkdir *txt* mkdir: 'myfirstfile.txt' exists but is not a directory mkdir: 'mysecondfile.txt' exists but is not a directory mkdir: 'mythirdfile.txt' exists but is not a directory [you@workstation20 firstfiles]$ Here, the shell has interpreted *txt* as a pattern and tried to match it against the files in your current working directory. As luck would have it, there is indeed a matchthe pattern *txt* matches the filenames myfirstfile.txt, mysecondfile.txt, and mythirdfile.txt, so the shell behaved as though you had entered this: mkdir myfirstfile.txt mysecondfile.txt mythirdfile.txt At such times, you want the shell to treat the phrase you've entered as normal text, rather than as a pattern. To do this, you need to quote the text. You can do so with either single or double quotation marks. (There is a difference, which we discuss in later chapters.) For now, use single quotation marks to create the directory *txt* in your current working directory: [you@workstation20 firstfiles]$ mkdir '*txt*' [you@workstation20 firstfiles]$ ls myfirstfile.txt mysecondfile.txt mythirdfile.txt *txt* [you@workstation20 firstfiles]$ You have now successfully created a directory called *txt* in your current working directory by quoting what could otherwise have been a pattern for filename expansion. |