Lesson 4: Working with files in Unix - moving, renaming, and removing files and directories
Quick review
In previous the lesson, we learned how to copy content (cp
), change file and directory permissions (chmod
), and remove files (rm
).
Lesson objectives
In this lesson we will achieve the following.
- Know how to move and rename files and directories
- Be able to remove directories.
Unix commands that we will visit in this lesson
cp
(to copy files or directories)mv
(to move/rename files or directories)rm
(to remove/delete files or directories)rmdir
(to remove empty directory)tree
(to view directory tree)
Connecting to Biowulf
Before getting started, connect to Biowulf.
ssh username@biowulf.nih.gov
Moving and renaming files and directories
For this portion of the lesson, change into your data directory.
cd /data/username
Then, copy over the folder unix_on_biowulf_2023_documents from /data/classes/BTEP.
cp -r /data/classes/BTEP/unix_on_biowulf_2023_documents .
Change into unix_on_biowulf_2023_documents after copying.
cd unix_on_biowulf_2023_documents
Then, change into the folder unix_on_biowulf_2023.
cd unix_on_biowulf_2023
Let's make a copy of counts.csv called counts_copy.csv and a copy of text_1.txt called text_1_copy.txt.
cp counts.csv counts_copy.csv
cp text_1.txt text_1_copy.txt
Next, we will create a folder called lesson4 inside the present working directory (ie. unix_on_biowulf_2023).
mkdir lesson4
Moving files from one directory to another
To move files from one directory to another, we use the mv
command. Let's move the copied files (counts_copy.csv and text_1_copy.txt) to the lesson4 folder. The command syntax is mv
followed the following arguments
- Argument: file that we want to move (ie. counts_copy.csv and text_1_copy.txt )
- Argument: folder we like to move the file to (ie. lesson4)
mv counts_copy.csv lesson4
mv text_1_copy.txt lesson4
After moving, let's list the contents of unix_on_biowulf_2023 to make sure the move was successful.
ls
As we hoped, counts_copy.csv and text_1_copy.txt are not in this folder.
counts.csv lesson4 results.csv text_1.txt
Renaming files
Let's now change into the lesson4 folder and list its contents.
cd lesson4
ls
Indeed, we see that the two files now reside in the lesson4 folder.
counts_copy.csv text_1_copy.txt
Let's rename counts_copy.csv to counts_lesson4.csv using the mv
command, with the arguments below.
- Argument: the file that we want to rename
- Argument: the name of the new file
mv counts_copy.csv counts_lesson4.csv
Listing the contents of the lesson4 folder, we will see that we now have the file counts_lesson4.csv rather than counts_copy.csv.
ls
counts_lesson4.csv text_1_copy.txt
Let's cp the counts_lesson4.csv and text_1_copy.txt back to the unix_on_biowulf_2023 folder, which is just one directory up. Recall that we can use ".." to denote one directory up.
cp counts_lesson4.csv ../
cp text_1_copy.txt ../
Now, let's go one directory back to the unix_on_biowulf_2023 folder. Again, we will use ".." to indicate one directory up.
cd ..
Listing the contents of unix_on_biowulf_2023, we see that counts_lesson4.csv and text_1_copy.txt have been copied over.
ls
counts.csv counts_lesson4.csv lesson4 results.csv text_1_copy.txt
text_1.txt
Renaming folders
We can also use the mv
command to rename folders. To see this let's make a copy of the lesson4 folder called lesson4_new. Again, we need to append -r
to the cp
command since we are copying a directory.
cp -r lesson4 lesson4_new
Listing the contents of unix_on_biowulf_2023, we see that lesson4_new has been added.
ls
counts.csv counts_lesson4.csv lesson4 lesson4_new results.csv
text_1_copy.txt text_1.txt
Now, we want to use mv
to rename lesson4_new to lesson4_copy where the arguments are
- Argument: the folder that we want to rename
- Argument: new name for the folder
mv lesson4_new lesson4_copy
Listing the contents of unix_on_biowulf_2023, we see that we now have a folder called lesson4_copy in place of lesson4_new.
ls
counts.csv counts_lesson4.csv lesson4 lesson4_copy results.csv
text_1_copy.txt text_1.txt
Here, we will make a copy of the folder lesson4_copy and name it lesson4_copy_1. Again, using cp -r
to copy recursively.
cp -r lesson4_copy lesson4_copy_1
Moving a folder into another folder
Next, we will create a new folder called new_house.
mkdir new_house
Then we will move lesson4_copy_1 to the folder new_house. Moving a folder to another only works when the name of the destination folder already exists. In the mv
command below, the arguments are
- Argument: folder we like to move (ie. lesson4_copy_1)
- Argument: destination that we like to move the folder to (ie. new_house)
mv lesson4_copy_1/ new_house/
Now, if we listed the contents of new_house, we will see that it has as a subfolder, lesson4_copy_1.
ls -l new_house
total 1
drwxr-x---. 2 wuz8 wuz8 4096 Jan 12 20:15 lesson4_copy_1
If we used the tree
command, we can get a hierarchical tree print out of contents inside a folder. For example, doing this for the folder new_house, we will see the lesson4_copy_1 subfolder, as well as the files within lesson4_copy_1.
tree new_house
new_house/
└── lesson4_copy_1
├── counts_lesson4.csv
└── text_1_copy.txt
Removing or deleting
The command to remove or delete something in Unix is rm
. To explore this command, let's change into the lesson4_copy folder.
cd lesson4_copy
Next, list the contents to see the contents of this folder.
ls
counts_lesson4.csv text_1_copy.txt
To remove text_1_copy.txt, we will use the rm
command and provide as the following arguments, the file or directory that we wish to remove.
rm text_1_copy.txt
If we listed the contents of lesson4_copy, we will see that text_1_copy.txt has disappeared.
ls
counts_lesson4.csv
Let's copy text_1_copy.txt back to the lesson4_copy folder. There is a copy of the text_1_copy.txt file that resides one directory up in the lesson4 subfolder of the unix_on_biowulf_2023 folder; thus, with the cp
command, we can use ".." to denote one directory up, which is the unix_on_biowulf_2023 folder, and then "/lesson4" to get into the lesson4 folder. Because we are still in the folder lesson4_copy, we can tell cp
to copy to here, in the present working directory, which is denoted by ".".
cp ../lesson4/text_1_copy.txt .
Listing the content again, we see that text_1_copy.txt has been added back.
ls
counts_lesson4.csv text_1_copy.txt
Recall that in Unix, we do not have a trash can or recycle bin to place stuff that we deleted and then recover. At best, Biowulf provides snapshots that we can use for reovery purposes. Looking the help document for rm
, we see that there is an option -i
that will prompt and ask before we remove something. This will allow us to review before deleting.
rm --help
-i prompt before every removal
Let's try rm -i
with text_1_copy.txt.
rm -i text_1_copy.txt
We will be asked whether we really want to remove. We can respond with no at the prompt if we do not want to remove the file.
rm: remove regular file ‘text_1_copy.txt’?
To remove a folder, we can use the -r
option with rm
. The -r
option will delete recursively (similar logic to cp -r
). Let's go back up one folder for this exercise, use ".." to denote one folder up.
cd ..
Let's remove lesson4_copy. We will include -i
in the rm
command below.
rm -ir lesson4_copy
Type yes when prompted and the list the contents of the unix_on_biowulf_2023 folder.
Note that because lesson4_copy is not empty, we will be asked whether we want to descend into the folder and then confirm to delete everything. We will say yes to all.
rm: descend into directory ‘lesson4_copy’? yes
rm: remove regular file ‘lesson4_copy/text_1_copy.txt’? yes
rm: remove regular file ‘lesson4_copy/counts_lesson4.csv’? yes
rm: remove directory ‘lesson4_copy’? yes
ls
Indeed, the folder lesson4_copy is gone.
counts.csv counts_lesson4.csv lesson4 new_house results.csv
text_1_copy.txt text_1.txt
To remove an empty directory, we can use rmdir
. For this exercise, let's make a new directory called empty_folder.
mkdir empty_folder
Listing the contents of unix_on_biowulf_2023, we see the empty_folder was added.
ls
counts.csv counts_lesson4.csv empty_folder lesson4 new_house
results.csv text_1_copy.txt text_1.txt
Now, we use rmdir
to delete the empty_folder.
rmdir empty_folder
Listing the contents of unix_on_biowulf_2023 again, we see that empty_folder is no longer there.
ls
counts.csv counts_lesson4.csv lesson4 new_house results.csv
text_1_copy.txt text_1.txt