Optional Exercise (Linux Terminal Commands)
Gaurav Ojha
VisFac @ MPSTME, Mumbai
Exercise: Directory and File Management in Linux
1. Create a directory structure:
a) Create a directory called "MyFiles" in your home directory.
b) Inside the "MyFiles" directory, create three subdirectories: "Documents", "Pictures", and "Music".
2. Navigate through directories:
a) Change your current working directory to "MyFiles/Documents".
b) Print the absolute path of your current working directory using the "pwd" command.
3. List directory contents:
a) List all the files and directories in "MyFiles".
b) List all the files (including hidden files) and directories in "MyFiles/Pictures" using appropriate
options with the "ls" command.
4. Create and remove directories:
a) Create a new directory named "Notes" inside "MyFiles/Documents".
b) Remove the "Music" directory from "MyFiles".
5. Create and copy files:
a) Create a new empty file named "[Link]" inside the "Documents" directory using the "touch"
command.
b) Copy the "[Link]" file to the "Notes" directory.
6. View file contents:
a) View the first 5 lines of the "[Link]" file using the "head" command.
b) View the last 10 lines of the "[Link]" file using the "tail" command.
7. Append content to a file:
a) Add a new task to the "[Link]" file using the "echo" command and redirect the output to append
to the file.
8. Search for specific content:
a) Use the "grep" command to search for the word "important" in the "[Link]" file.
9. Move and rename files:
a) Move the "[Link]" file from the "Documents" directory to the "Notes" directory using the "mv"
command.
b) Rename the "[Link]" file in the "Notes" directory to "[Link]" using the "mv" command.
10. Clean up:
a) Remove the "MyFiles" directory and all its contents using the appropriate command.
1. Create a directory structure:
a) Create a directory called "MyFiles" in your home directory.
b) Inside the "MyFiles" directory, create three subdirectories: "Documents", "Pictures", and
"Music".
mkdir ~/MyFiles
mkdir ~/MyFiles/Documents
mkdir ~/MyFiles/Pictures
mkdir ~/MyFiles/Music
2. Navigate through directories:
a) Change your current working directory to "MyFiles/Documents".
b) Print the absolute path of your current working directory using the "pwd" command.
cd ~/MyFiles/Documents
pwd
3. List directory contents:
a) List all the files and directories in "MyFiles".
b) List all the files (including hidden files) and directories in "MyFiles/Pictures" using
appropriate options with the "ls" command.
ls ~/MyFiles
ls -a ~/MyFiles/Pictures
4. Create and remove directories:
a) Create a new directory named "Notes" inside "MyFiles/Documents".
b) Remove the "Music" directory from "MyFiles".
mkdir ~/MyFiles/Documents/Notes
rm -r ~/MyFiles/Music
5. Create and copy files:
a) Create a new empty file named "[Link]" inside the "Documents" directory using the "touch"
command.
b) Copy the "[Link]" file to the "Notes" directory.
touch ~/MyFiles/Documents/[Link]
cp ~/MyFiles/Documents/[Link] ~/MyFiles/Documents/Notes/[Link]
6. View file contents:
a) View the first 5 lines of the "[Link]" file using the "head" command.
b) View the last 10 lines of the "[Link]" file using the "tail" command.
nano ~/MyFiles/Documents/[Link]
# Add 10-20 lines
head -n 5 ~/MyFiles/Documents/[Link]
tail -n 10 ~/MyFiles/Documents/[Link]
7. Append content to a file:
a) Add a new task to the "[Link]" file using the "echo" command and redirect the output to
append
to the file.
echo "New task: Complete exercise 7" >> ~/MyFiles/Documents/[Link]
8. Search for specific content:
a) Use the "grep" command to search for the word "Gatsby" in the "[Link]" file.
grep "Gatsby" ~/MyFiles/Documents/[Link]
9. Move and rename files:
a) Move the "[Link]" file from the "Documents" directory to the "Notes" directory using the
"mv"
command.
b) Rename the "[Link]" file in the "Notes" directory to "[Link]" using the "mv" command.
mv ~/MyFiles/Documents/[Link] ~/MyFiles/Documents/Notes/[Link]
mv ~/MyFiles/Documents/Notes/[Link] ~/MyFiles/Documents/Notes/[Link]
10. Clean up:
a) Remove the "MyFiles" directory and all its contents using the appropriate command.
rm -r ~/MyFiles
Note: An even faster way would be to use: rm -rf ~/MyFiles