Navigating the Linux File System
◾
85
would now have directory temp2, files f1, f2, f3, and temp2 would have subdirectories a
and b, files f1, f2, f3, and subdirectory b would have b1 and b2. The
recursive copy is a very
convenient way to copy an entire portion of the file system to another location. Of course,
to copy the files and subdirectories, you would need to have adequate permissions.
3.3.3 File Deletion Commands
The next command to examine is
rm
, for remove or delete. The rm command has the
syntax
rm [
options
]
file(s)
As with mv and cp, the rm command can work on multiple files either by listing each
item (separated by spaces) or using wildcards, or both. If a directory contained files file1.
txt, file2.txt, and file3.txt (and no other files ending in .txt), and
you wanted to delete all
three, either of the following instructions would work
rm file*.txt
rm file1.txt file2.txt file3.txt
Obviously, the first instruction is simpler. However, if the directory did contain other
files whose names were similar, for instance, file4.txt and file10.txt, and you did not want
those deleted, you would have to use the latter instruction.
As rm permanently deletes files, it is safest to use rm with the option -i. This option
causes rm to pause before each file is deleted and ask the user if he or she is sure about
deleting that file. The user responds with a ‘y’ or ‘n’ answer.
Users may not think to use
rm
-i
, so it is common for a system administrator to create an alias of rm to rm -i so that, by
default, rm is always run with the -i option.
If you are applying rm to a lot of files, for instance by doing
rm *.txt
, you may not
want to be bothered with having to respond to all of the prompts (one per item being
deleted) and so you might use
rm -f *.txt
to override the prompting messages. This
can be dangerous though, so only use -f when you are sure that you want to delete the file(s)
specified.
The -r
option is the same as with cp, but in this case, the files and subdirectories are
deleted. If you (or the system administrator) have aliased rm to be rm -i, then with -r you
are asked before each deletion to confirm. This can be time consuming and undesirable. In
this case, you might combine -f and -r, as in
rm -fr *
This will delete everything in the current directory, any subdirectories and their con-
tents as well. Of course you should explore the current directory before using such a com-
mand to ensure that you do not delete content mistakenly.
Notice that
rm
-r
will delete directories.
Generally, rm cannot delete a directory.
You would have to use rmdir (explained below). But in the case of a recursive deletion,
86
◾
Linux with Operating System Concepts
directories can be removed once their contents are deleted. Referring back to Figure 3.4,
the command
rm -fr /home/foxr/temp2
would delete the files and directories in this order: f1, f2, f3, b1, b2, a, b, temp2.
3.3.4 Creating and Deleting Directories
To create a directory, use
mkdir
followed by the directory name as in
mkdir TEMP
The directory is created in the current working directory, unless a path is specified.
The directory is initially empty. The permissions for the directory default to permissions
that you have established
previously through the
umask
command. If you have not used
umask, then they default to the umask value established by the system administrator. We
look at umask in Chapter 9.
The mkdir command only allows for a few basic options. With -m or --
mode=
MODE
,
you can specify the initial permissions of the directory, otherwise they default to the per-
missions set up with umask. Alternatively, -Z or --
context=
CTX
can be used to spec-
ify SELinux (security enhanced) rather than normal permissions
to control access to the
directory. The context,
CTX
, provides for rules that describe access rights. SELinux is dis-
cussed in Chapter 8.
To remove a directory, it must be empty. Therefore, you would have to delete its contents
first. Once a directory is empty, to delete it, use the instruction
rmdir
. Attempting to
delete a nonempty directory yields the error message:
rmdir: failed to remove `dirname’: Directory not empty.
To delete the contents of the directory, you would want to use rm. By using the * wild-
card, it simplifies the rm operation. Now you will perform these three operations:
rm *
(in the directory will delete its contents)
cd ..
(to move to the parent directory)
rmdir
Do'stlaringiz bilan baham: