same contents and attributes. So, if you edit one, the other changes because they are both the same file.

On the other hand, a symlink — sometimes called a soft link — is a redirect to the real file. When a program tries to read from a symlink, it automatically is redirected to what the symlink is pointing at. The fact that symlinks are really just dumb pointers has two advantages: You can link to something that does not exist (and create it later if you want), and you can link to directories.

Both types of links have their uses. Creating a hard link is a great way to back up a file on the same disk. For example, if you delete the file in one location, it still exists untouched in the other location. Symlinks are popular because they allow a file to appear to be in a different location; you could store your website in /var/www/live and an under-construction holding page in /var/www/construction. Then you could have Apache point to a symlink /var/www/html that is redirected to either the live or construction directory depending on what you need.

TIP

The shred command overwrites a file's contents with random data, allowing for safe deletion. Because this directly affects a file's contents, rather than just a filename, this means that all filenames hard linked to an inode are affected.

Both types of link are created with the ln command. By default, the ln command creates hard links, but you can create symlinks by passing it the -s parameter. The syntax is ln [-s] <something> <somewhere>, for example:

$ ln -s myfile.txt mylink

That command creates the symlink mylink that points to myfile.txt. Remove the -s to create a hard link. You can verify that your link has been created by running ls -l. Your symlink should look something like this:

lrwxrwxrwx 1 paul paul 5 2007-11-12 12:39 mylink -> myfile.txt

Note how the file properties start with l (lowercase L) for link and how ls -l also prints where the link is going. Symlinks are always very small in size; the previous link is 5 bytes in size. If you created a hard link, it should look like this:

-rw-rw-r 2 paul paul 341 2007-11-12 12:39 mylink

This time the file has normal attributes, but the second number is 2 rather than 1. That number is how many hard links point to this file, which is why it is 2 now. The file size is also the same as that of the previous filename because it is the file, as opposed to just being a pointer.

Symlinks are used extensively in Linux. Programs that have been superseded, such as sh, now point to their replacements (in this case, bash), and library versioning is accomplished through symlinks. For example, applications that link against zlib load /usr/lib/libz.so. Internally, however, that is just a symlink that points to the actual zlib library: /usr/lib/libz.so.1.2.1.2. This enables multiple versions of libraries to be installed without applications needing to worry about the version name.

Finding Files from an Index with locate

When you use the find command, it searches recursively through each directory each time you request a file. This is slow, as you can imagine. Fortunately, Fedora ships with a cron job that creates an index of all the files on your system every night. Searching this index is extremely fast, which means that, if the file you are looking for has been around since the last index, this is the preferable way of searching.

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

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату