|
|
|
|
|
|
|
|
Creating/Renaming/Deleting Files and Directories
|
|
You can make a new directory with the "mkdir"
(make directory) command, and you can remove an empty directory with the
"rmdir" (remove directory) command.
If there is any files under a directory you want to delete,
you need to use -R (recursive) flag to delete the directory and
files under it.
Let's assume that following examples are done in the home
directory of user "wclinton."
You type |
It does |
$ mkdir timber |
create a directory timber in current directory |
$ mkdir ~/grisly |
create a directory grisly in home directory |
$ mkdir /home/wclinton/grisly |
the same as above example |
$ rmdir starr |
delete a directory starr in current directory |
$ rmdir -R ~/whitewater |
delete a directory whitewater AND all files/subdirectories under it |
$ mkdir -R
/home/wclinton/whitewater |
the same as above example |
|
Time to time, you'd like to know where in the file
tree a directory resides before using any of "mkdir" or "rmdir" command.
In such case, use "pwd"
(print working directory) command to display the absolute pathname of
your current working directory.
Directories under Unix can be renamed and moved around
with a single command. For example, whole "/data/life_saver" directory
(and whatever under it) can be moved to home directory by :
-
$ mv ~/data/life_saver ~
Files can be moved around in a similar way.
To rename a file under Unix, you use the "mv" (move) command.
For example, to change the name of file "foo" to "boo" in
current directory, you can type :
-
$ mv foo boo
You can also copy files with the "cp"
(copy) command. Here is an explanation by examples:
-
$ cp batman robin
makes a duplicate of the file "batman" and gives it
the name "robin" in the current directory. Note that
the filenames can include pathnames as well. Let's
say that you're currently in "/home/wclinton/project"
directory.
-
$ cp /home/wclinton/project/batman ./robin
Above "cp" command makes a copy of the file "batman"
found in the "/home/wclinton/project" directory and
places it in the current
working directory (= /home/wclinton) into a file
called "robin. "
If you don't specify the filename for "cp"
command, the same filename (=source filename)
will be used to create a duplicate.
-
$ cp catwoman /home/wclinton/project
Above "cp" command makes a copy of the file
"catwoman" found in current directory to a file
"/home/wclinton/project/catwoman. " The filename,
"catwoman" will remain unchanged.
Same manner, following "cp" command makes a copy
of the file "joker" found in "/home/wclinton/project"
directory to the current directory. The filename,
"joker" will remain unchanged.
-
$ cp /home/wclinton/project/joker .
You can also use a wildcard character, an asterisk
that matches any number of characters in a filename,
to copy all the files (but not the subdirectories)
from a directory all at once.
-
$ cp /home/wclinton/project/* .
will copy all the files (but not the subdirectories if
there is any) from "/home/wclinton/project" directory
into the current directory at once. Hence, you can copy
all the files and subdirectories (if there is any) from
"/home/wclinton/project" directory to the current
directory by using the "-R" (recursive) flag as follows:
-
$ cp -R /home/wclinton/project/* .
Same manner, to copy everything from the
"/home/wclinton/secret" directory into the "shredder"
directory under the home directory (assuming the "shredder"
directory already exists):
-
$ cp -R ~/secret/* ~/shredder
Keep in mind that all wildcard characters can be
used for "cp" and "mv" commands.
Another useful flag for "cp" command is the "-i"
(interactive) flag which will prompt for confirmation
if you are about to overwrite an existing file(s)
that happens to be the same filename(s) you're copying from.
A "y" answer means that the copy should proceed.
Any other answer prevents "cp" from overwriting.
It is a nice cautionary measure, however it can be a
tiresome routine if used repeatedly.
You can delete a file with the "rm"
(remove) command. Be careful if you type "rm *", because
Unix doesn't ask you if you're sure you want to do that.
Also, there is NO UNDELETE MECHANISM for the Unix file system.
When a file is gone, it's gone! BE CAREFUL. ONLY DELETE WHAT
YOU WANT TO DELETE.
If you're unsure about whether a wildcard will pick
up only what you want, do "ls" with that wildcard
first, to see what it matches, then run the "rm. "
You type |
It does |
$ rm uh-oh.txt |
delete a file uh-oh.txt in current directory |
$ rm ~/brainstorm/not_a_good.idea |
delete a file not_a_good.idea under directory brainstorm |
$ rm /home/wclinton/brainstorm/not_a_good.idea |
the same as above example |
|
Of course, similarly like in "cp" command,
you can use "-i" (Interactive)
flag with "rm" command to confirm before removing any files.
The bottomline is that you should know what you're doing
when you're using "cp",
"mv",
"rm" and
"rmdir" commands.
Make a good habit to visualize what will be the result
before issuing any one of those commands.
|
|
|