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 they 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.
Initially, the touch command was not intended for creating files but for updating file access and modification times[2]. However, touch will create the files listed as empty files if they do not exist. The syntax is:
touch [options] file [file...] |
touch file1 images/file2 |
will create an empty file called file1 in the current directory and an empty file file2 in directory images, if the files did not previously exist.
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...] |
Options include:
-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 create an empty ~/.alias file that will prevent setting system wide aliases. Alternatively you can edit your ~/.bashrc file to disable some of the system wide aliases by adding 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[3].
rm -i images/*.jpg file1: deletes all files with names ending in .jpg in the images directory and deletes 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 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. [4]! 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> |
Some options:
-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.
Some examples:
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 already there.
mv foo bar: rename file foo to bar. If a bar directory already existed, the effect of this command would be to move file foo or the whole 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 and xcopy and adds more options. Its syntax is as follows:
cp [options] <file|directory> [file|directory ...] <destination> |
cp has a lot of options. Here are the most common:
cp -i /timages/* images/: copies all files in the /timages/ 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.
[2] 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).
[3] 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.
[4] there are actually several ways to do this but no trivial ways.