< Previous | Contents | Next >
3.3. Manipulating files
3.3.1. Viewing file properties
3.3.1.1. More about ls
Besides the name of the file, ls can give a lot of other information, such as the file type, as we already discussed. It can also show permissions on a file, file size, inode number, creation date and time, owners and amount of links to the file. With the -a option to ls, files that are normally hidden from view can be displayed as well. These are files that have a name starting with a dot. A couple of typical examples include the configuration files in your home directory. When you've worked with a certain system for a while, you will notice that tens of files and directories have been created that are not automatically listed in a directory index. Next to that, every directory contains a file named just dot (.) and one with two dots (..), which are used in combination with their inode number to determine the directory's position in the file system's tree structure.
You should really read the Info pages about ls, since it is a very common command with a lot of useful options. Options can be combined, as is the case with most UNIX commands and their options. A common combination is ls -al; it shows a long list of files and their properties as well as the destinations that any symbolic links point to. ls -latr displays the same files, only now in reversed order of the last change, so that the file changed most recently occurs at the bottom of the list. Here are a couple of examples:
krissie:~/mp3> ls
Albums/ Radio/ Singles/ gene/ index.html
krissie:~/mp3> ls -a
./ .thumbs Radio gene/
../ Albums/ Singles/ index.html
krissie:~/mp3> ls -l Radio/
total 8
drwxr-xr-x | 2 krissie krissie | 4096 | Oct | 30 | 1999 | Carolina/ |
drwxr-xr-x | 2 krissie krissie | 4096 | Sep | 24 | 1999 | Slashdot/ |
krissie:~/mp3> ls -ld Radio/
drwxr-xr-x 4 krissie krissie 4096 Oct 30 1999 Radio/
krissie:~/mp3> ls -ltr
total 20
drwxr-xr-x 4 krissie krissie 4096 Oct 30 1999 Radio/
-rw-r--r-- 1 krissie krissie 453 Jan 7 2001 index.html
drwxrwxr-x 30 krissie krissie 4096 Oct 20 17:32 Singles/
drwxr-xr-x 2 krissie krissie 4096 Dec 4 23:22 gene/
drwxrwxr-x 13 krissie krissie 4096 Dec 21 11:40 Albums/
On most Linux versions ls is aliased to color-ls by default. This feature allows to see the file type without using any options to ls. To achieve this, every file type has its own color. The standard scheme is in
/etc/DIR_COLORS:
Table 3-5. Color-ls default color scheme
Color | File type |
blue | directories |
red | compressed archives |
white | text files |
pink | images |
cyan | links |
yellow | devices |
green | executables |
flashing red | broken links |
More information is in the man page. The same information was in earlier days displayed using suffixes to every non-standard file name. For mono-color use (like printing a directory listing) and for general readability, this scheme is still in use:
Table 3-6. Default suffix scheme for ls
Character | File type |
nothing | regular file |
/ | directory |
* | executable file |
@ | link |
= | socket |
| | named pipe |
A description of the full functionality and features of the ls command can be read with info coreutils ls.
3.3.1.2. More tools
To find out more about the kind of data we are dealing with, we use the file command. By applying certain tests that check properties of a file in the file system, magic numbers and language tests, file tries to make an educated guess about the format of a file. Some examples:
mike:~> file Documents/
Documents/: directory
mike:~> file high-tech-stats.pdf
high-tech-stats.pdf: PDF document, version 1.2
mike:~> file Nari-288.rm
Nari-288.rm: RealMedia file
mike:~> file bijlage10.sdw
bijlage10.sdw: Microsoft Office Document
mike:~> file logo.xcf
logo.xcf: GIMP XCF image data, version 0, 150 x 38, RGB Color
mike:~> file cv.txt
cv.txt: ISO-8859 text
mike:~> file image.png
image.png: PNG image data, 616 x 862, 8-bit grayscale, non-interlaced
mike:~> file figure
figure: ASCII text
mike:~> file Documents/
Documents/: directory
mike:~> file high-tech-stats.pdf
high-tech-stats.pdf: PDF document, version 1.2
mike:~> file Nari-288.rm
Nari-288.rm: RealMedia file
mike:~> file bijlage10.sdw
bijlage10.sdw: Microsoft Office Document
mike:~> file logo.xcf
logo.xcf: GIMP XCF image data, version 0, 150 x 38, RGB Color
mike:~> file cv.txt
cv.txt: ISO-8859 text
mike:~> file image.png
image.png: PNG image data, 616 x 862, 8-bit grayscale, non-interlaced
mike:~> file figure
figure: ASCII text
mike:~> file me+tux.jpg
me+tux.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), "28 Jun 1999", 144 x 144
mike:~> file 42.zip.gz
42.zip.gz: gzip compressed data, deflated, original filename,
`42.zip', last modified: Thu Nov 1 23:45:39 2001, os: Unix
mike:~> file vi.gif
vi.gif: GIF image data, version 89a, 88 x 31
mike:~> file slide1
slide1: HTML document text
mike:~> file template.xls
template.xls: Microsoft Office Document
mike:~> file abook.ps
abook.ps: PostScript document text conforming at level 2.0
mike:~> file /dev/log
/dev/log: socket
mike:~> file /dev/hda
/dev/hda: block special (3/0)
mike:~> file me+tux.jpg
me+tux.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), "28 Jun 1999", 144 x 144
mike:~> file 42.zip.gz
42.zip.gz: gzip compressed data, deflated, original filename,
`42.zip', last modified: Thu Nov 1 23:45:39 2001, os: Unix
mike:~> file vi.gif
vi.gif: GIF image data, version 89a, 88 x 31
mike:~> file slide1
slide1: HTML document text
mike:~> file template.xls
template.xls: Microsoft Office Document
mike:~> file abook.ps
abook.ps: PostScript document text conforming at level 2.0
mike:~> file /dev/log
/dev/log: socket
mike:~> file /dev/hda
/dev/hda: block special (3/0)
The file command has a series of options, among others the -z option to look into compressed files. See info file for a detailed description. Keep in mind that the results of file are not absolute, it is only a guess. In other words, file can be tricked.