Linux: File and Directory Manipulation

Linux: File and Directory Manipulation

ยท

4 min read

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

  1. 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.'

  2. Create 'Downloads' Directory:

    Inside the current user directory, create a subdirectory called 'Downloads.'

  3. Create 'Pictures' Directory:

    Inside the current user directory, create a subdirectory called 'Pictures.'

  4. 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.'

  5. Create 'subfile1.txt' and 'subfile2.txt' in 'project1':

    Inside the 'project1' directory, create two text files named 'subfile1.txt' and 'subfile2.txt.'

  6. Create 'Videos' Directory:

    Inside the current user directory, create a subdirectory called 'Videos.'

  7. Display the current working directory:

    Print the current working directory to the console.

  8. Create a specified number of directories:

    Create ten directories with names "Archive_1," "Archive_2," ..., "Archive_10" within the "Documents" directory, ensuring unique names.

  9. 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.

  10. Add data to a file:

    Append some text content to "report1.txt" or any other file you created earlier.

  11. 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.

  12. 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.

  13. Copy directory and files:

    Duplicate the entire "Projects" directory along with its contents and name the copy "Projects_Backup."

  14. Remove directory and files:

    Delete the directory "Videos" and all its contents, as well as the file "Report_5.txt" from the "Documents" directory.

  15. Rename directory and files:

    Rename "Archive_1" to "Archive_new" and "report2.txt" to "FinalReport.txt" in the "Documents" directory.

  16. Move directory and files:

    Move "Archive_new" into the "Pictures" directory and move "FinalReport.txt" into the "Projects/project1" directory.


Solution

  1. 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
    
  2. Create 'Downloads' Directory:

     ubuntu@~/Documents$ :cd ~
     ubuntu@~$ :mkdir Downloads
     ubuntu@~$ :ls
     Documents Downloads
    
  3. Create 'Pictures' Directory:

     ubuntu@~$: mkdir Pictures
     ubuntu@~$: ls
     Documents  Downloads  Pictures
    
  4. 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
    
  5. 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
    
  6. Create 'Videos' Directory:

     ubuntu@~/Projects/project1$: cd ~
     ubuntu@~$: mkdir Videos
     ubuntu@~$: ls
     Documents  Downloads  Pictures  Projects  Videos
    
  7. Display the current working directory:

     ubuntu@~$: pwd
     /home/ubuntu
    
  8. 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
    
  9. 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
    
  10. 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
    
  11. 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
    
  12. 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
    
  13. Copy directory and files:

    ubuntu@~/Projects$: cp ~/Projects/ ~/Projects_Backup -r
    ubuntu@~/Projects$: cd
    ubuntu@~$: ls
    Documents  Downloads  Pictures  Projects  Projects_Backup  Videos
    
  14. 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
    
  15. 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
    
  16. 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
    

    Go back to the main page

"๐ŸŒฑ Keep learning, and spread the knowledge to inspire others. ๐Ÿš€๐Ÿ’ก"

ย