This section covers basic file and directory management, including filesystems, files and directories, standard file management commands, their recursive capabilities (where applicable), and wildcard patterns.
Nearly every operating system that has ever been devised structures its collection of stored objects in a hierarchy, which is a tree of objects containing other objects. This hierarchy allows a sane organization of objects and allows identically named objects to appear in multiple locations—this is essential for multiuser systems like Linux. Information about each object in the filesystem is stored in a table (which itself is part of the filesystem), and each object is numbered uniquely within that table. Although there are a few special object types on Linux systems, the two most common are directories and files.
A directory is a container intended to hold objects such as files and other directories. A directory's purpose is primarily for organization. A file, on the other hand, exists within the directory and its purpose is to be responsible for raw data
storage. At the top of all Linux filesystem hierarchies is a directory depicted simply by /; this is known as the root directory. Beneath / are named directories and files in an organized and well-defined tree. To describe these objects, you simply refer to them by name separated by the / character. For example, the object ls is an executable program stored in a directory called /bin under the root directory; it is depicted simply as /bin/ls.
The identification information for a filesystem object is known as its inode. Inodes carry information about objects, such as where they are located on disk, their modification time, security settings, and so forth. Each Linux ext2 filesystem is created with a finite number of inodes that is calculated based on the size of the filesystem and other options that are given to mke2fs. Multiple objects in the filesystem can share the same inode; this concept is called linking.
Once a hierarchy is defined, there is a constant need to manage the objects in the filesystem. Objects are constantly created, read, modified, copied, moved, and deleted, so wisely managing the filesystem is one of the most important tasks of a system administrator. In this section, we discuss the basic command-line utilities used for file and directory management. While there are GUI tools for this task, the LPI Level 1 exams only test on command-line tools.
cp
cp [options
]file1 file2
cp [options
] files directory
In the first command form, copy file1
to file2
. If file2
exists and you have appropriate privileges, it will be overwritten without warning (unless you use the -i option). Both file1
and file2
can be any valid filename, either fully qualified or in the local directory. In the second command form, copy files
to directory
. Note that the presence of multiple files implies that you wish to copy the files to a directory. If directory
doesn't exist, an error message will be printed. This command form can get you in trouble if you attempt to copy a single file into a directory that doesn't exist, as the command will be interpreted as the first form and you'll end up with file2
instead of directory
.
Force an overwrite of existing files in the destination.
Prompt interactively before overwriting destination files. It is common practice (and advised) to alias the cp command to cp -i to prevent accidental overwrites. You may find that this is already done for you for root
on your Linux system.
Preserve all information, including owner, group, permissions, and timestamps. Without this option, the copied file or files will have the present date and time, default permissions, owner, and group.
Recursively copy directories. You may use either upper or lowercase for this option. If file1
is actually a directory instead of a file and the recursive option is specified, file2
will be a copy of the entire hierarchy under directory file1
.
Display the name of each file verbosely before copying.
Copy the messages file to the local directory (specified by .
):
$ cp /var/log/messages .
Make an identical copy, including preservation of file attributes, of directory src
in new directory src2
:
$ cp -Rp src src2
Copy file1, file2, file5, file6
, and file7
from the local directory into your home directory (in bash):
$ cp file1 file2 file[567] ~
mkdir
mkdir [options] directories
Create one or more directories
. You must have write permission in the directory where directories
are to be created.
mode
Set the access mode
for directories
.
Create intervening parent directories if they don't exist.
Create a read-only directory named personal:
$ mkdir -m 444 personal
Create a directory tree in your home directory, as indicated with a leading tilde (~), using a single command:
$ mkdir -p ~/dir1/dir2/dir3
In this case, all three directories are created. This is faster than creating each directory individually.
mv
mv [options] source target
Move or rename files and directories. For targets
on the same filesystem (partition), moving a file doesn't relocate the contents of the file itself. Rather, the directory entry for the target is updated with the new location. For targets
on different filesystems, such a change can't be made, so files are copied to the target location and the original sources are deleted.
If a target file or directory does not exist, source
is renamed to target
. If a target
file already exists, it is overwritten with source
. If target
is an existing directory, source
is moved into that directory. If source
is one or more files and target
is a directory, the files are moved into the directory.
Force the move even if target
exists, suppressing warning messages.
Query interactively before moving files.
rm
rm [options] files
Delete one or more files from the filesystem. To remove a file, you must have write permission in the directory that contains the file, but you do not need write permission on the file itself. The rm command also removes directories when the -d, -r, or -R option is used.
-d
Remove directories even if they are not empty. This option is reserved for privileged users.
-f
Force removal of write-protected files without prompting.
-i
-r
,-R
If the file is a directory, recursively remove the entire directory and all of its contents, including subdirectories.
rmdir
touch
touch [options] files
Change the access and/or modification times of files
. This command is used to refresh timestamps on files. Doing so may be necessary, for example, to cause a program to be recompiled using the date-dependant make utility.
Change only the access time.
Change only the modification time.
timestamp
Instead of the current time, use timestamp
in the form of [[CC]YY]MMDDhhmm[.ss]
. For example, the timestamp for January 12, 2001, at 6:45 p.m. is 200101121845.
When working with files on the command line, you'll often run into situations in which you need to perform operations on many files at once. For example, if you are developing a C program, you may want to touch all of your .c files in order to be sure to recompile them the next time you issue the make utility to build your program. There will also be times when you need to move or delete all the files in a directory or at least a selected group of files. At other times, filenames may be long or difficult to type, and you'll want to find an abbreviated alternative to typing the filenames for each command you issue (see Table 5-3).
To make these operations simpler, all shells on Linux offer file-naming wildcards ( ).
Wildcards are expanded by the shell, not by commands. When a command is entered with wildcards included, the shell first expands all the wildcards (and other types of expansion) and passes the full result on to the command. This process is invisible to you.
Rather than explicitly specifying every file or typing long filenames, you can use wildcard characters in place of portions of the filenames can usually do the work for you. For example, the shell expands things like *.txt to a list of all the files that end in .txt. File wildcard constructs like this are called file globs, and their use is awkwardly called globbing. Using file globs to specify multiple files is certainly a convenience, and in many cases is required to get anything useful accomplished. Wildcards for shell globbing are listed in Table 5-3.
Table 5-3. Common file-naming wildcards
The following are a few common applications for wildcards:
If you remember part of a filename but not the whole thing, use wildcards with the portion you remember to help find the file. For example, if you're working in a directory with a large number of files and you know you're looking for a file named for Linux, you may enter a command like this:
$ ls -l *inux*
When working with groups of related files, wildcards can be used to help separate the groups. For example, suppose you have a directory full of scripts you've written. Some are Perl scripts, for which you've used an extension of .pl, and some are Python, with a .py extension. You may wish to separate them into new separate directories for the two languages like this:
$mkdir perl python
$mv *.pl perl
$mv *.py python
Wildcards match directory names as well. Suppose you have a tree of directories starting with contracting, where you've created a directory for each month (that is, contracting/january, contracting/february, through contracting/december). In each of these directories are stored invoices, named simply invoice_
custa
_01.txt, invoice_
custa
_02.txt, invo
ice_custb_01.txt, and so on, where custa
and custb
are customer names of some form. To display all of the invoices, wildcards
can be used:
$ ls con*/*/inv*.txt
The con*
matches contracting. The second matches all directories under the contracting directory (january through december). The last matches all the customers and each invoice number for each customer.
See the bash manpages or info page for additional information on how bash handles expansions and on other expansion forms.