In the chapter "Basic UNIX System Concepts", you were shown how to launch a shell. In this chapter, we will show you how to work with it.
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 (very) small number of them here. One of UNIX's greatest assets is the ability to combine these utilities, as we shall see later.
In this context, file handling means copying, moving and deleting files. Later, we will look at ways of changing their attributes (owner, permissions).
mkdir (MaKe DIRectory) is used to create directories. Its syntax is simple:
mkdir [options] <directory> [directory ...] |
Only one option is worth noting: the -p option. It does two things:
it will create parent directories if these did not exist before. Without this option, mkdir would just fail, complaining that the said parent directories do not exist;
it will return silently if the directory which you want to create already exists. Similarly, if you did not specify the -p option, mkdir will send back an error message, complaining that the directory already exists.
Here are some examples:
mkdir foo: creates a directory foo in current directory;
mkdir -p images/misc docs: creates the misc directory in the images directory. First, it creates the latter if it does not exist (-p); it also creates a directory named docs in the current directory.
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 empty files if they did not exist before. The syntax is:
touch [options] file [file...] |
So running the command:
touch file1 images/file2 |
will create an empty file called file1 in the current directory and an empty file file2 in directory images.
The rm command (ReMove) replaces the DOS commands del and deltree, and adds more options. Its syntax is as follows:
rm [options] <file|directory> [file|directory...] |
-r, or -R: delete recursively. This option is mandatory for deleting a directory, empty or not. However, you can also use rmdir to delete empty directories.
-i: request confirmation before each deletion. Note that by default in Mandrake Linux, rm is an alias to rm -i, for safety reasons (similar aliases exist for cp and mv). Your mileage may vary as to the usefulness of these aliases. If you want to remove them, you can edit your .bashrc and add this line: unalias rm cp mv.
-f, the opposite of -i, forces deletion of the files or directories, even if the user has no write access on the files[2].
Some examples:
rm -i images/*.jpg file1: deletes all files with names ending in .jpg in the images directory and deletes the file1 file in the current directory, requesting confirmation for each file. Answer y to confirm deletion, n to cancel.
rm -Rf images/misc/ file*: deletes, without requesting confirmation, the whole directory misc/ in the images/ directory, together with all files in the current directory whose names begin with file.
![]() | Using rm deletes files irrevocably. There is no way to restore them! Do not hesitate to use the -i option to ensure you do not delete something by mistake. |
The syntax of the mv (MoVe) command is as follows:
mv [options] <file|directory> [file|directory ...] <destination> |
-f: forces operation – no warning if an existing file is overwritten.
-i: the opposite. Asks the user for confirmation before overwriting an existing file.
-v: verbose mode, report all changes and activity.
mv -i /tmp/pics/*.png .: move all files in the /tmp/pics/ directory whose names end with .png to the current directory (.), but request confirmation before overwriting any files.
mv foo bar: rename file foo to bar. If a bar directory already existed, the effect of this command would be to move the whole foo directory (the directory itself plus all files and directories in it, recursively) into the bar directory.
mv -vf file* images/ trash/: move, without requesting confirmation, all files in the current directory whose names begin with file, together with the entire images/ directory to the trash/ directory, and show each operation carried out.
cp (CoPy) replaces the DOS commands copy, xcopy and adds more options. Its syntax is as follows:
cp [options] <file|directory> [file|directory ...] <destination> |
-R: recursive copy; mandatory for copying a directory, even empty.
-i: request confirmation before overwriting any files which might be overwritten.
-f: the opposite of -i, replaces any existing files without requesting confirmation.
-v: verbose mode, displays all actions performed by cp.
Some examples:
cp -i /tmp/images/* images/: copies all files in the /tmp/images/ directory to the images/ directory located in the current directory. It requests confirmation if a file is going to be overwritten.
cp -vR docs/ /shared/mp3s/* mystuff/: copies the whole docs directory, plus all files in the /shared/mp3s directory to the mystuff directory.
cp foo bar: makes a copy of the foo file under the name bar in the current directory.
[1] | In UNIX, there are three distinct timestamps for each file: 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. |