same contents and attributes. So, if you edit one, the other changes because they are both the same file.
On the other hand, a
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.
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] <
, 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 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
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
> -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