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
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.
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.
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:
- Open Terminal in the FF_2 folder.
- Type the following command in Terminal.
pwd
- Press ENTER. You’ll find the Absolute path of the current folder.
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.
- Let’s assume you have a file structure like the following:
-
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.
-
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
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:
- Open Terminal in FF_1.
- To access the Previous/Parent Directory
cd ..
To access the Previously/Last accessed Directory
cd -
-
Press ENTER. The (..) argument will take you to the Previous/Parent directory. The (–) argument will let you access the Previously accessed directory.
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
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:
- Let’s start the Terminal in the FF_3 folder.
- Press the following command in the Terminal.
ls
You will only find the file names in the FF_3 directory.
- Now type the following command.
ls -a
- Press ENTER.
There’s also another folder named .Moby_Dick.txt. This hidden file was previously not shown using the ls command.
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.
- Let’s start the Terminal in the FF_3 folder.
- 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
- Press ENTER. You will see the files in a list view, including hidden files.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.
- Open the Terminal in the FF_3 folder.
-
Type the following commands to view the information in the human-readable format for easy understanding.
ls -h
-
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.
-
Type the following command to view the file in the human-readable format, including the hidden files.
ls -lah
-
Press ENTER. As you can see, there’s a hidden file .Moby_Dick.txt, inside, and they are shown in a human-readable format.
3.4 -S Option
This option sorts the file’s content according to the files’ size inside the directory.
- Open the Terminal in the FF_3 folder.
- 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.
-
Press ENTER. You will find the result in the display.
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.
- Open the Terminal in the FF_3 folder.
- Type the following commands to copy FILE_3.
cp FILE_3 ../FF_4/FILE_3
- Type the following command to view the files inside the FF_3 folder
ls
- Type the following command to view the files inside the FF_4 folder.
ls ../FF_4/
- Press ENTER. You can see that FILE_3 has been copied to the FF_4 folder.
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.
-
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.
cp -r Folder_2/FF_3 Folder_1/FF_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.
- Now type the following command to view the files inside Folder_1.
ls Folder_1
-
Press ENTER. You can see that the FF_3 folder has been copied from Folder_2 to Folder_1.
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.
- Open the LinuxSimply folder.
- Type the following command.
cp -rv Folder_2/FF_3 Folder_1/FF_3
- Press ENTER. You can see that the details about the files moved are displayed.
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:
- 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.
ls Folder_1/FF_1
ls Folder_1/FF_2
- Press ENTER. As you can see, FILE_2 has been moved from FF_1 to FF_2.
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.
- 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 a 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.
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
- 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]
FUNDAMENTALS A Complete Guide for Beginners