This tutorial explains basic Linux commands (pwd, cd, mkdir, ls, cp, mv, rm, rmidr, cat, more, less and touch) in detail with practical examples. Learn how these essential commands are used in Linux File System management including how absolute path and Relative path work in Linux.
Absolute path and Relative path in Linux
Linux file system is built from files and directories. Files are used to store the data. Directories are used to keep the files systematically. Root directory (/) is the main directory in Linux. All directories and files are created and managed under this directory.
We can access a file or directory in two ways :-
- From absolute path
- From relative path
Absolute Path
Absolute path starts from root directory and goes up to the actual object (file or directory). Since absolute path is the complete path, regardless where we are in LFS (Linux file system) it will always works. Absolute path does not change when we change the directory.
To know to the absolute path of current location we can use pwd command.
Relative path
Relative path starts from current location and goes up to the actual object. Since relative path depends on current location, it may not work in every case. When we change the directory, relative path also changes. To access objects from upper directory double dots (..) is used. To access objects from lower directory, path from current directory is used. Single dot represent current directory.
Practical Example
Login from normal user and create a directory. Access this directory from both paths.
Command used in this example
Command | Description |
pwd | To know the absolute path from root directory |
cd destination-directory | To change the current directory |
mkdir directory name | To create a new directory |
cd .. To move in parent directory |
Home directory is default location for user data. When we login, shell starts our session from home directory. No matter where we are in Linux file system, cd command without any option or argument will bring us back in home directory.
Listing directory
ls command is used to list the content of a directory. Without any argument it will list the content of current directory.
Common options used with ls command
-l :- This option is used to list the contents in list format including additional content details such as file type, permission, creation date and time, owner and group information etc.
-a :- By default ls command does not list hidden files. This option is used to list all files including hidden files.
-R :- This options is used to list the contents from specified directory and all sub directories.
-ltr :- This option is also used to list the contents in long format including additional details. But with option output will be sorted based on last modification date and time.
Practical Example
List the content from home directory and root directory. List the hidden files from home folder and find out the directory in which files are most recently modified.
Command | Description |
ls | list content of current directory |
ls / | list content of root directory |
ls /home/sanjay/test | list content of /home/sanjay/test directory |
ls –l | list content of current directory with additional information such as file type, permission, owner, group, creation date etc |
ls –a | list all content of current directory including hidden files |
ls –R | list content of current directory including contents from all sub directories |
ls –ltr | list content of current directory. Output will be sorted based on modification date & time. |
Copying files and directories
To copy a file or directory, we can use cp command. cp command needs two arguments; source and destination.
cp source destination
Source is the file or directory name along with full path. Destination is the location where we want to copy the source file or directory.
Common options used with cp command
-a :- This option is used to copy the file with all properties and permission. If file is copied without this option, file permission may (or may not) be changed at destination.
-R :- This option is used to copy all files and directories and sub- directories from source to destination.
By default cp command does not copy the hidden files. To copy the hidden files, we have to use –a option. If copying with –R option or * argument, hidden files will also be copied. In such a case –a option is not require to copy the hidden files. But it may require to maintain the permission and properties at destination.
Moving files and directories
To move files or directories, we can use mv command. This command works exactly same as cp command. The only difference between cp and mv is that in cp command file remains on both locations while in mv command file is deleted from source. mv command is also used to rename the file.
mv source destination
Removing files and directories
To delete files and directories following commands are used:-
Command | Description |
rm | This command is used to delete a file |
rmdir | This command is used to delete an empty directory |
rm –f | By default rm command asks for confirmation. This options is used to delete the file without confirmation |
rm –rf | This command is used to delete the directory and all its contents including sub-directories. |
Viewing a file
To view a file we have three basic commands; cat, more and less.
cat :- This command display all contents from file without any scroll facility. If a file has multiple pages then we will be able to see the content from last page only.
more :- This command display all contents from file with down scroll facility. If a file has multiple pages then we will be able to see all pages one by one in down direction. In this command we cannot scroll the output in upward direction.
less :- This command display all contents from file with up and down scroll facility. If a file has multiple pages then we will be able to see all pages one by one in both directions.
- To scroll the page use up and down arrow keys.
- To terminate the output press q key.
Creating an empty file
We can use touch command to create an empty file
touch file-name
Using wildcard and brace expansion
Shell supports two wildcards; asterisk and question mark. Asterisk represents all characters. Question mark represents a single character. For a range of characters we can use curly braces.
Wildcard and brace expansion examples
Example | Description |
cp test/* data/ | copy all files and directories from test directory to data directory |
ls /etc/* | list all files from /etc/ directory |
ls ?est | list all files which has four character name. In four character, first character doesn’t matter. It could be anything. But rest three characters must be est. Valid examples are aest, best, test, rest, fest |
touch test{1,2,3} | create three files test1, test2,test3. |
touch test{a,b,c} | create three files testa, testb,testc |
touch {1,2,3} – {a,b,c} | create nine files 1a, 1b, 1c, 2a, 2b, 3c, 3a, 3b, 3c |
touch {1..3} – {a..c} | create nine files 1a, 1b, 1c, 2a, 2b, 3c, 3a, 3b, 3c |
Practical Example
- Login from normal user and create a test directory.
- Move in test directory and create 100 files.
- Use alphabet range (a – j) and numeric range (1- 10) for files name.
- Confirm files creation. Exit from directory.
- Create a new directory with mydir name.
- Copy files which starts with letter a from test directory to mydir directory.
- Move the files which start with letter b from test directory to mydir.
- Confirm the copy and move action.
- Delete the files which end with letter 8.
- Confirm the action.
- Delete all files from test directory and delete the directory.
- Try to delete the mydir directory with rmdir command.
- Delete the mydir directory and clear the screen.
Login from normal user and create a test directory. Move in test directory and create 100 files. Use alphabet range (a – j) and numeric range (1- 10) for file name. Confirm files creation. Exit from directory.
Create a new directory with mydir name. Copy files which starts with letter a from test directory to mydir directory. Move the files which start with letter b from test directory to mydir. Confirm the copy and move action.
Delete all files from test directory and delete the directory. Try to delete the mydir directory with rmdir command. Delete the mydir directory and clear the screen.
Few more basic commands
We can use –p options with mkdir command to create a directory tree.
We can use head and tail command to view the starting and end part of a file respectively. If we know that the information we are looking for is available in starting, instead of viewing all file use head command. By default it will display first ten lines from file.
We can use –n option to display the specific number of lines.
Just like head command we can use tail command to display the bottom of file. (Default is 10 lines.)
We can use –n option to view specific lines from bottom.
As we know cat command is used to view the file. We can also create and append a file with cat command.
To create a file with cat command use cat > filename command. To save the file use Ctrl+D key combination.
To append the file use cat >> filename command.
While appending a file with cat command always use >>. If single > is used instead of >>, file would be overwritten without any warning.