Directory Command in Linux Cheat Sheet

Learn via video courses
Topics Covered

Overview

When it comes to navigating and managing files on a Linux system, understanding directory commands is essential. Directories help organize and access files, similar to how folders work in a graphical user interface. In this article, we will explore various directory command in Linux that allow users to create, move, copy, delete, and manipulate directories.

This cheat sheet is a convenient reference to assist you in mastering directory commands.

Introduction

Paths in Linux represent directories and function-like addresses that specify the location of a file or directory within the file system. Before we delve into the world of directory command in Linux, it is essential to understand the concepts of absolute and relative paths.

Absolute Path An absolute path is a complete path that starts from the root directory / and includes all intermediate directories leading up to the target directory or file. For example, /home/user/Documents is an absolute path that specifies the location of the Documents directory starting from the root directory.

Relative Path A relative path specifies the location of a file or directory relative to the current working directory. It does not start from the root directory but relies on the current directory as the reference point. For example, if the current working directory is /home/user, the relative path Documents refers to the Documents directory within the current working directory.

Directory Command Cheat Sheet

In this section, we will explore each command in detail, including the available flags/options, descriptions, and examples.

mkdir: Create Directories

The mkdir directory command in Linux allows you to create directories. It takes the directory name as an argument and creates it in the current working directory.

Flags/Options:

  • -p (parents): Create directories, including parent directories if needed.

Example: To create a directory called my_directory in the current working directory, including any parent directories if they don't exist, execute the following command:

mkdir

cd: Change Directory

The cd command changes the current working directory. It allows you to navigate to a different directory in the file system. By default, if no additional arguments are provided, cd takes you to the user's home directory. You can use specific options to navigate to the previous directory or move to the parent directory.

Flags/Options:

  • - (hyphen): Change to the previous directory.
  • ~ (tilde): Change to the home directory.
  • .. (double dot): Change to the parent directory.

Example: To change to a directory named scaler_topcics/Blogs, use the following command:

cd

To change to the previous directory, use the following command:

CD-2

To change to the home directory, execute the following directory command in Linux terminal:

CD-3

To change to the parent directory, use the following command:

CD_DOUBLE_DOT

ls: List Directories and Files

The ls directory command in Linux lists the files and directories in a given directory. It provides information such as permissions, ownership, size, and modification time. The default behavior of ls is to list non-hidden files and directories in a simple format.

Flags/Options:

  • -l (long format): Display detailed information about the files, including permissions, ownership, size, and modification time.
  • -a (all): Include hidden files and directories in the listing.
  • -h (human-readable): Display file sizes in a human-readable format, such as K for kilobytes and M for megabytes.

Example: To list the contents of the current directory, use the following command:

LS-comm

To list the contents in long format, including permissions, ownership, size, and modification time, run the following directory command in Linux:

LS_L

To include hidden files and directories in the listing, use the following command:

LS_A

To display file sizes in a human-readable format, use the -h along with the -l flag. Execute the following command:

LS_LH

You can also list the details of a specific file or directory by providing its absolute or relative path as an argument to the ls command. For example, to view the details of a file named Blog_1.md having relative path scaler_topics/Blogs/Blog_1.md, you would use the following command:

LS_LH_2

By combining the available flags and providing specific paths, you can customize the ls command to display the desired details for individual files or directories.

pwd: Print Working Directory

The pwd command prints the absolute path of the current working directory. It can be helpful when you need to know the exact location of the directory you are working in. The output is the full path starting from the root directory.

Example: To print the absolute path of the current working directory, execute the following directory command in Linux terminal:

PWD

cp: Copy Directories

The cp command allows you to copy directories from one location to another. It creates a duplicate copy of the directory, preserving its contents.

Flags/Options:

  • -r (recursive): Copy directories and their contents recursively, including all files and subdirectories. It preserves the directory structure and copies all files and subdirectories within the specified directory. Use it when you want to duplicate an entire directory and its contents.
  • -a (archive): It stands for "archive mode" and is often used when you want to create an exact replica of a file or directory, including its permissions, timestamps, and other attributes.

Example: To copy a directory named source_directory to a destination directory, use the following command:

CP_R

To preserve file attributes while copying, use the following command:

CP_A

mv: Move/Rename Directories

The mv directory command in Linux is versatile and can be used for both moving and renaming directories. When used for moving directories, it effectively changes the location of the directory within the file system. When used for renaming directories, mv changes the name of the directory while keeping it in the same location.

Example: To move a directory named old_directory to a new location or rename it, use the following command:

 MV

rm: Remove Directories

The rm directory command in Linux is used to remove directories. It is important to note that removing a directory with rm is irreversible, and all files and subdirectories within the directory will be permanently deleted. T

Flags/Options:

  • -r (recursive): Remove directories and their contents recursively.
  • -f (force): Force removal without prompting for confirmation.

Example: To remove a directory named GO_PROJECT and its contents, use the following command:

IMAGE_15RM_R

To force the removal of the directory named topics without confirmation, use the following command:

RM_F

rmdir: Remove Empty Directories

The rmdir directory command in Linux is used to remove empty directories. It is specifically designed to delete directories that have no files or subdirectories.

Example: To remove an empty directory named empty_directory, use the following command:

RMDIR

Note: When encountering a new command or needing a reminder of command usage, try command --help first to access the command's documentation. Using the --help flag is a convenient way to access command documentation and gain a better understanding of how to use the command effectively. For example,

HELP

To make it easier for you to reference the different directory command in Linux, here is a table summarizing the commands, flags, descriptions, and examples:

CommandFlags/OptionsDescriptionExample
mkdir-pCreate directories, including parent directories if neededmkdir -p my_directory
cdChange directorycd my_directory
-Change to the previous directorycd -
~Change to the home directorycd ~
..Change to the parent directorycd ..
lsList directories and filesls
-lLong format; display detailed information about the filesls -l
-aInclude hidden files and directoriesls -a
-hHuman-readable file sizesls -h
pwdPrint working directorypwd
cp-rCopy directoriescp -r source_directory destination_directory
-aPreserve file attributescp -a source_directory destination_directory
mvMove or rename directoriesmv old_directory new_directory
rm-rRemove directoriesrm -r my_directory
-fForce removalrm -r -f my_directory
rmdirRemove empty directoriesrmdir empty_directory

Remember to refer back to this cheat sheet whenever you need a quick reminder or guidance.

Conclusion

Directories are the building blocks of file organization in Linux. With the directory commands provided in this article, you now have a powerful set of tools at your disposal to create, move, copy, and delete directories. By mastering these directory command in Linux, you can streamline your file management tasks and enhance your overall productivity.