Basic Linux Commands Explained With Examples
Basic Linux Commands Explained With Examples
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.
Exercise-1
Login from normal user and create a directory. Access this directory from both paths.
Solution
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
Tip
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.
-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.
Exercise-2
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.
Solution
Command Description
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.
Tip
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.
mv source destination
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.
touch file-name
Example Description
cp test/* data/ copy all files and directories from test directory to data directory
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
Exercise-3
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.
Solution
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.
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.
Thats all for this article. Please visit our site from more articles
ComputerNetworkingNotes.com