File and directory management

Use the CLI to manage files and directories.

By the end of the lab, you should be able to navigate the Linux file system, manage files and directories, manipulate text files, understand basic file permissions, and utilize process management commands.

Part 2: File and Directory Management

Reminder: All file system names a case-sensitive.

Now, let’s practice adding and removing files and directories using the CLI.

Creating and Removing Directories

  • mkdir - Make Directory
  • rmdir - Remove Directory
  • rm -r - Remove Directory and its contents recursively. WARNING: This is going to delete the directory and everything below it recursively. Linux does not have ‘undelete’, so be very careful with this command!

The commands below have a # character, which indicated the beginning of a comment. # comments are there for clarification and you do not type them.

bash

cd   # switch to your home directory
mkdir MyLab
ls   # You should see the new MyLab/ directory.
cd MyLab
ls   # You will not see anything. The directory is empty.
cd ..
rm -r MyLab
ls   # MyLab should now be gone

Creating, Copying, and Deleting Files

  • touch - Create an Empty File
  • cp - Copy Files and Directories
  • rm - Remove Files
  • mv - Move or Rename Files
bash

cd  # go to your home directory
ls
touch sample.txt
ls
cp sample.txt sample_copy.txt
ls
mv sample.txt renamed_sample.txt
ls
rm sample_copy.txt
ls

Exercise

  1. Create a new directory named LabDirectory
  2. Navigate into this directory using the cd command
  3. Create a new file named LabFile.txt inside this directory. Use touch
  4. Copy this file to a new file named LabFileCopy.txt. Use cp
  5. Delete LabFileCopy.txt. Use rm

Next

Move on to Text files.

Last modified January 21, 2025.