Working with directories in Linux is an essential part of managing files and organizing your file system. Directories, also known as folders, provide a hierarchical structure for storing and organizing files. Working with directories in Linux involves creating, navigating, listing, renaming, moving, copying, deleting, and changing permissions of directories.
Top commands in Directory in Linux
If you wish to learn on “Working with Directories in Linux” then you are at the correct page. In this section we will discuss on these below commands along with its example.
- pwd
- ls
- mkdir
- cd
- rmdir
1. pwd command
The ‘pwd’ command stands for print working directory’ or ‘present working directory’. This command is used to display the current working directory where the user is located over the system. In the below screenshot, my current working directory is ‘azureuser’ which is located under ‘home’ directory
We can also create ‘Nested directories’ by just mentioning ‘-p’ command which stands for ‘Parent directory’. Nested directories means we can create a directory inside a directory while working in a different directory.
In the below screenshot, my current location is ‘azureuser’ and want to create a directory inside ‘testdir2’ without navigating to this directory.
2. ls command
This command is used to see all the contents available in a working directory. Here, working directory means the directory location where we are working on. In Linux, directory are same as folder in Windows Operating System. In the attached screenshot, we can see there are 8 directories named as dir1, dir2…… dir8 and 8 files as well which are named as testfile1, testfile2…….. testfile8.
The ‘ls -a’ command in Linux is used to list all the files and directories available in the directory as well as hidden files and directories. By default, the ‘ls’ command does not shows the hidden files and directories. These hidden files or directories names start with a dot (.) Here is an example of ‘ls -a’ command. All the files and directories starts with dot are not visible while we executing ‘ls’ command.
The ‘ls -l’ command is used to see the output as long listing format. It provides detailed information about a file or directories such as total numbers for files or directories, permissions level, last modification time, size etc. In this example, we can see permissions level, size or date and time when it was last modified.
This command help us in getting the files and directories detailed information in human readable form.
Note: We can also use ‘ls -l -h’ command instead of ‘ls -lh’.
In command ‘ls -lr’, ‘l’ stands for long listing format and ‘r’ for reversing the order of the listing. This command is used to list files and directories in reverse order. The output is same as ‘ls -l’ command above.
3. mkdir command
The ‘mkdir’ command stands for ‘make directory’. This command is used to create a new directory in Linux or Unix based Operating System. In the below example, we tried creating a directory named as ‘testdir1’ by using ‘mkdir’ command and can see its created.
4. rmdir command
The command ‘rmdir’ stands for ‘remove directory’. This command is used to remove the empty files or directory from the system. In this example, we removed ‘dir8’ directory and got removed because this was empty.
This command is used to delete a directory which contains any nested directory or files. We need to put ‘-p’ while trying to delete a directory.
In the below screenshot, we tried deleting a directory ‘testdir1’ which containing 4 another directory by using ‘rmdir’ command and we got an error message stating that “Directory not empty”. Then we use ‘rm -r’ command to delete the same file and was able to delete it successfully.
The command ‘cd’ stands for ‘change directory’ which help us in changing the directory within the Linux Operating System. In the below screenshot we can see, my ‘present working directory’ was showing as ‘/home/azure/user’ and then I used ‘cd’ command to change the directory which is now ‘testdir1’.
5. cd command
This command is used to go to the ‘home directory’. This command is useful in returning to the home directory with the help of a single command. The symbol ‘~’ is read as ’tilde’. In the below screenshot, we can see my current directory was ‘/home/azureuser/testdir1/testchild1/testsubchild2’ and then I used ‘cd ~’ command and was return to the ‘home directory’ directly i.e., ‘/home/azureuser’.
Note: We can also use ‘cd’ command only to return to the home directory.
The command ‘cd ..’ is used to return to the parent directory which means one directory above from the directory where you are working on. In the below screenshot, we can see the current working directory is showing as ‘/home/azureuser/testdir1/testchild1/testsubchild2’ and then I used ‘cd ..’ command and was able to return to one directory from where I was working on i.e ‘/home/azureuser/testdir1/testchild1’
Note: There is a space between ‘cd’ and dot-dot ‘..’
This command is used to stay in the previous directory. Lets suppose, we had be in the same directory but somehow we came out of the directory and need to go back to the same directory then we use this command.
This is a shortcut key available in Linux. We can start typing any files or directories name upto 2-3 characters of that and then press ‘Tab’ key, it will show all possible suitable items. Lets suppose, in my Linux machine having files name as testfile1, testfile2, testfile3 etc.. When we will start typing name for this file and will type up to ‘test’ and will press ‘Tab’ key, it will be showing all three file.
Wrapping Up Working with Directories in Linux
So, in this section we covered “working with directories in Linux”. Directory is fundamental for organizing and managing files in the file system. The ability to create, navigate, list, rename, move, copy, and delete directories provides a powerful toolkit for efficient data organization. Changing permissions and searching for directories further enhances the control and accessibility of files. The hierarchical structure of directories allows for logical categorization and easy retrieval of files. By understanding and utilizing the various commands and operations available for working with directories, users can effectively organize their data, streamline workflows, and ensure efficient file management in the Linux environment. If you want to explore more on this topic, please click here.
If you wish to learn about File Contents in Linux, please follows the URL.