3.3.Manipulating the file system

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 Table3.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]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 -i option when using mv (see Table3.3, “Commands for manipulating the file system”).

Similarly, cp can silently replace files in the copying process. As with mv, you can specify the -i option to prevent cp from overwriting files.

[Caution]Caution

Although you can use rm with the wildcard (*) to quickly and easily remove files and directories, be careful: you can easily delete files that you did not mean to delete, and those deleted files cannot be recovered. This issue is particularly important when you are using the -rf option.

Example3.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.


Example3.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.


Example3.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.


Table3.3.Commands for manipulating the file system

CommandAction

cp [-i] [-r] source destination

Copy files and directories[a] [prompt before overwrite] [include all subdirectories and their contents]

ln -s target link_name

Make symbolic link to target

mkdir directory

Create directory

mv [-i] old_name new_name

Rename file or directory [prompt before overwrite][b]

mv [-i] source destination

Move file or directory [prompt before overwrite][b]

rm [-rf] [-i] files

Remove files or directories[c] [include all subdirectories and their contents, with no prompt for confirmation] [prompt before any removal]

rmdir directory

Remove empty directory

[a] Linux manual page for cp.

[b] Linux manual page for mv.

[c] Linux manual page for rm.



Back to Guide main page