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?

Review Questions

Suppose that you are working directly inside of your home directory on Odin, execute the tree command, and observe the output below:

output from the Unix ``tree`` command showing the directory structure.
  1. If /home/users/user is the absolute path to your home directory, then which of the commands shown below would move you directly into the cs1730 directory? Select all that apply.

    1. cd /home/users/user/cs1302-unix/notes/cs1730

    2. cd ~/cs1302-unix/notes/cs1730

    3. cd cs1730

    4. cd ~/cs1730

    5. cd cs1302-unix/notes/cs1730

    6. cd /cs1302-unix/notes/cs1730

  2. Now, imagine that you have moved to the cs1730 directory, which of the following commands will get you back to your home directory?

    1. cd ~

    2. cd ../../..

    3. cd ../..

    4. cd ../

    5. cd /home/users/user

    6. cd /../../

Solutions (Don’t open until completing the questions above)
    1. Correct. This is a valid absolute path to the cs1730 directory.

    2. Correct. Using the ~ for an alias to the home directory, this will work.

    3. Incorrect. We cannot get directly into cs1730 from our home directory. This is an invalid relative path.

    4. Incorrect. cs1730 is not contained within our home directory.

    5. Correct. This is a valid relative path from our home directory to cs1730.

    6. Incorrect. This is an invalid absolute path.

    1. Correct. This command will always get you to your home directory since ~ is an alias for our home directory.

    2. Correct. This is a valid relative path that moves up three directories.

    3. Incorrect. This will move us to the cs1302-unix directory.

    4. Incorrect. This will move us up to the notes directory.

    5. Correct. This is a valid absolute path to our home directory.

    6. Incorrect. This is an invalid absolute path.