In this chapter we shall study about following commands:
2.1 Linux pwd
2.2 Linux cd
2.3 Linux Absolute Relative paths
2.4 Linux ls
2.5 Linux mkdir
2.6 Linux rmdir
2.1 Linux pwd
2.1.1 Usage:
“pwd” stands for Print Working Directory.
This command is used to print the path of present working directory. It will show the path starting from the “root” directory.
2.1.2 Options:
-L – Print the logical directory path, even if it is a symbolic link. This will be the default option, if no options are mentioned.
-P – If the directory is a symbolic link, then resolve that link and print the resolved path.
2.1.3 Examples:
First we create a symbolic link to a directory and then we shall look at the difference.
To create symbolic link, we use below commands:
Create a directory in “/usr/local/sbin/test_pwd”
Create a symbolic link to a directory “sym_test_pwd” by using below command
ls –s /usr/local/sbin/test_pwd sym_test_pwd
Now let us run the “pwd” and “pwd –L” command while going into both of the directories. As you can see a simple “pwd” command and “pwd –L” command will display the same output.
But when we use “pwd –P” in the symbolic link folder, it will resolve the symbolic link and display the actual directory.
2.2 Linux cd:
2.2.1 Usage:
“cd” stands for Change Directory.
Change Directory is one of the basic command that you should known. When you are working on a terminal, “cd” is the only command that you can use to navigate from one directory to another. Below we shall see some of the variants of “cd” command.
2.2.2 Options:
-L: Follow the symbolic link.
-P: Resolve the symbolic link, go to the actual directory.
When tried on the symbolic directory created above, “sym_test_pwd” with “-L” option goes to the symbolic directory. But with “-P” option will go to the actual directory.
2.2.3 Examples:
1. To go to “/usr/local/” directory from present directory:
cd /usr/local
2. To go to the previous directory from where you were working:
cd –
3. To go to one level up from the present directory use “..”
cd ..
4. To go to home directory use “cd”
5. To go to “/usr/local/sbin” without typing full directory names, use <TAB> after entering first letter. If there are multiple directories with the same first letter, then you need to use <TAB> 2 times to list the directories, then enter the second letter of that directory then use <TAB> to go to that directory.
cd /u<TAB>/l<TAB>/s<TAB><TAB>
Lists all the directories with same starting letter, the enter “cd /u<TAB>/l<TAB>/sb<TAB>” to go to “/usr/local/sbin”.
2.3 Linux Absolute and Relative paths:
2.3.1 Usage:
If the path to a particular directory starts from root directory is called as absolute path.
2.3.2 Example:
cd /usr/local/sbin
Linux Relative Path:
If the path to a directory starts by taking the present directory as a reference, then it is called as relative path.
Example:
You are in “/usr/local/sbin” and you want to navigate to “/usr/local/”:
Using Absolute path, you write “cd /usr/local/”.
Using Relative path, you write “cd ../”.
2.4 Linux ls
2.4.1 Usage
List Directory contents.
This command is used to list the contents of a directory.
2.4.2 Options:
-l list the properties of the file file permission, owner of the file, size, modified date and time.
-a Displays all the files and directories along with hidden files. Hidden files are those, whose first letter starts with a “.”.
-h Displays the details in a human readable format.
-F It is used to display “/” at the end of each directory.
-A Skip the current “.” and previous “..” directory in the output.
-r Display in reverse order alphabetically
-X sort the files according to the extension.
-t Sort according to the last modified time.
-R Used to list the sub directories recursively.
2.4.3 Example:
1. List all the files in a directory “ls –l” command.
As you can see from the output, there are 8 columns. They are:
1. File Permission
2. Number of hard links
3. Owner Name
4. Group Name
5. File size
6. Date of last modified
7. Time of last modified
8. File name
2. List all the files including hidden files in a directory “ls –la” command.
3. List all the files and directories in human readable format. “ls –lh”
4. List all directories with “/” at the end. “ls –F”
5. List files in reverse order. “ls –r”
6. List all the directories inside a directory recursively. “ls –R”
7. Sort by file size. “ls –lS”
8. Display the inode number of a file. “ls –i”.
2.5 Linux mkdir
2.5.1 Usage:
Create Linux directory
2.5.2 Options:
-v Write message after every operation
-p Create parent directory if needed
-m To specify the permissions
2.5.3 Example:
1. Create a simple directory. “mkdir test_dir”
2. Create directory with message after operation is completed “mkdir –v test_dir”
3. Create directory with parent directory. “mkdir –p dir_1/dir_2/dir_3”
4. Create directory with permissions. “mkdir –m 777 test_dir”
2.6 Linux rmdir
2.6.1 Usage:
Remove “empty” Directory in Linux.
2.6.2 Options:
-v Display message after operation is completed.
-p Removes parent directory also.
–ignore-fail-on-non-empty Will not display the error message, if the command is not able to delete the directory because it is not empty.
2.6.3 Example:
1. Remove simple directory. “rm test_dir”
2. Remove directory with exit message. “rm –v test_dir”
3. Remove directory along with parent directory. “rm –p dir_1/dir_2/dir_3”
4. Remove directory by suppressing the warnings. “rm –ignore-fail-on-non-empty dir_1”
Once you have run the command on a non empty directory, using “rmdir dir_1” we get an error. After we use “rm –ignore-fail-on-non-empty dir_1”, the error is not being displayed.
In both of the cases, the directory will not be deleted, because it is not empty. To delete “dir_1”, we have to delete “dir_2” first.