What is Directory in Linux [A Complete Overview]

Though quite similar to Windows, the directory in Linux takes a different objective of using Terminal. In our day-to-day life, we don’t use Terminal, but for professional use and automation, we need Terminal. In Linux, you should learn the use of Terminal to access directories, and in this article, you will specifically learn different commands and applications of directories in Linux.

What is a Directory in Linux?

In a directory, we store files and folders. Linux has a hierarchical file system, just like UNIX and MS-DOS. In Windows, you can see a folder system in a GUI where you can store a file. Linux also has a similar directory system. But before GUI, every computer used DOS (Disk Operating System), which had a Terminal to type commands.

Here you will learn to access directories using a Terminal. First, you need to know about some basic directory systems.

  • Root Directory
  • Home Directory

1. Root Directory

The root directory in Linux is the ROOT or the starting point of all directories. It is considered the topmost directory in the Linux system.

You can access the root directory using the following command. The command ls is used to view the files inside the root directory.

cd / 
 ls

Accessing Root Directory in Linux

2. Home Directory

The Home directory in Linux is the directory owned by a user. Every user in a Linux system owns a Home directory. There are many ways of accessing a Home directory.

2.1 Using ~ Symbol to Access Home Directory

You can use the ~ symbol, which indicates the Home directory symbol. You have to use the cd command to access the Home directory.

home directory in linux symbol

2.2 Using Absolute Path

Using the Absolute path is also another way of accessing the Home directory. This method is a bit longer and requires knowledge of the path of the directory system.

home directory in linux absolute path

Accessing a Directory in Linux Using Commands

To use the directory, you have to know about some special commands.

1. Viewing the Current Directory Using “pwd” Command

You can use the pwd (print working directory) command to print the Absolute path of the current directory. This way, you can keep track of your current directory without losing your way, even if the structure is quite complex.

To view the current directory using pwd command, follow the steps below:

  1. Open Terminal in the FF_2 folder.
  2. Type the following command in Terminal.
    pwd
  3. Press ENTER. You’ll find the Absolute path of the current folder.directory in linux pwd command

2. Navigating Directories Using “cd” Command

The cd (change directory) command will let you navigate between different directories. Using this command, we can access a directory from another one.

cd ~/Desktop/DIRECTORY_NAME

This command will let you inside/access the DIRECTORY_NAME folder in the Desktop folder.

  1. Let’s assume you have a file structure like the following:folder structure to explain directory in linux
  2. Now, if you want to access the FF_1 folder, you can navigate step by step like the following:

    cd Desktop/

    You will get the Terminal inside the Desktop folder.

    cd LinuxSimply/

    Now you’re inside the LinuxSimply folder.

    cd Folder_1

    You’re inside the Folder_1 folder.

    cd FF_1

    Now you’re in the FF_1 folder.

    cd command relative path

  3. Press ENTER at the end of every command.

    But you can also directly access the FF_1 folder using the Absolute Path by just typing the following command.

    cd ~/Desktop/LinuxSimply/Folder_1/FF_1

    cd command absolute path

There are also 2 other commands that you should know. Follow the step below on how you can navigate to the previous directory or the last accessed directory:

  1. Open Terminal in FF_1.
  2. To access the Previous/Parent Directory
    cd ..

    To access the Previously/Last accessed Directory

    cd -
  3. Press ENTER. The (..) argument will take you to the Previous/Parent directory. The () argument will let you access the Previously accessed directory.

    2 other cd command

3. Viewing File Information Using the “ls” Command

The ls (list) command is generally used to list the available files inside a directory, and their information can also be shown using different options. This command helps us understand what files or folders are inside a directory.

ls ~/Desktop/DIRECTORY_NAME

Let’s say you want to know about the files inside the FF_1 Folder. You can just type the following command and press ENTER.

ls

ls command

This command will show you the content inside the DIRECTORY_NAME folder. There are some options to show specific information about the DIRECTORY_NAME.

Here you will see the FF_3 folder to show different options available in ls.

Useful Options

3.1 -a Option

This option shows all the files or folders inside the current directory, including the hidden files.

To see how to see the list of files or folders inside the current directory with the hidden files, follow the steps below:

  1. Let’s start the Terminal in the FF_3 folder.
  2. Press the following command in the Terminal.
    ls

    You will only find the file names in the FF_3 directory.

  3. Now type the following command.
    ls -a
  4. Press ENTER.

    There’s also another folder named .Moby_Dick.txt. This hidden file was previously not shown using the ls command.

    ls -a command in directory

    Note: You will find some other folders here. The (.) folder indicates the current folder, while the (..) folder indicates the Previous/Parent folder. They are, in general, hidden by default.

3.2 -l Option

This option shows some more information about the current directory. It presents a long listing format, showing details such as file permissions, number of links, owner, group, file size, modification time, and the name of the file or directory.

  1. Let’s start the Terminal in the FF_3 folder.
  2. Type the following commands.
    ls -l

    You will find the files and folders inside the FF_3 directory.

    You can use both -l and -a options simultaneously, like -la.

    ls -la
  3. Press ENTER. You will see the files in a list view, including hidden files.ls -l and -la command for directory in linux
    Note: You can use multiple options without any SPACE between them and with a single .

3.3 -h Option

When used with ls -h, it makes file sizes more easily readable by using unit suffixes like KB, MB, or GB.

  1. Open the Terminal in the FF_3 folder.
  2. Type the following commands to view the information in the human-readable format for easy understanding.

    ls -h
  3. This command won’t be much different because there’s nothing to show in the human-readable format.

    ls -lh

    Here, we have the human-readable format. But the files are empty. So the sizes are shown as zero.

  4. Type the following command to view the file in the human-readable format, including the hidden files.

    ls -lah
  5. Press ENTER. As you can see, there’s a hidden file .Moby_Dick.txt, inside, and they are shown in a human-readable format.

    ls -h command in human readable format directory in llinux

3.4 -S Option

This option sorts the file’s content according to the files’ size inside the directory.

  1. Open the Terminal in the FF_3 folder.
  2. Type the following commands.
    ls -S

    This command will just sort the files by the file size but won’t show many details.

    ls -lS

    You can use this command to sort the files and arrange them in a list view.

    ls -laS

    This command will show all files, including hidden files, and sort them according to size in a list view.

    ls -laSr

    It is quite similar to the previous one, just in reverse order. Here, the -r option sorts the files according to size in reverse order.

  3. Press ENTER. You will find the result in the display.

    using ls -s command to sort directory in linux

    Note: -r command option indicates Reverse. You can use the -r option to sort in reverse, smaller to larger file order.

4. Copying a Directory in Linux Using the “cp” Command

The cp command is used to copy a file or directory. You must provide the SOURCE_PATH and the DESTINATION_PATH to use this command.

cp SOURCE_PATH DESTINATION_PATH

This command will copy a directory/file from the source directory to the destination directory.

Let’s say you want to copy FILE_3 from the FF_3 folder to the FF_4 folder.

  1. Open the Terminal in the FF_3 folder.
  2. Type the following commands to copy FILE_3.
    cp FILE_3 ../FF_4/FILE_3
  3. Type the following command to view the files inside the FF_3 folder
    ls
  4. Type the following command to view the files inside the FF_4 folder.
    ls ../FF_4/
  5. Press ENTER. You can see that FILE_3 has been copied to the FF_4 folder.copying directory in linux a file using cp command

Useful Options:

4.1 -r, -R, –recursive Option

Here, r stands for recursive. This option is used to copy the entire directory, including files or folders.

  1. Open the Terminal in the LinuxSimply folder.

  2. Type the following command to copy the whole FF_3 folder, including inside files from Folder_1, into the Folder_2.

    cp -r Folder_2/FF_3 Folder_1/FF_3
  3. Type the following command to view the files inside the Folder_2.
    ls Folder_2

    As you can see, there is the FF_3 folder inside Folder_2.

  4. Now type the following command to view the files inside Folder_1.
    ls Folder_1
  5. Press ENTER. You can see that the FF_3 folder has been copied from Folder_2 to Folder_1.

    copying directory in linux of a file using cp -r command

4.2 -v Option

This option shows extra information about the files copied. Generally, the path of the parent files and copied files are displayed.

Assume you want to copy the folder and view some information about the files.

  1. Open the LinuxSimply folder.
  2. Type the following command.
    cp -rv Folder_2/FF_3 Folder_1/FF_3
  3. Press ENTER. You can see that the details about the files moved are displayed.copying directory in linux a folder using cp -rv command

5. Moving a Directory Using the “mv” Command

The mv command is used to move a file into another directory. The command syntax is the following:

mv SOURCE DESTINATION

Let’s assume you want to move the FILE_2 file from the FF_1 folder to the FF_2 folder. Follow the steps given below to do so:

  1. Open the LinuxSimply folder.
  2. You can type the following command.
    mv Folder_1/FF_1/FILE_2 Folder_1/FF_2/FILE_2
  3. Type the ls command to view the files inside.
    ls Folder_1/FF_1
    ls Folder_1/FF_2
  4. Press ENTER. As you can see, FILE_2 has been moved from FF_1 to FF_2.moving directory in linux using mv command

6. Renaming a Directory in Linux Using the “mv” Command

The mv has another function. It’s renaming. You can use this command to rename a file in the same directory. But you can also move that file to another directory and rename it simultaneously. The command syntax for renaming is as follows:

mv SOURCE_PATH DESTINATION_PATH

Here SOURCE_PATH and DESTINATION_PATH should have different file names at the end. This way, the source file’s name will change to the destination file.

You can follow these steps to move a file.

  1. Open the Terminal in the LinuxSimply folder again.
  2. Type the ls command to view the file names before renaming.
  3. Type the following command to rename a file.
    mv Folder_1/FF_1/FILE_2 Folder_1/FF_1/file
  4. Type the ls command again to view the file names.
  5. Press ENTER. As you can see, The FILE_2 file has been renamed to file.mv command to move a file

Conclusion

In this article, you learned to use Terminal to access directories in Linux. They are all basic commands, and you will certainly find their use in professional use. You will use these methods and commands for developing programs or automation purposes. Yes, there are many options for each command, and they have different syntaxes. Fortunately, you don’t need to memorize them. You just need to get used to them and find the syntax using the man command, or you can come back here for a quick recall.

People Also Ask

What is a directory in Linux?

A directory is a type of file that serves as a container for other files and directories. It organizes the file system hierarchy, providing a structured way to store and retrieve data. Each directory is identified by a name and has its own path within the file system. The root directory, represented by a forward slash (/), is the top-level directory from which the entire file system originates.

How do I find a directory in Linux?

To find a directory in Linux, you can use the find command. The basic syntax for finding a directory is as follows:

find [starting_directory] -type d -name "directory_name"

What is mkdir in Linux?

The mkdir command is used to create directories or folders. The command stands for “make directory.” Its basic syntax is:

mkdir [directory_name]

You replace [directory_name] with the name you want to give to the new directory. For example, mkdir my_directory would create a directory named “my_directory” in the current working directory. Additionally, you can specify a path to create a directory in a specific location.

What is mv in Linux?

The mv command is used to move or rename files and directories. Its primary purpose is to transfer files or directories from one location to another or to rename them. The basic syntax is:

mv [source] [destination]

You replace [source] with the file or directory you want to move or rename, and [destination] with the target location or new name. If the destination is an existing directory, the source is moved into that directory. If the destination is a non-existent directory and ends with a new name, the source is moved and renamed.

For example, to move a file named “file.txt” from the current directory to another directory, you would use:

mv file.txt /path/to/destination/

To rename a file, you can use the mv command as well:

mv old_name.txt new_name.txt

What is ls command in Linux?

The ls command in Linux is used to list the files and directories in a directory. Its basic syntax is:

ls [options] [directory]

By default, when used without any options or directory specified, ls lists the files and directories in the current working directory. You can include various options to customize the output, such as -l for a detailed (long) listing, -a to show hidden files, or -h for human-readable file sizes.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Md. Rafsan Zani

Hello, I’m Md. Rafsan Zani. I have recently completed my Undergraduate from the Bangladesh University of Engineering and Technology (BUET). Currently, I’m pursuing higher studies abroad. I’m really interested in computer science and would like to learn a lot about the wonderful world of computers. Currently, I’m working as a Linux Content Developer Executive and find Linux really interesting. I certainly would like to learn more about Linux and implement them in my future studies. Read Full Bio

Leave a Comment