Home

Working with the File System

Overview

I worked on creating and manipulating files and directories in a Linux environment. My objectives were to:

Task 2: Creating a Folder Structure

I needed to recreate a specific folder structure on my Linux machine. The required structure was:

/home/ec2-user/CompanyA/
/home/ec2-user/CompanyA/Finance/
/home/ec2-user/CompanyA/Finance/ProfitAndLossStatements.csv
/home/ec2-user/CompanyA/Finance/Salary.csv
/home/ec2-user/CompanyA/HR/
/home/ec2-user/CompanyA/HR/Assessments.csvv
/home/ec2-user/CompanyA/HR/TrialPeriod.csv
/home/ec2-user/CompanyA/Management/
/home/ec2-user/CompanyA/Management/Managers.csv
/home/ec2-user/CompanyA/Management/Schedule.csv

My Step-by-Step Process:

Step 1: First, I made sure I was in the correct home folder by checking my current location.

pwd

Since I needed to be in the home folder, I used:

cd /home/ec2-user

Step 2: I created the top-level CompanyA folder:

mkdir CompanyA

Step 3: I moved into the CompanyA folder:

cd CompanyA

Step 4: I created all the required subdirectories at once:

mkdir Finance HR Management

Step 5: I verified the folders were created correctly:

ls

This showed me:

Finance HR Management

Step 6: I moved into the HR directory to create files there:

cd HR

Step 7: I created the empty files inside the HR folder:

touch Assessments.csv TrialPeriod.csv

Step 8: I verified the files were created:

ls

This showed me:

Assessments.cvs TrialPeriod.csv

Step 9: I changed my directory to Finance:

cd ../Finance

Step 10: I created the empty files inside the Finance folder:

touch Salary.csv ProfitAndLossStatements.csv

Step 11: I verified the files were created:

ls

This showed me:

Salary.csv ProfitAndLossStatements.csv

Step 12: I went back to the CompanyA folder:

cd ..

Step 13: I created the files in the Management folder without changing directories:

touch Management/Managers.csv Management/Schedule.csv

Step 14: I verified the files were created:

ls Management

This showed me:

Managers.csv Schedule.csv

In the previous steps, I used the touch and ls command two ways:
Directly in the working folder: The ls command lists the current directory, and touch myFile.csv creates myFiles.csv in the current directory.
By using a path relative to the current folder: ls Management or touch Management/myFile.csv work in the Management folder inside the current folder.
cd ../ navigates back to the parent folder and touch ../Management/myFile.csv creates the myFile.csv file in the Management folder located in the parent folder of the current folder.

Step 15: I validated that all the files and folders were created correctly:

ls -laR

This showed the complete structure:

.:
total 0
drwxr-xr-x 5 ec2-user root 49 Aug 10 13:36 .
drwx------ 4 ec2-user ec2-user 90 Aug 10 13:25 ..
drwxrwxr-x 2 ec2-user ec2-user 59 Aug 10 13:39 Finance
drwxrwxr-x 2 ec2-user ec2-user 52 Aug 10 13:37 HR
drwxrwxr-x 2 ec2-user ec2-user 46 Aug 10 13:39 Management

./Finance:
total 0
drwxrwxr-x 2 ec2-user ec2-user 59 Aug 10 13:39 .
drwxr-xr-x 5 ec2-user root 49 Aug 10 13:36 ..
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 10 13:39 ProfitAndLossStatements.csv
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 10 13:39 Salary.csv

./HR:
total 0
drwxrwxr-x 2 ec2-user ec2-user 52 Aug 10 13:37 .
drwxr-xr-x 5 ec2-user root 49 Aug 10 13:36 ..
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 10 13:37 Assessments.cvs
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 10 13:37 TrialPeriod.csv

./Management:
total 0
drwxrwxr-x 2 ec2-user ec2-user 46 Aug 10 13:39 .
drwxr-xr-x 5 ec2-user root 49 Aug 10 13:36 ..
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 10 13:39 Managers.csv
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 10 13:39 Schedule.csv

Task 3: Delete and reorganize folders

A few weeks later, I was tasked with reorganizing the content as follows:

My Step-by-Step Process:

Step 1: I made sure I was in the correct directory:

pwd

This confirmed I was in the right location:

/home/ec2-user/CompanyA

Step 2: I copied the Finance folder and its content to the HR folder:

cp -r Finance HR

Step 3: I verified that the folder and content were copied correctly:

ls HR/Finance

This showed me:

ProfitAndLossStatements.csv Salary.csv

Step 4: I attempted to remove the Finance folder:

rmdir Finance

But got this error:

rmdir: failed to remove 'Finance/': Directory not empty

rmdir works only on an empty directory.

To remove the folder, I had two options:

Step 5: I chose to remove the files first:

rm Finance/ProfitAndLossStatements.csv Finance/Salary.csv

Step 6: I verified that the folder was empty:

ls Finance

This showed an empty result, confirming the folder was empty.

Step 7: I removed the now-empty Finance folder:

rmdir Finance

Step 8: I verified that the folder was removed:

ls

This showed me:

HR Management

Step 9: I moved the Management folder inside the HR folder:

mv Management HR

Step 10: I verified that the folder and files were moved correctly:

ls . HR/Management

This showed me:

.:
HR

HR/Management:
Managers.csv Schedule.csv

Step 11: I navigated into the HR folder:

cd HR

Step 12: I created the Employees folder:

mkdir Employees

Step 13: I moved the files to the Employees folder:

mv Assessments.csv TrialPeriod.csv Employees

Step 14: I verified that the files were moved correctly:

ls . Employees

This showed me:

.:
Employees Finance Management

Employees/:
Assessments.csv TrialPeriod.csv

Related Topics