Grouping Files for Efficient File Management


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 Line

In 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.

Table 19.1. Common Pattern-Matching Characters and Their Effects

Pattern

Matching Effects

*

Matches all characters in any quantity, including no characters at all

?

Matches any single character

[a-b] (range)

Matches a single character in the range of characters between a and b; for example, [A-Z] would match the letter X or the letter P, but not the number 9 or the letter a

[AaBbCc] (list)

Matches a single character from the list of characters provided


Ranges Don't Always Work as Expected

Because letters in a computer are represented internally using a special code called ASCII that doesn't function as humans do, some range patterns might not have the effect you expected.

For example, [a-Z] will match nothing, whereas [A-z] will match a number of characters that are not uppercase or lowercase letters. For this reason, in the interest of clarity, you should use ranges for which both characters are uppercase, both characters are lowercase, or both characters are numbers.


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.

Table 19.2. Sample Commands Using Expansion and Their Effects

Command

Effects

rm *.txt

Would remove any file with a .txt extensionfor example, a.txt, b.txt, hello.txt, or everybody_is_cool.txt but not bicycle.gif, car_bills.xls, or myoldtxt.

rm a*jpg

Would remove any file beginning with the letter a and ending with the three letters jpgfor example, a.jpg, apple.jpg, answermachine.jpg, and anastasiajpg but not boat.jpg, file.txt, monkeys.gif, or macaroni_list.

rm k*n.?if

Would remove kitchen.gif, kluckchicken.tif, kn.zif, and korn.weekday..if but not korean.if, kasino.gif, or fountain.tif.

rm l[eou]g.*

Would remove leg.gif, log.txt, lug.jpg, log.my.hours.please, leg.gomyeg.go.txt, and lug.this but not lag.gif, leglover.txt, or log.

rm [a-f]*

Would remove apple.txt, bacon.jpg, cradle.song, dog.walking.schedule, everybody.mp3, and fanatics_favorites but not goose.txt, xylophone.jpg, or Barbie.gif.

rm *

Would remove every file in the present working directory. Adding the -rf options would remove every file and every directory in the present working directory. (Remember the -r and -f options from Chapter 6?)


Be Careful When Using Patterns

Be careful when grouping files using filename expansion; constructing your patterns carelessly can have unintended consequences. For example, when you remove files using the methods shown in Table 19.2, Linux does not caution you or ask whether you are sure before removing all the files that match the pattern you supplied.


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 Expansion

Sometimes 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.



    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net