Table of contents
- Tasks
- Create 'Documents' Directory and Files:
- Create 'Downloads' Directory:
- Create 'Pictures' Directory:
- Create 'Projects' Directory and 'project1' Subdirectory:
- Create 'subfile1.txt' and 'subfile2.txt' in 'project1':
- Create 'Videos' Directory:
- Display the current working directory:
- Create a specified number of directories:
- Create a specified number of files:
- Add data to a file:
- Redirect output to a file:
- Redirect errors to a file:
- Copy directory and files:
- Remove directory and files:
- Rename directory and files:
- Move directory and files:
- Solution
- Create 'Documents' Directory and Files:
- Create 'Downloads' Directory:
- Create 'Pictures' Directory:
- Create 'Projects' Directory and 'project1' Subdirectory:
- Create 'subfile1.txt' and 'subfile2.txt' in 'project1':
- Create 'Videos' Directory:
- Display the current working directory:
- Create a specified number of directories:
- Create a specified number of files:
- Add data to a file:
- Redirect output to a file:
- Redirect errors to a file:
- Copy directory and files:
- Remove directory and files:
- Rename directory and files:
- Move directory and files:
In this section, we'll cover essential file and directory operations. We'll create, manage, and manipulate directories and files using command-line instructions. Learn to organize data efficiently and handle tasks like copying, renaming, and more. Let's get started!
Tasks
Create 'Documents' Directory and Files:
Inside the current user directory, create a subdirectory called 'Documents.'
Inside the 'Documents' directory, create two text files named 'report1.txt' and 'report2.txt.'
Create 'Downloads' Directory:
Inside the current user directory, create a subdirectory called 'Downloads.'
Create 'Pictures' Directory:
Inside the current user directory, create a subdirectory called 'Pictures.'
Create 'Projects' Directory and 'project1' Subdirectory:
Inside the current user directory, create a subdirectory called 'Projects.'
Inside the 'Projects' directory, create a subdirectory called 'project1.'
Create 'subfile1.txt' and 'subfile2.txt' in 'project1':
Inside the 'project1' directory, create two text files named 'subfile1.txt' and 'subfile2.txt.'
Create 'Videos' Directory:
Inside the current user directory, create a subdirectory called 'Videos.'
Display the current working directory:
Print the current working directory to the console.
Create a specified number of directories:
Create ten directories with names "Archive_1," "Archive_2," ..., "Archive_10" within the "Documents" directory, ensuring unique names.
Create a specified number of files:
Create five text files with names "Report_1.txt," "Report_2.txt," ..., "Report_5.txt" within the "Documents" directory, ensuring unique names.
Add data to a file:
Append some text content to "report1.txt" or any other file you created earlier.
Redirect output to a file:
Execute a command (e.g., "ls -al") and redirect its output to a file named "directory_listing.txt" in the "Projects" directory.
Redirect errors to a file:
Execute a command that generates an error (e.g., "ls temp") and redirect the error message to a file called "error_log.txt" in the "Projects" directory.
Copy directory and files:
Duplicate the entire "Projects" directory along with its contents and name the copy "Projects_Backup."
Remove directory and files:
Delete the directory "Videos" and all its contents, as well as the file "Report_5.txt" from the "Documents" directory.
Rename directory and files:
Rename "Archive_1" to "Archive_new" and "report2.txt" to "FinalReport.txt" in the "Documents" directory.
Move directory and files:
Move "Archive_new" into the "Pictures" directory and move "FinalReport.txt" into the "Projects/project1" directory.
Solution
Create 'Documents' Directory and Files:
ubuntu@~$ :mkdir Documents ubuntu@~$ :ls Documents ubuntu@~$ :cd Documents/ ubuntu@~/Documents$ :touch report1.txt report2.txt ubuntu@~/Documents$ :ls report1.txt report2.txt
Create 'Downloads' Directory:
ubuntu@~/Documents$ :cd ~ ubuntu@~$ :mkdir Downloads ubuntu@~$ :ls Documents Downloads
Create 'Pictures' Directory:
ubuntu@~$: mkdir Pictures ubuntu@~$: ls Documents Downloads Pictures
Create 'Projects' Directory and 'project1' Subdirectory:
ubuntu@~$: mkdir Projects ubuntu@~$: ls Documents Downloads Pictures Projects ubuntu@~$: mkdir Projects/project1 ubuntu@~$: cd Projects/ ubuntu@~/Projects$: ls project1
Create 'subfile1.txt' and 'subfile2.txt' in 'project1':
ubuntu@~/Projects$: cd project1/ ubuntu@~/Projects/project1$: touch subfile1.txt subfile2.txt ubuntu@~/Projects/project1$: ls subfile1.txt subfile2.txt
Create 'Videos' Directory:
ubuntu@~/Projects/project1$: cd ~ ubuntu@~$: mkdir Videos ubuntu@~$: ls Documents Downloads Pictures Projects Videos
Display the current working directory:
ubuntu@~$: pwd /home/ubuntu
Create a specified number of directories:
ubuntu@~$: cd Documents/ ubuntu@~/Documents$: mkdir Archive_{1..10} ubuntu@~/Documents$: ls Archive_1 Archive_10 Archive_2 Archive_3 Archive_4 Archive_5 Archive_6 Archive_7 Archive_8 Archive_9 report1.txt report2.txt
Create a specified number of files:
ubuntu@~/Documents$: touch Report_{1..5}.txt ubuntu@~/Documents$: ls Archive_1 Archive_10 Archive_2 Archive_3 Archive_4 Archive_5 Archive_6 Archive_7 Archive_8 Archive_9 Report_1.txt Report_2.txt Report_3.txt Report_4.txt Report_5.txt report1.txt report2.txt
Add data to a file:
ubuntu@~/Documents$: echo "This is report1.txt file" > report1.txt ubuntu@~/Documents$: cat report1.txt This is report1.txt file
Redirect output to a file:
ubuntu@~/Projects$: ls -al > directory_listings.txt ubuntu@~/Projects$: cat directory_listings.txt total 12 drwxrwxr-x 3 ubuntu ubuntu 4096 Jul 26 13:54 . drwxr-x--- 9 ubuntu ubuntu 4096 Jul 26 13:43 .. -rw-rw-r-- 1 ubuntu ubuntu 0 Jul 26 13:54 directory_listings.txt drwxrwxr-x 2 ubuntu ubuntu 4096 Jul 26 13:41 project1
Redirect errors to a file:
ubuntu@~/Projects$: ls temp 2> error_log.txt ubuntu@~/Projects$: cat error_log.txt ls: cannot access 'temp': No such file or directory
Copy directory and files:
ubuntu@~/Projects$: cp ~/Projects/ ~/Projects_Backup -r ubuntu@~/Projects$: cd ubuntu@~$: ls Documents Downloads Pictures Projects Projects_Backup Videos
Remove directory and files:
ubuntu@~$: ls Documents Downloads Pictures Projects Projects_Backup Videos ubuntu@~$: rm Videos -r ubuntu@~$: ls Documents Downloads Pictures Projects Projects_Backup ubuntu@~$: rm Documents/Report_5.txt ubuntu@~$: ls Documents/ Archive_1 Archive_10 Archive_2 Archive_3 Archive_4 Archive_5 Archive_6 Archive_7 Archive_8 Archive_9 Report_1.txt Report_2.txt Report_3.txt Report_4.txt report1.txt report2.txt
Rename directory and files:
ubuntu@~$: mv Documents/Archive_1 Documents/Archive_new ubuntu@~$: ls Documents/ Archive_10 Archive_2 Archive_3 Archive_4 Archive_5 Archive_6 Archive_7 Archive_8 Archive_9 Archive_new Report_1.txt Report_2.txt Report_3.txt Report_4.txt report1.txt report2.txt ubuntu@~$: mv Documents/report2.txt Documents/FinalReport.txt ubuntu@~$: ls Documents/ Archive_10 Archive_2 Archive_3 Archive_4 Archive_5 Archive_6 Archive_7 Archive_8 Archive_9 Archive_new FinalReport.txt Report_1.txt Report_2.txt Report_3.txt Report_4.txt report1.txt
Move directory and files:
ubuntu@~$: mv Projects/project1/Archive_new/ Documents/ ubuntu@~$: mv Documents/Archive_new/ Pictures/ ubuntu@~$: ls Pictures/ Archive_new ubuntu@~$: ls Documents/ Archive_10 Archive_2 Archive_3 Archive_4 Archive_5 Archive_6 Archive_7 Archive_8 Archive_9 FinalReport.txt Report_1.txt Report_2.txt Report_3.txt Report_4.txt report1.txt ubuntu@~$: mv Documents/FinalReport.txt Projects/project1/ ubuntu@~$: ls Documents/ Archive_10 Archive_2 Archive_3 Archive_4 Archive_5 Archive_6 Archive_7 Archive_8 Archive_9 Report_1.txt Report_2.txt Report_3.txt Report_4.txt report1.txt
"๐ฑ Keep learning, and spread the knowledge to inspire others. ๐๐ก"