File and directory management

Use the CLI to manage files and directories.

You are getting the first edition of all these pages. Please let me know if you find an error!

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.

Type ls after each command below to see the changes:

bash

cd   # switch to your home directory
mkdir MyLab
cd MyLab
rmdir MyLab   # This should fail because the directory is not empty
cd ..
rm -r MyLab

Creating, Copying, and Deleting Files

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

Type ls after each command below to see the changes:

bash

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

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
Last modified August 29, 2024.