While being able to examine the file system is important, you'll also need to be able to make changes to it, by creating, copying, moving, and removing files and directories, as well as making symbolic links. Relevant commands can be found in Table 3.3, “Commands for manipulating the file system”. Note that mv can be used either to rename a file/directory or to move it, depending on the arguments passed to mv.
Caution | |
---|---|
If mv is used to move a file to a place where a file with the name already exists or to rename a file such that a file with the new name already exists in that directory, the old file will be silently replaced, unless you specify the Similarly, cp can silently replace files in the copying process. As with mv, you can specify the |
Caution | |
---|---|
Although you can use rm with the wildcard ( |
Example 3.5. Using ln
ln -s ~/classes/cse332/lab5 332lab5
will create a symbolic link (that is, an alias or shortcut) to ~/classes/cse332/lab5
called 332lab5
, placing the symbolic link in the current directory.
Example 3.6. Using mv
Assuming that foo
is a directory and that bar
is a file in foo
:
mv bar baz
will rename bar
to baz
, silently replacing any pre-existing file in foo
with the name baz
.
mv bar ..
will move bar
to one directory above foo
, silently replacing any pre-existing file in that directory with the name bar
.
mv bar ../baz
will move bar
to one directory above foo
and rename bar
to baz
, silently replacing any pre-existing file in that directory with the name baz
.
Example 3.7. Using rm
rm *.txt
will delete all files whose names end in .txt
from the current directory only.
rm * .txt
, however, will delete ALL files (but not directories) in the current directory and will then try to delete a file named .txt
.
Therefore, when using rm, double-check your typing before you press Enter!
rm -rf foo
will delete foo
regardless of whether it is a file or directory; if foo
is a directory, then all of foo
's contents (including subdirectories) will also be deleted.
Table 3.3. Commands for manipulating the file system
Command | Action |
---|---|
| Copy files and directories[a] [prompt before overwrite] [include all subdirectories and their contents] |
| Make symbolic link to target |
| Create directory |
| Rename file or directory [prompt before overwrite][b] |
| Move file or directory [prompt before overwrite][b] |
| Remove files or directories[c] [include all subdirectories and their contents, with no prompt for confirmation] [prompt before any removal] |
| Remove empty directory |
[a] Linux manual page for cp. [b] Linux manual page for mv. [c] Linux manual page for rm. |