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
To move or rename a file in Unix, use the mv
(move) command.
Here is some usage information adapted from the manual:
Command |
Description |
---|---|
|
Rename |
|
Move |
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
State |
Absolute Path |
---|---|
Before |
|
After |
|
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
State |
Absolute Path |
---|---|
Before |
|
After |
|
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
andrealpath
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 |
---|---|
|
Copy |
|
Copy |
cd ~/cs1302-unix
cp notes/cs1730/c.md notes/cs1730/cpp.md
cd ~/cs1302-unix
cp books/moby_dick.txt notes/other/a/
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
The default behavior of rm
is to not permit the removal
of a directory file.
cd ~/cs1302-unix
rm notes/other
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
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:
If
/home/users/user
is the absolute path to your home directory, then which of the commands shown below would move you directly into thecs1730
directory? Select all that apply.cd /home/users/user/cs1302-unix/notes/cs1730
cd ~/cs1302-unix/notes/cs1730
cd cs1730
cd ~/cs1730
cd cs1302-unix/notes/cs1730
cd /cs1302-unix/notes/cs1730
Now, imagine that you have moved to the
cs1730
directory, which of the following commands will get you back to your home directory?cd ~
cd ../../..
cd ../..
cd ../
cd /home/users/user
cd /../../
Solutions (Don’t open until completing the questions above)
Correct. This is a valid absolute path to the
cs1730
directory.Correct. Using the
~
for an alias to the home directory, this will work.Incorrect. We cannot get directly into
cs1730
from our home directory. This is an invalid relative path.Incorrect.
cs1730
is not contained within our home directory.Correct. This is a valid relative path from our home directory to
cs1730
.Incorrect. This is an invalid absolute path.
Correct. This command will always get you to your home directory since
~
is an alias for our home directory.Correct. This is a valid relative path that moves up three directories.
Incorrect. This will move us to the
cs1302-unix
directory.Incorrect. This will move us up to the
notes
directory.Correct. This is a valid absolute path to our home directory.
Incorrect. This is an invalid absolute path.