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 number 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 that start with a dot. Typical examples include the configuration files in your home directory which you will begin to notice after you’ve worked with a system for a while. Every directory also contains a file named with a single 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. Every file type has its own color which allows one to see the file type without using any options to ls. 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 (ls -F):
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.
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 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.
Why all the fuss about file types and formats?
… Is not a difficult thing to do. Today almost every system is networked, so naturally files get copied from one machine to another. And especially when working in a graphical environment, creating new files is a piece of cake and is often done without the approval of the user. To illustrate the problem, here’s the full content of a new user’s directory, created on a standard RedHat system:
[newuser@blob user]$ ls -al total 32 drwx------ 3 user user 4096 Jan 16 13:32 . drwxr-xr-x 6 root root 4096 Jan 16 13:32 .. -rw-r--r-- 1 user user 24 Jan 16 13:32 .bash_logout -rw-r--r-- 1 user user 191 Jan 16 13:32 .bash_profile -rw-r--r-- 1 user user 124 Jan 16 13:32 .bashrc drwxr-xr-x 3 user user 4096 Jan 16 13:32 .kde -rw-r--r-- 1 user user 3511 Jan 16 13:32 .screenrc -rw------- 1 user user 61 Jan 16 13:32 .xauthDqztLr
On first sight, the content of a used home directory doesn’t look that bad either:
olduser:~> ls app-defaults/ crossover/ Fvwm@ mp3/ OpenOffice.org638/ articles/ Desktop/ GNUstep/ Nautilus/ staroffice6.0/ bin/ Desktop1/ images/ nqc/ training/ brol/ desktoptest/ Machines@ ns_imap/ webstart/ C/ Documents/ mail/ nsmail/ xml/ closed/ Emacs@ Mail/ office52/ Xrootenv.0
But when all the directories and files starting with a dot are included, there are 185 items in this directory. This is because most applications have their own directories and/or files, containing user-specific settings, in the home directory of that user. Usually these files are created the first time you start an application. In some cases you will be notified when a non-existent directory needs to be created, but most of the time everything is done automatically.
Furthermore, new files are created seemingly continuously because users want to save files, keep different versions of their work, use Internet applications, and download files and attachments to their local machine. It doesn’t stop. It is clear that one definitely needs a scheme to keep an overview on things.
In the next section, we will discuss our means of keeping order. We only discuss text tools available to the shell, since the graphical tools are very intuitive and have the same look and feel as the well known point-and-click MS Windows-style file managers, including graphical help functions and other features you expect from this kind of application. The following list is an overview of the most popular file managers for GNU/Linux. Most file managers can be started from the menu of your desktop manager, or by clicking your home directory icon, or from the command line, issuing these commands:
These applications are certainly worth giving a try and usually impress newcomers to Linux, if only because there is such a wide variety: these are only the most popular tools for managing directories and files, and many other projects are being developed. Now let’s find out about the internals and see how these graphical tools use common UNIX commands.
A way of keeping things in place is to give certain files specific default locations by creating directories and subdirectories (or folders and sub-folders if you wish). This is done with the mkdir command:
richard:~> mkdir archive richard:~> ls -ld archive drwxrwxrwx 2 richard richard 4096 Jan 13 14:09 archive/
Creating directories and subdirectories in one step is done using the -p option:
richard:~> cd archive richard:~/archive> mkdir 1999 2000 2001 richard:~/archive> ls 1999/ 2000/ 2001/ richard:~/archive> mkdir 2001/reports/Restaurants-Michelin/ mkdir: cannot create directory `2001/reports/Restaurants-Michelin/': No such file or directory richard:~/archive> mkdir -p 2001/reports/Restaurants-Michelin/ richard:~/archive> ls 2001/reports/ Restaurants-Michelin/
If the new file needs other permissions than the default file creation permissions, the new access rights can be set in one move, still using the mkdir command, see the Info pages for more. We are going to discuss access modes in the next section on File Security.
The name of a directory has to comply with the same rules as those applied on regular file names. One of the most important restrictions is that you can’t have two files with the same name in one directory (but keep in mind that Linux is, like UNIX, a case sensitive operating system). There are virtually no limits on the length of a file name, but it is usually kept shorter than 80 characters, so it can fit on one line of a terminal. You can use any character you want in a file name, although it is advised to exclude characters that have a special meaning to the shell. When in doubt, check with Appendix C.
Now that we have properly structured our home directory, it is time to clean up unclassified files using the mv command:
richard:~/archive> mv ../report[1-4].doc reports/Restaurants-Michelin/
This command is also applicable when renaming files:
richard:~> ls To_Do -rw-rw-r-- 1 richard richard 2534 Jan 15 12:39 To_Do richard:~> mv To_Do done richard:~> ls -l done -rw-rw-r-- 1 richard richard 2534 Jan 15 12:39 done
It is clear that only the name of the file changes. All other properties remain the same.
Detailed information about the syntax and features of the mv command can be found in the man or Info pages. The use of this documentation should always be your first reflex when confronted with a problem. The answer to your problem is likely to be in the system documentation. Even experienced users read man pages every day, so beginning users should read them all the time. After a while, you will get to know the most common options to the common commands, but you will still need the documentation as a primary source of information. Note that the information contained in the HOWTOs, FAQs, man pages and such is slowly being merged into the Info pages, which are today the most up-to-date source of online (as in readily available on the system) documentation.
Copying files and directories is done with the cp command. A useful option is recursive copy (copy all underlying files and subdirectories), using the -R option to cp. The general syntax is
cp [-R] fromfile tofile
As an example the case of user newguy, who wants the same Gnome desktop settings user oldguy has. One way to solve the problem is to copy the settings of oldguy to the home directory of newguy:
newguy:~> cp -R ../oldguy/.gnome/ .
This gives some errors involving file permissions, but all the errors have to do with private files that newguy doesn’t need anyway. We will discuss in the next part how to change these permissions in case they really are a problem.
Use the rm command to remove single files, rmdir to remove empty directories. (Use ls -a to check whether a directory is empty or not). The rm command also has options for removing non-empty directories with all their subdirectories, read the Info pages for these rather dangerous options.
On Linux, just like on UNIX, there is no garbage can - at least not for the shell, although there are plenty of solutions for graphical use. So once removed, a file is really gone, and there is generally no way to get it back unless you have backups, or you are really fast and have a real good system administrator. To protect the beginning user from this malice, the interactive behavior of the rm, cp and mv commands can be activated using the -i option. In that case the system won’t immediately act upon request. Instead it will ask for confirmation, so it takes an additional click on the Enter key to inflict the damage:
mary:~> rm -ri archive/ rm: descend into directory `archive'? y rm: descend into directory `archive/reports'? y rm: remove directory `archive/reports'? y rm: descend into directory `archive/backup'? y rm: remove `archive/backup/sysbup200112.tar'? y rm: remove directory `archive/backup'? y rm: remove directory `archive'? y
We will discuss how to make this option the default in Chapter 7, which discusses customizing your shell environment.
Prev: Orientation in the file system
Home
Next: Finding files
Copyright (c) by the authors.
This section of the wiki is licensed under the terms of the GNU Free Documentation License.
See the LBook-licensing page for details.
Linux® is a registered trademark of Linus Torvalds.