3.3.2.2. The tools
3.3.2.2.1.
Creating directories
A way of keeping things in place is to give certain files
specific default locations by creating directories and
subdirectories (or folders and sub-folders if you wish). This is
done with the mkdir command:
richard:~> mkdir archive
richard:~> ls -ld archive
drwxrwxrwx 2 richard richard 4096 Jan 13 14:09 archive/
|
Creating directories and subdirectories in one step is done
using the -p option:
richard:~> cd archive
richard:~/archive> mkdir 1999 2000 2001
richard:~/archive> ls
1999/ 2000/ 2001/
richard:~/archive> mkdir 2001/reports/Restaurants-Michelin/
mkdir: cannot create directory `2001/reports/Restaurants-Michelin/':
No such file or directory
richard:~/archive> mkdir -p 2001/reports/Restaurants-Michelin/
richard:~/archive> ls 2001/reports/
Restaurants-Michelin/
|
If the new file needs other permissions than the default file
creation permissions, the new access rights can be set in one move,
still using the mkdir command, see the
Info pages for more. We are going
to discuss access modes in the next section on file security.
The name of a directory has to comply with the same rules as
those applied on regular file names. One of the most important
restrictions is that you can't have two files with the same name in
one directory (but keep in mind that Linux is, like UNIX, a case
sensitive operating system). There are virtually no limits on the
length of a file name, but it is usually kept shorter than 80
characters, so it can fit on one line of a terminal. You can use
any character you want in a file name, although it is advised to
exclude characters that have a special meaning to the shell. When
in doubt, check with
Appendix C.
3.3.2.2.2.
Moving files
Now that we have properly structured our home directory, it is
time to clean up unclassified files using the mv command:
richard:~/archive> mv ../report[1-4].doc reports/Restaurants-Michelin/
|
This command is also applicable when renaming files:
richard:~> ls To_Do
-rw-rw-r-- 1 richard richard 2534 Jan 15 12:39 To_Do
richard:~> mv To_Do done
richard:~> ls -l done
-rw-rw-r-- 1 richard richard 2534 Jan 15 12:39 done
|
It is clear that only the name of the file changes. All other
properties remain the same.
Detailed information about the syntax and features of the
mv command can be found in the man or Info
pages. The use of this documentation should always be your first
reflex when confronted with a problem. The answer to your problem
is likely to be in the system documentation. Even experienced users
read man pages every day, so beginning users should read them all
the time. After a while, you will get to know the most common
options to the common commands, but you will still need the
documentation as a primary source of information. Note that the
information contained in the HOWTOs, FAQs, man pages and such is
slowly being merged into the Info pages, which are today the most
up-to-date source of online (as in readily available on the system)
documentation.
3.3.2.2.3.
Copying files
Copying files and directories is done with the cp command. A useful option is recursive copy (copy
all underlying files and subdirectories), using the -R option to cp. The general
syntax is
cp [-R] fromfile
tofile
As an example the case of user newguy, who wants the
same Gnome desktop settings user oldguy has. One way to
solve the problem is to copy the settings of oldguy to the
home directory of newguy:
victor:~> cp -R ../oldguy/.gnome/ .
|
This gives some errors involving file permissions, but all the
errors have to do with private files that newguy doesn't
need anyway. We will discuss in the next part how to change these
permissions in case they really are a problem.
3.3.2.2.4.
Removing files
Use the rm command to remove single
files, rmdir to remove empty directories.
(Use ls -a to check
whether a directory is empty or not). The rm
command also has options for removing non-empty directories with
all their subdirectories, read the Info pages for these rather
dangerous options.
|
How empty can a directory
be? |
|
It is normal that the directories . (dot) and .. (dot-dot) can't
be removed, since they are also necessary in an empty directory to
determine the directories ranking in the file system hierarchy.
|
On Linux, just like on UNIX, there is no garbage can - at least
not for the shell, although there are plenty of solutions for
graphical use. So once removed, a file is really gone, and there is
generally no way to get it back unless you have backups, or you are
really fast and have a real good system administrator. To protect
the beginning user from this malice, the interactive behavior of
the rm, cp and
mv commands can be activated using the
-i option. In that case the system won't
immediately act upon request. Instead it will ask for confirmation,
so it takes an additional click on the Enter
key to inflict the damage:
mary:~> rm -ri archive/
rm: descend into directory `archive'? y
rm: descend into directory `archive/reports'? y
rm: remove directory `archive/reports'? y
rm: descend into directory `archive/backup'? y
rm: remove `archive/backup/sysbup200112.tar'? y
rm: remove directory `archive/backup'? y
rm: remove directory `archive'? y
|
We will discuss how to make this option the default in
Chapter 7, which discusses customizing your
shell environment.