Using Is

The command Is lists files and directories. We doubt that many people know all of the options to the Is command, and we shall certainly not list them all here. If you're curious, read the man page (man Is) or, better, the info pages (info coreutils Is). But there are a few important things to note about the behavior of Is. If you don't use the -a or -A option, you will not see the hidden files (that is, those with names starting with a dot). It can be easy to forget about this possibility — for example, consider the following:

[email protected]:~> rmdir directory/

rmdir: 'directory/': Directory not empty [email protected] [email protected] [email protected] [email protected]

Here we tried to remove the directory that appeared to be empty because the ls command on its own produced no output. But ls -a shows that there is a hidden file in it, which is why the rmdir command failed (rmdir removes only empty directories).

As mentioned in Chapter 2, symbolic links are objects that point to other files and directories. In a standard directory listing, symbolic links show up as regular files, but ls provides the -l option to provide more information about files and directories as well as to help you to identify any symbolic links in the directory. For example, create a symbolic link in this directory and then look at it with the standard ls command:

[email protected] [email protected] [email protected]

-/directory> ln -s ../otherdir/bfile alink -/directory> touch afile -/directory> ls afile alink

Using the ls - l command clearly shows that this file is a symbolic link:

[email protected]:~/directory> ls -l total 0

-rw-r--r-- 1 user users 0 2007-06-20 10:10 afile lrwxrwxrwx 1 user users 26 2007-06-20 10:32 alink -> ../otherdir/bfile

Unless you are looking at a directory with a very large number of files in it, ls -la is quite a good way to use the ls command. If there is anything unexpected about the sizes or ownerships of the files, you will be able to notice it, and dot files will be displayed.

The -t option to ls can be very useful. Suppose, for example, you have recently done something that has caused a file to be created in a large directory, but you have forgotten or don't know the name of the file. The ls -lat command lists the files in order of modification date and time, so the new or newly modified files will be at the top of the listing.

If you want to get a full recursive listing of all files beneath a particular directory, the command Is -laR is what you want. This can be particularly useful if you are doing some detective work to find out, for example, what is being changed when you make some change to a system using a graphical or other configuration tool, but you don't know which file or files are being changed. If you use Is -laR before and after making the change, writing the output to a file, you can then compare the two files and work out what has happened.

(Some change to the system.)

[email protected]: / # ls -laR>/tmp/listing2 [email protected]: / # diff /tmp/listingl /tmp/listing2

Here we have made some change to the system at the step marked "(Some change to system),'' before and after which we created separate files containing a listing of the files existing at that time. Using diff, you can see what has changed.

Occasionally, it can be useful to know that ls -i shows you the inodes to which the files are attached in the underlying filesystem. In particular, this can help you to understand hard links:

[email protected] [email protected] [email protected] [email protected] total 8 drwxr-xr-x drwxr-xr-x -rw-i—i— -rw-i—i— -rw-i—i—

-/directory> touch afile -/directory> touch bfile -/directory> ln afile cfile -/directory> ls -la

2 user users 4096 2004-06-20 10:44 .

32 user users 4096 2004-06-20 10:31 ..

2 user users 0 2004-06-20 10:44 afile

1 user users 0 2004-06-20 10:44 bfile

2 user users 0 2004-06-20 10:44 cfile

An inode is the low-level data structure in the filesystem that contains information about the files on the filesystem.

The total 8 message is initially somewhat confusing. The number that follows the total is the total number of files and links to files in the specified directory, not just the total number of files. If you add the values in the second column, counting ''..'' (which is a link to the parent directory of the current directory) as 1, you'll get the number 8.

[email protected]:~/directory> ls -il|sort -n total 0

1259203 -rw-r--r-- 2 user users 0 2004-06-20 10:44 afile

1259203 -rw-r--r-- 2 user users 0 2004-06-20 10:44 cfile

1259206 -rw-r--r-- 1 user users 0 2004-06-20 10:44 bfile

The inode numbers confirm that afile and cfile refer to the same file (which is expected because cfile is a hard link to afile).

rm cfile leaves the file untouched: the rm command really removes links to files rather than the files themselves — when there are no links left, you can't access the file. Here, after removing cfile, there will still be one link left, so the file is still there.

The way in which the Is command displays its output depends on a set of default options, which are stored in the $LS_OPTIONS environment variable. This setting is, in turn, set up in the file /etc/bash.bashrc. If you examine the contents of this variable, you will find something like the following:

[email protected]:~ > echo $LS_OPTIONS -N --color=tty -T 0

This means that these options are passed to Is whenever it is run. You can override these options by setting and exporting a different LS_OPTIONS variable:

[email protected]:~ > export LS_OPTIONS='-color=never -T 0'

Then you will see the same layout, but without any colorized entries. Any options that can be passed to ls can be included in this variable.

If you want to set a permanently different option for yourself, you can set and export the LS_OPTIONS variable by adding a line similar to that just given to your .bashrc file.

i- ■ iThe color scheme that ls uses to colorize its output is determined by the ■MM&Um^K LS_COLORS variable. By default this is taken from the file /etc/DIR_COLORS, but you can override the defaults by copying that file to ~ /.dir_colors and editing it as required.

Was this article helpful?

0 0

Post a comment