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.
In this instance, file handling means copying, moving and deleting files. Later, we will look at ways of changing their attributes (owner, permissions).
mkdir is used for creating directories. Its syntax is simple:
mkdir [options] <directory> [directory ...] |
mkdir foo creates a directory foo in the current directory;
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.
This command 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, there is also the command rmdir for deleting empty directories.
-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.
-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:
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.
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.
![]() | 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. |
The syntax of the mv command is as follows:
mv [options] <file|directory> [file|directory ...] <destination> |
-f Forces file moving -- no warning if an existing file is overwritten by the operation.
-i The opposite -- ask the user for confirmation before overwriting an existing file.
-v Verbose mode, report all changes and activity.
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.
mv foo bar Rename the file foo to bar.
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.
cp 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, replace 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 directory /tmp/images to the directory images/ located in the current directory, requesting confirmation if a file is going to be overwritten.
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.
cp foo bar Makes a copy of the file foo under the name bar in the current directory.
[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. |