Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 2.3 - Create, Copy, Move & Delete Directories

Creating, copying, moving, and deleting directories on Linux systems are fundamental operations for managing your file system. In this detailed guide, I'll walk you through these tasks, focusing on the key commands: mkdir, cp, mv, rmdir, and rm.

Whereas Windows operating systems are case insensitive, Linux operating system are case sensitive. Directory /tmp and /TMP are two distinct folders.

Creating Directories

The mkdir command is used to create directories. You can create a directory with a single command, and you can create nested directories by specifying the -p option.

To create a single directory:

mkdir directory_name

To create nested directories:

mkdir -p parent_directory/child_directory

Copying Directories

The cp command is used to copy files and directories. To copy a directory and its contents, you can use the -r (recursive) option.

To copy a directory and its contents to another location:

cp -r source_directory destination_directory

Moving Directories

The mv command is used to move files and directories. You can use it to rename directories or move them to a different location within the file system.

To rename a directory:

mv old_directory_name new_directory_name

To move a directory to a different location:

mv directory_name /new/location/

Deleting Empty Directories

The rmdir command is used to remove empty directories. It is important to note that rmdir can only delete directories that contain no files or subdirectories.

To delete an empty directory:

rmdir directory_name

Deleting a Directory with Contents

The rm command is used to remove files and directories.

To delete a directory and its contents, you can use the -r (recursive) option.

rm -r directory_name

To forcefully delete a directory without prompting for confirmation the -f option can be added:

rm -rf directory_name

Exercise

In the following exercise you will create, copy & move directories before tidying up by deleting directories:

Exercise: Create, copy, move & delete directories:

The -v option is used for many commands to give verbose output. This will help in confirming the commands run were successful.

Ensure you are in your users home folder:

cd

Create a directory called lpi_directory:

mkdir -v lpi_directory

Change into that directory:

cd lpi_directory

Create a parent and child directory using the -p (parent) option:

mkdir -vp dir1/sub_dir1

Create a copy of sub_dir1 called sub_dir2:

cp -vr dir1/sub_dir1 dir1/sub_dir2

Move sub_dir2 up one directory so it lives under the lpi_directory folder:

mv -v dir1/sub_dir2 ./

The output states the folder was renamed (moved) to the current directory (./).

As the directory is no longer a sub directory, rename it to dir2:

mv -v sub_dir2 dir2

Remove empty directory dir2

rmdir -v dir2

Remove directory dir1 that contains data (in this case a sub folder):

rm -rfv dir1

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.