In this context, file handling means copying, moving and deleting files. Later, we will look at ways of changing file 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 previously. Without this option, mkdir would just fail, complaining that these directories do not exist;
it will return silently if the directory
you wanted 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[3]. 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) is equivalent to the DOS commands del and deltree, but has 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 Mandrakelinux,
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 which 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[4].
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
.
The syntax of the mv (MoVe) command is as follows:
mv [options] <file|directory> [file|directory ...] <destination>
Note that when you move multiple files the destination has to be a directory. To rename a file you simply move it to the new name.
-f
: forces
operation –– no warnings are given if an existing
file is to be 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 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) is equivalent to the DOS commands copy and xcopy but has more options. Its syntax is as follows:
cp [options] <file|directory> [file|directory ...] <destination>
cp has many 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 with the name
bar
in the current directory.
[3] In UNIX®, there are three
distinct timestamps for each file: the last file access date
(atime
), i.e. the date when the file was last
opened for read or write; the date when the inode attributes
were last modified (ctime
); and finally, the date
when the content of the file was last modified
(mtime
).
[4] 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.