2.6. Moving and Removing

2.6.1. realpath, mv

In Unix, every file has an absolute path that describes the traversal path from the root of the disk / to the file itself. Each path can be split into two components: the first describing the parents (the dirname); and the second describing the file at the end of the path (the basename). Consider the following path that we have carefully annotated using ASCII characters:

/home/myid/user/cs1302-unix/notes/cs1302/cpp.md
|                                       |     |
+---------------------------------------+-----+
                  |                        |
               dirname                 basename

Understanding this split is important when it comes to moving and renaming a file as both of those tasks modify a file’s absolute path when performed successfully.

If you modify

Related

dirname

basename

Task

move a file

rename a file

move and rename a file

If you are unsure what the absolute path for a file is, but you do know some relative path for it, then you can print its absolute path using the realpath command, supplying the relative path as a command-line argument:

cd ~/cs1302-unix
realpath notes/cs1302/cpp.md
../../_images/realpath-demo.svg

To move or rename a file in Unix, use the mv (move) command. Here is some usage information adapted from the manual:

Command

Description

mv SOURCE DEST

Rename SOURCE to DEST

mv SOURCE... DIRECTORY

Move SOURCE(s) into DIRECTORY

To move a file, use the mv command in a way that changes the file’s dirname.

cd ~/cs1302-unix
mv notes/cs1302/cpp.md notes/cs1730/cpp.md
../../_images/mv-first-demo.svg

State

Absolute Path

Before

/home/myid/■■■■■■■/cs1302-unix/notes/cs1302/cpp.md

After

/home/myid/■■■■■■■/cs1302-unix/notes/cs1730/cpp.md

To rename a file, use the mv command in a way that changes the file’s basename.

cd ~/cs1302-unix
mv notes/cs1730/cpp.md notes/cs1730/c.md
../../_images/mv-second-demo.svg

State

Absolute Path

Before

/home/myid/■■■■■■■/cs1302-unix/notes/cs1730/cpp.md

After

/home/myid/■■■■■■■/cs1302-unix/notes/cs1730/c.md

For more information about mv, consult the manual page using man mv.

Test Yourself

  • Why is the mv (move) command used to rename a file instead of some new command?

  • When is the output of pwd and realpath the same?

2.6.2. cp, cp -r

To copy a file in Unix, use the cp (copy) command. Here is some usage information adapted from the manual:

Command

Description

cp SOURCE DEST

Copy SOURCE to DEST

cp SOURCE... DIRECTORY

Copy SOURCE(s) into DIRECTORY

cd ~/cs1302-unix
cp notes/cs1730/c.md notes/cs1730/cpp.md
../../_images/cp-first-demo.svg
cd ~/cs1302-unix
cp books/moby_dick.txt notes/other/a/
../../_images/cp-second-demo.svg

The default behavior of cp when attempting to copy a directory is to copy the directory itself but not its contents. To make cp recursively copy the contents of a directory in addition to the directory itself, supply cp with the -r (recursive) option as a command-line argument.

cd ~/cs1302-unix
cp -r books books-copy

For more information about cp, consult the manual page using man cp.

Test Yourself

  • What command-line argument is needed to copy a directory using the cp command?

  • What are two different ways to make a copy of a file with a different name than original?

2.6.3. rm, rm -r

To remove or delete a file in Unix, use the rm (remove) command. If you have permission to remove a file, then you can do so by supplying rm a path to the file as a command-line argument. REMOVAL CANNOT BE UNDONE.

cd ~/cs1302-unix
rm notes/cs1730/c.md
../../_images/rm-first-demo.svg

The default behavior of rm is to not permit the removal of a directory file.

cd ~/cs1302-unix
rm notes/other
../../_images/rm-second-demo.svg

To make rm recursively remove the contents of a directory so that it can remove it, supply rm with the -r (recursive) option as a command-line argument. REMEMBER, THIS CANNOT BE UNDONE.

cd ~/cs1302-unix
rm -r notes/other
../../_images/rm-third-demo.svg

For more information about rm, consult the manual page using man rm.

Test Yourself

  • What directories should you avoid typing when using rm -r?