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 "todo.txt" inside the "Documents" directory using the "touch"
command.
b) Copy the "todo.txt" file to the "Notes" directory.
6. View file contents:
a) View the first 5 lines of the "todo.txt" file using the "head" command.
b) View the last 10 lines of the "todo.txt" file using the "tail" command.
7. Append content to a file:
a) Add a new task to the "todo.txt" 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 "todo.txt" file.
9. Move and rename files:
a) Move the "todo.txt" file from the "Documents" directory to the "Notes" directory using the "mv"
command.
b) Rename the "todo.txt" file in the "Notes" directory to "tasks.txt" 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 "todo.txt" inside the "Documents" directory using the "touch"
command.
b) Copy the "todo.txt" file to the "Notes" directory.
touch ~/MyFiles/Documents/todo.txt
cp ~/MyFiles/Documents/todo.txt ~/MyFiles/Documents/Notes/todo.txt
6. View file contents:
a) View the first 5 lines of the "todo.txt" file using the "head" command.
b) View the last 10 lines of the "todo.txt" file using the "tail" command.
nano ~/MyFiles/Documents/todo.txt
# Add 10-20 lines
head -n 5 ~/MyFiles/Documents/todo.txt
tail -n 10 ~/MyFiles/Documents/todo.txt
7. Append content to a file:
a) Add a new task to the "todo.txt" file using the "echo" command and redirect the output to
append
to the file.
echo "New task: Complete exercise 7" >> ~/MyFiles/Documents/todo.txt
8. Search for specific content:
a) Use the "grep" command to search for the word "Gatsby" in the "todo.txt" file.
grep "Gatsby" ~/MyFiles/Documents/todo.txt
9. Move and rename files:
a) Move the "todo.txt" file from the "Documents" directory to the "Notes" directory using the
"mv"
command.
b) Rename the "todo.txt" file in the "Notes" directory to "tasks.txt" using the "mv" command.
mv ~/MyFiles/Documents/todo.txt ~/MyFiles/Documents/Notes/todo.txt
mv ~/MyFiles/Documents/Notes/todo.txt ~/MyFiles/Documents/Notes/tasks.txt
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