Chapter 6 Manipulating directories
6.1 Make a directory
To make a directory the command mkdir
is used.
For the below examples we will be making heavy use of ls
to visualise the contents of the directories.
Type in the following commands to make a subdirectory within the "CGR_Linux" directory called "Chicken" then make a subdirectory within "Chicken" called "Egg".
#Change directory to CGR_linux in your home (~)
cd ~/Linux/
#Always good to list contents when you move into a directory
ls
#Make a directory called Chicken in your current directory
mkdir Chicken
#Make a subdirectory of Chicken called Egg
mkdir Chicken/Egg
#List the contents of the Chicken directory
ls Chicken
Tip: You can use the up arrow key to get to previously run commands which you can edit using the right and left keys.
6.2 Moving Directories and Files
Files and directories can be moved with the mv
command. This can also be used to change the name of a file or a directory.
Note: If you move a file to the path of a file that already exists, the pre existing file will be overwritten.
First ensure your working directory is the "Linux" directory.
Move the "Chicken" directory into the "3_chicken_farm/3_1_hut/" directory. This will move the directory and all its contents.
#Before moving list the contents of the the destination directory
ls 3_chicken_farm/3_1_hut/
#Also list the contents of the current directory to ensure
#Chicken is present
ls
#Move Chicken to 3_chicken_farm/3_1_hut/
mv Chicken/ 3_chicken_farm/3_1_hut/
#List the current directory and destination
ls . 3_chicken_farm/3_1_hut/
Move the Directory "3_chicken_farm/3_1_hut/Chicken/" to "3_chicken_farm/3_2_field" and rename it "Outdoor_Chicken".
#List the contents of the start directory
ls ~/Linux/3_chicken_farm/3_1_hut
#Move the Chicken directory whilst renaming it
mv ~/Linux/3_chicken_farm/3_1_hut/Chicken/ \
~/Linux/3_chicken_farm/3_2_field/Outdoor_Chicken
#List the contents of the destination directory
ls ~/Linux/3_chicken_farm/3_2_field/Outdoor_Chicken
Move the file "3_chicken_farm/3_3_supplies/feed.txt" to the directory "3_chicken_farm/3_2_field" and rename it "used_feed.txt"
6.3 Copying Directories and Files
Files and directories can be copied with the cp
command. Directories and files can be copied to any directory and given a new name.
Note: If you copy a file to the path of a file that already exists, the pre existing file will be overwritten.
Before running the below commands move into your "~/Linux/3_chicken_farm/" directory.
Copy the file "3_1_hut/Laid_Egg.txt" to the "3_2_field" directory and give it the filename "Chick.txt"
#List contents of 3_1_hut
ls 3_1_hut
#Copy Laid_egg to another directory as a new file called Chick.txt
cp 3_1_hut/Laid_Egg.txt 3_2_field/Chick.txt
#List contents of 3_2_field
ls 3_2_field
Copy the file "3_2_field/Chick.txt" and give the copy the name "Chick_2.txt"
#List contents of 3_2_field
ls 3_2_field
#Copy the Chick.txt file as Chick_2.txt
cp 3_2_field/Chick.txt 3_2_field/Chick_2.txt
#List contents of 3_2_field
ls 3_2_field
Copy the directory "3_1_hut" to "3_1_hut_2"
6.4 Deleting Files and Directories
To delete files and directories the command rm
can be used.
Important: This command can be very dangerous. There is no recycle bin on Linux machines so once you delete something it cannot be recovered. Be very careful when deleting files and directories. To avoid major loss try to keep backups of important data and scripts.
Ensure you are in your "3_chicken_farm/" directory
Remove the file "3_2_field/Chick_2.txt"
#List contents of 3_2_field
ls 3_2_field
#Remove Chick_2.txt
rm 3_2_field/Chick_2.txt
#List contents of 3_2_field
ls 3_2_field
Remove the directory "3_1_hut_2/" and its contents
6.5 MCQs: Manipulating directories
Please attempt to answer the below Multiple-Choice Questions to reinforce what you have learnt in this chapter.
- What command removes files and directories?
- What command moves files and directories?
- What command makes a new directory?
- What option is needed for moving or removing a directory?
- What command list the contents of directories?