Chapter 3. Introduction to the command line

Table of Contents
3.1. File Handling Utilities
3.1.1. mkdir, touch: creating empty directories and files (MaKe DIRectory)
3.1.2. rm: deleting files or directories (ReMove)
3.1.3. mv: moving or renaming files (MoVe)
3.1.4. cp: copying files and directories (CoPy)
3.2. Handling File Attributes
3.2.1. chown, chgrp: change the owner and group of one or more files
3.2.2. chmod: changing permissions on files and directories (CHange MODe)
3.3. shell globbing patterns and regular expressions
3.4. Redirections and pipes
3.4.1. A little more about processes
3.4.2. Redirections
3.4.3. Pipes
3.5. Command-line Completion
3.5.1. Example
3.5.2. Other Completion Methods
3.6. Starting and handling background processes: job control
3.7. A Final Word

In Chapter Basic Unix concepts of the User Guide, you were shown how to launch a shell. In this chapter we will show you how to put it to work.

The shell's main asset is the number of existing utilities: there are thousands of them, and each one is devoted to a particular task. We will only look at a small number of them here. One of Unix's greatest assets is the ability to combine these utilities, as we shall see later.

3.1. File Handling Utilities

In this instance, file handling means copying, moving and deleting files. Later, we will look at ways of changing their attributes (owner, permissions).

3.1.1. mkdir, touch: creating empty directories and files (MaKe DIRectory)

mkdir is used for creating directories. Its syntax is simple:
mkdir [options] <directory> [directory ...]
Only one option is worth noting: the option -p. If this option is set, mkdir will create parent directories if these did not exist before. If this option is not specified and the parent directories do not exist, mkdir will display an error. Examples:

  1. mkdir foo creates a directory foo in the current directory;

  2. mkdir -p images/misc docs creates a directory misc in directory images by first creating the latter if it does not exist, together with a directory docs.

Initially, the touch command is not intended for creating files but for updating file access and modification times[1]. However, touch will create the files mentioned as 0 byte files if they did not exist before. The syntax is:

touch [options] file [file...]

So running the command:

touch file1 images/file2

will create a 0 byte file called file1 in the current directory and a 0 byte file file2 in directory images.

3.1.2. rm: deleting files or directories (ReMove)

This command replaces the DOS commands del and deltree, and adds more options. Its syntax is as follows:
rm [options] <file|directory> [file|directory...]
Options include:

  1. -r, or -R Delete recursively. This option is mandatory for deleting a directory, empty or not. However, there is also the command rmdir for deleting empty directories.

  2. -i Request confirmation before each deletion. It is recommended to alias the bare rm command to rm -i in your shell. The same goes for the cp and mv commands.

  3. -f The opposite of -i, forces deletion of the files or directories, even if the user has no write authorization on the files[2].

Some examples:

  1. rm -i images/*.jpg file1 Deletes all files with names ending in .jpg in the directory images and deletes the file file1 in the current directory, requesting confirmation for each file. Answer y to confirm deletion, n to cancel.

  2. rm -Rf images/misc/ file* Deletes, without requesting confirmation, the whole directory misc/ in the directory images/ together with all files in the current directory with names beginning with file.

Warning

a file deleted using rm is deleted irrevocably. There is no way of restoring the files! Do not hesitate to use the -i option to ensure you don't delete something by mistake.

3.1.3. mv: moving or renaming files (MoVe)

The syntax of the mv command is as follows:
mv [options] <file|directory> [file|directory ...] <destination>
Some options:

  1. -f Forces file moving -- no warning if an existing file is overwritten by the operation.

  2. -i The opposite -- ask the user for confirmation before overwriting an existing file.

  3. -v Verbose mode, report all changes and activity.

Some examples:

  1. mv -i /tmp/pics/*.gif . Move all files in the directory /tmp/pics/ with names ending in .gif to the current directory (.), requesting confirmation before overwriting any files.

  2. mv foo bar Rename the file foo to bar.

  3. mv -vf file* images/ trash/ Move, without requesting confirmation, all files in the current directory with names beginning in file together with the entire directory images/ to the directory trash/, and show each operation carried out.

3.1.4. cp: copying files and directories (CoPy)

cp replaces the DOS commands copy, xcopy and adds more options. Its syntax is as follows:
cp [options] <file|directory> [file|directory ...] <destination>
cp has a lot of options. These are the most common:

  1. -R Recursive copy; mandatory for copying a directory, even empty.

  2. -i Request confirmation before overwriting any files which might be overwritten.

  3. -f The opposite of -i, replace any existing files without requesting confirmation.

  4. -v Verbose mode, displays all actions performed by cp.

Some examples:

  1. cp -i /tmp/images/* images/ Copies all files in the directory /tmp/images to the directory images/ located in the current directory, requesting confirmation if a file is going to be overwritten.

  2. cp -vR docs/ /shared/mp3s/* mystuff/ Copies the whole directory docs to the current directory plus all files in the directory /shared/mp3s to the directory mystuff located in the current directory.

  3. cp foo bar Makes a copy of the file foo under the name bar in the current directory.

Notes

[1]

There are three distinct timestamps for each file in Unix: the last file access date (atime), i.e. the last date when the file was opened for read or write; the last date when the inode attributes were modified (mtime); and finally, the last date when the contents of the file were modified (ctime).

[2]

It is enough for the user to have write access to the directory, to be able to delete files in it, even if he is not the owner of the files.


Tux on Star from MandrakeSoft Linux is a registered trademark of Linus Torvalds. All other trademarks and copyrights are the property of their respective owners.
Unless otherwise stated, all the content of these pages and all images are Copyright MandrakeSoft S.A. and MandrakeSoft Inc. 2000.
http://www.linux-mandrake.com/