-->
Previous Table of Contents Next


Chapter 17
Managing Files and Directories

by Jack Tackett

In this chapter
Listing Files
Organizing Files
Copying Files
Moving and Renaming Files
Removing Files or Directories
Viewing the Contents of a File
Searching for Files
Changing File Time and Date Stamps
Compressing Files

The vast majority of Linux commands manipulate files and directories. Indeed, Linux shell scripts are particularly adept at manipulating files and directories. File manipulations that are difficult in a conventional language (even in C) are made easy from within a shell, largely because of the rich selection of file-manipulation commands available in Linux.

File-manipulation commands can be grouped roughly into two categories:

  Commands that manipulate files as objects
  Commands that manipulate the contents of files

This chapter concentrates on commands that manipulate files as objects—commands that move, rename, copy, delete, locate, and change the attributes of files and directories. This chapter also takes a quick look at commands that manipulate the contents of files.

Listing Files

The basic command to list files is ls. The way ls displays files depends on how you use the command. If you use the ls command in a pipe, every file is displayed on a line by itself. This is also the default for some versions of UNIX, such as SCO UNIX. Other versions of UNIX list files in several columns. For most uses, the columnar format is more convenient; systems that list files one per row often have an alternative command, usually lc, for lists in column format.

The ls command’s behavior is modified with the use of flags that take the form -abcd. In general, versions of the ls command fall into two categories: versions of ls derived from Linux System V and those derived from Berkeley. Because the Berkeley Linux systems are slowly giving way to Linux System V, this chapter concentrates on the flags used by System V. If you’re in doubt about which version of ls you have, consult the manuals for your system or try the command man ls.


NOTE:  Most man pages for commands in this chapter are no longer being maintained and may be inaccurate or incomplete under Red Hat Linux because the system is moved to more graphical-based systems such as HTML and Texinfo. However, for the time being, this information is accurate for this release of Red Hat Linux 4.0.

Flags used with the ls command can be concatenated or listed separately. This means that the following commands are effectively identical:


ls -l –F

and


ls -lF

Table 17.1 lists in alphabetical order several of the flags used with ls and their uses.

Table 17.1 Flags for the ls Command

Flag Description

-a Lists all entries. In the absence of this or the -A option, entries whose names begin with a period (.) aren’t listed. Linux has a way of “hiding” files; all files that begin with a period by default aren’t listed because they’re generally files used to customize applications. For example, .profile is used to customize the Bourne and Korn shells, and .mailrc is used to customize your system-wide e-mail configuration file. Because almost every major command you use has a startup file, your home directory looks cluttered if the ls command lists all those startup files by default. If you want to see them, use the -a flag.
-A Same as -a, except that . and .. aren’t listed. (Recall from Chapter 16, “Understanding the File and Directory System,” that . is a pseudonym for the current directory, and .. is a pseudonym for the parent directory.) Because these filenames begin with a period, the -a flag lists them. If you don’t want to see these pseudonyms, use the -A flag instead.
-b Forces printing of nongraphic characters to be in octal \ddd notation. -b is more useful than the -q flag because it allows you to figure out what the characters are.
-c Uses time of last edit (or last mode change) for sorting or printing. Linux maintains three time and date stamps on every file: the file creation date, the date of last access, and the date of last modification. Normally, files are listed in ASCII order (which is the same as alphabetical order except that capitals are sorted before lowercase letters).
-C Forces multicolumn output with entries sorted down the columns. This is the default format of ls when output is to a terminal.
-d filename If the argument is a directory, this flag lists only its name (not its contents); often used with the -l flag to get the status of a directory. Normally, the contents of a directory are listed if a directory name is explicitly listed or implied with the use of a wildcard. Thus, the simple command ls lists just the directory names themselves, but ls * lists files, directories, and the contents of any directories encountered in the current directory.
-F Marks directories with a trailing slash (/), marks executable files with a trailing asterisk (*), marks symbolic links with a trailing at sign (@), marks FIFOs with a trailing bar (|), and marks sockets with a trailing equal sign (=).
-i Prints each file’s inode number (inodes are described in Chapter 16, “Understanding the File and Directory System”) in the first column of the report. If you list linked files, notice that both files have the same inode number.
-l Lists directory entries in long format, giving mode, number of links, owner, size in bytes, and time of last modification for each file. If the file is a special file, the size field instead contains the major and minor device numbers. If the time of last modification is greater than six months ago, the month, date, and year are shown; otherwise, only the date and time are shown. If the file is a symbolic link, the path name of the linked-to file is printed, preceded by the characters ->. You can combine -l with other options, such as -n, to show user and group ID numbers instead of names.
-n Lists the user and group ID numbers, instead of names, associated with each file and directory. Usually, only the names are listed. If you’re setting up networking products, such as TCP/IP, it’s useful to know ID numbers when you’re setting up permissions across several systems.
-q Displays nongraphic characters in filenames as the character ?. For ls, this is the default action when output is to a terminal. If a file has accidentally been created with nonprintable characters, the -q flag displays the file.
-r Reverses the sort order to show files in reverse alphabetical or oldest-file-first order, as appropriate.
-s Gives the size of each file, including any indirect blocks used to map the file, in kilobytes. If the environment variable POSIX_CORRECT is defined, the block size is 512 bytes.
-t Sorts by time modified (latest first) instead of by name. If you want to see the oldest file first, use the -rt combination.
-u Uses time of last access, instead of last modification, for sorting (with the -t option) or printing (with the -l option).
-x Forces multicolumn output with entries sorted across instead of down the page.


Previous Table of Contents Next