Выбрать главу

To look for a file in your index, use the command locate followed by the names of the files you want to find, like this:

$ locate myfile.txt

On a relatively modern computer (1.5GHz or higher), locate should be able to return all the matching files in less than a second. The trade-off for this speed is lack of flexibility. You can search for matching filenames, but, unlike with find, you cannot search for sizes, owners, access permissions, or other attributes. The one thing you can change is case sensitivity; use the -i parameter to do a search that is not case sensitive.

Although Fedora rebuilds the filename index nightly, you can force a rebuild whenever you want by running the command updatedb as root. This usually takes a few minutes, but when it's done the new database is immediately available.

Listing Files in the Current Directory with ls

The ls command, like ln, is one of those you expect to be very straightforward. It lists files, but how many options can it possibly have? In true Linux style, the answer is many, although again you need know only a few to wield great power!

The basic use is simply ls, which outputs the files and directories in the current location. You can filter that with normal wildcards, so all these are valid:

$ ls *

$ ls *.txt

$ ls my*ls *.txt *.xml

Any directories that match these filters are recursed into one level. That is, if you run ls my* and you have the files myfile1.txt and myfile2.txt and a directory mystuff, the matching files are printed first. Then ls prints the contents of the mystuff directory.

The most popular parameters for customizing the output of ls are the following:

► -a — Includes hidden files

► -h — Uses human-readable sizes

► -l — A lowercase L, it enables long listing

► -r — Reverses the order of results

► -R — Recursively lists directories

► -s — Shows sizes

► --sort — Sorts the listing

All files that start with a period are hidden in Linux, so that includes the .gnome directory in your home directory, as well as .bash_history and the . and .. implicit directories that signify the current directory and the parent. By default, ls does not show these files, but if you run ls -a, they are shown. You can also use ls -A to show all the hidden files except . and ...

The -h parameter has to be combined with the -s parameter, like this:

$ ls -sh *.txt

That query outputs the size of each matching file in a human-readable format, such as 108KB or 4.5MB.

Using the -l parameter shows much more information about your files. Instead of just providing the names of the files, you get output like this:

drwxrwxr-x 24 paul paul  4096 2007-11-12 21:33 arch

-rw-r--r--  1 paul paul 18691 2007-11-12 21:34 COPYING

-rw-r--r--  1 paul paul 88167 2007-11-12 21:35 CREDITS

drwxrwxr-x  2 paul paul  4096 2007-11-12 21:35 crypto

That output shows four matches and prints a lot of information about each of them. The first row shows the arch directory; you can tell it is a directory because its file attributes start with a d. The rwxrwxr-x following that shows the access permissions, and this has special meaning because it is a directory. Read access for a directory allows users to see the directory contents, write access allows you to create files and subdirectories, and execute access allows you to cd into the directory. If a user has execute access but not read access, he can cd into the directory but not list files.

Moving on, the next number on the line is 24, which also has a special meaning for directories: It is the number of subdirectories, including . and ... After that is paul paul, which is the name of the user owner and the group owner for the directory. Next are the size and modification time, and finally the directory name itself.

The next line shows the file COPYING, and most of the numbers have the same meaning, with the exception of the 1 immediately after the access permissions. For directories, this is the number of subdirectories, but for files, this is the number of hard links to this file. A 1 in this column means this is the only filename pointing to this inode, so if you delete it, it is gone.

Fedora comes configured with a shortcut command for ls -l: ll.

The --sort parameter enables you to reorder the output from the default alphabetic sorting. You can sort by various things, although the most popular are extension (alphabetically), size (largest first), and time (newest first). To flip the sorting (making size sort by smallest first), use the -r parameter also. So, this command lists all .ogg files, sorted smallest to largest:

$ ls --sort size -r *.ogg

Finally, the -R parameter recurses through subdirectories. For example, ls /etc lists all the files and subdirectories in /etc, but ls -R /etc lists all the files and subdirectories in /etc, all the files and subdirectories in /etc/acpi, all the files and subdirectories in /etc/acpi/actions, and so on until every subdirectory has been listed.

Reading Manual Pages with man

Time for a much-needed mental break: The man command is easy to use. Most people use only the topic argument, like this: man gcc. However, two other commands that are very useful work closely with manwhatis and apropos.

The whatis command returns a one-line description of another command, which is the same text you get at the top of that command's man page. For example:

[paul@caitlin ~]$ whatis ls

ls (1) - list directory contents

The output there explains what ls does but also provides the man page section number for the command so that you can query it further.

On the other hand, the apropos command takes a search string as its parameter and returns all man pages that match the search. For example, apropos mixer gives this list:

alsamixer (1) - soundcard mixer for ALSA soundcard driver

mixer (1) - command-line mixer for ALSA soundcard driver

aumix (1) - adjust audio mixer

So, use apropos to help you find commands and use whatis to tell you what a command does.

Making Directories with mkdir

Making directories is as easy as it sounds, although there is one parameter you should be aware of: -p. If you are in /home/paul and you want to create the directory /home/paul/audio/sound, you get an error like this: