Directory in linux means

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?

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 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.

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.

Steps to Follow:

❶ Open Terminal in the FF_2 folder.

❷ Type the following command in Terminal.

❸ 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.

Читайте также:  Epson linux drivers ubuntu

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

Steps to Follow:

❶ Let’s assume you have a file structure like the following:

folder structure to explain directory in linux

❷ Now, if you want to access the FF_1 folder, you can navigate step by step like the following:

You will get the Terminal inside the Desktop folder.

Now you’re inside the LinuxSimply folder.

You’re inside the Folder_1 folder.

Now you’re in the FF_1 folder.

cd command relative path

❸ 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.

Steps to Follow:

❶ Open Terminal in FF_1.

❷ To access the Previous/Parent Directory

To access the Previously/Last accessed Directory

❸ 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.

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 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.

Steps to Follow:

❶ Let’s start the Terminal in the FF_3 folder.

❷ Press the following command in the Terminal.

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

❸ Now type the following command.

❹ 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.

Steps to Follow:

❶ Let’s start the Terminal in the FF_3 folder.

❷ Type the following commands.

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

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

❸ 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 or between them.

3.3 -h Option

Steps to Follow:

Читайте также:  Linux лучший vnc server

❶ Open the Terminal in the FF_3 folder.

❷ Type the following commands to view the information in the human-readable format for easy understanding.

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

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

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

❺ 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.

Steps to Follow:

❶ Open the Terminal in the FF_3 folder.

❷ Type the following commands.

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

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

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

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.

❸ 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.

Steps to Follow:

❶ Open the Terminal in the FF_3 folder.

❷ Type the following commands to copy FILE_3.

❸ Type the following command to view the files inside the FF_3 folder

❹ Type the following command to view the files inside the FF_4 folder.

❺ 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.

Steps to Follow:

❶ Open the Terminal in the LinuxSimply folder.

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

❸ Type the following command to view the files inside the Folder_2.

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

❹ Now type the following command to view the files inside Folder_2.

❺ 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.

Steps to Follow:

❶ Open the LinuxSimply folder.

❷ Type the following command.

cp -rv Folder_2/FF_3 Folder_1/FF_3

❸ Press ENTER. You can clearly 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:

Let’s assume you want to move the FILE_2 file from the FF_1 folder to the FF_2 folder.

Steps to Follow:

❶ Open the LinuxSimply folder.

❷ You can type the following command.

mv Folder_1/FF_1/FILE_2 Folder_1/FF_2/FILE_2.

❸ Type the ls command to view the files inside.

❹ 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.

Steps to Follow:

❶ Open the Terminal in the LinuxSimply folder again.

❷ Type the ls command to view the file names before renaming.

❸ Type the following command to rename e file.

mv Folder_1/FF_1/FILE_2 Folder_1/FF_1/file

❹ Type the ls command again to view the file names.

❺ 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. But 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.

Similar Readings

  • What is Alias in Linux? [Creating, Removing & More]
  • An Overview of Relative Path in Linux [The Complete Guide]
  • An Overview of Absolute Path in Linux [The Complete Guide]
  • How to Archive in Linux Using TAR [Archiving, Extraction, Compression]

Источник

Оцените статью
Adblock
detector