Now
let us get into commands. Please note that everything in Linux is
case-sensitive, so all commands should be in appropriate case.
- pwd
– the command is called “print working directory” which is used to
print the current directory. When you are logged in, you are taken into
your home directory. When Linux administrator creates your user account,
he specifies your home directory where you generally keep files,
folders etc. Type the command pwd in command prompt and hit [ Enter ].
- who
– this give you information regarding currently logged in users.
Suppose you want to know how many users are connected to Linux system
along with you. This command gives details like user id, logged in time,
terminals to which users are connected etc. This has another variation who am i which gives information about your session. Type the command who in your command prompt and hit [ Enter ].
First column is user id, second column is the terminal and third one is the logged in time. Now type who am i, hit [ Enter ] and verify the result.
- cd -
short name of “change directory” which is used to switch to another
directory from your current working directory. This command is used with
the argument directory name to which we should change, cd <directory name>. If you type cd without any argument, you are changed to your home directory. Type cd /usr in your command prompt and hit enter. Then type pwd command and hit [ Enter ].
In the above diagram you could see that /usr is printed on executing pwd command. This means that we changed our working directory to /usr using cd command. Now just type cd and hit [ Enter ]. Validate the result using pwd command.
- ls - List command used to show files and directories. If you just type ls,
it lists files and directories in the current working directory.
Optionally you may give the directory name as argument to list content
in that particular directory. You may add combination of additional
arguments to get more features in the result set. Some arguments are
given below.
-l
– this argument gives detailed listing of files and directories like
owner of the file, size of the file, whether the file is a directory,
last modified time etc. This is a common command everybody uses.
-R
– The argument -R is used for recursive purpose. This means it list all
files in sub directories also. It searches all inner directories
recursively and give results.
-a – The argument -a lists all hidden files.
-t – sort by modification time in descending order
directory name – If you give directory name as argument, it lists content in that directory rather than listing current working directory.
Ok, now let us put everything into an example. Go to your command prompt, type ls -lt /usr and hit [ Enter ].
The above command gives detailed listing of files in the directory /usr
in descending order of modified time. The first column is about file
permissions. The first character shows whether it is a directory. For
eg, in the above screenshot we could see that bin is a directory because
the first character of file permission column is d.
output.log is a file because the first character is -.
Second column gives the number of links to the file. Third column shows
the owner of the file and fourth column is about owner group. Fifth
column shows file size in bytes and sixth column about last modified
time. Final column shows the name of the file. Please note that in Linux
everything is file. So directory is also a file in Linux system.
- mkdir -
Used to create directories. You should give directory name as argument.
Let us create a directory in your home directory. Go to your home
directory using cd command. Type the following command and hit [ Enter ].
mkdir testdir
The above command creates testdir in your home directory. Verify the same using ls command.
- touch
– This command is used to change the timestamp of the file. But one of
the most import function of the command is to create an empty file if it
do not exist. Go to your system directory, type the following command
and hit [ Enter ].
touch sample.txt
It creates sample.txt file. Verify the same using ls command.
- cat – cat command
is very useful for many purposes like creating files, adding and
appending content to files etc. It also diplays the content of the file
to standard output or another file. You may concatenate multiple files
and display the content. Let us try some examples. Type the following
command and hit [ Enter ].
cat > sample.txt
It
waits for the input which should be added to the file in the next line.
Type the text “Hello how are you?” and hit [ Enter ]. Then press [
Ctrl+d ]. This add the text to sample.txt file. Now let us display the
content of the file.
cat sample.txt
You get the message in screen. Ok, now rather than writing the content to standard output, let us write it into another file.
cat sample.txt > samplecopy.txt
The
above command creates samplecopy.txt file if it do not exists already
and add the content of the file sample.txt. Verify the result using cat command.
Let us append the content. Type the following command and hit [ Enter ].
cat >> sample.txt
In
the next line just add the text “I am fine”, hit [ Enter ] and press [
Ctrl+d ]. Verify the content of the file sample.txt using cat command. You may combine multiple files and show the output. For eg,
cat sample.txt samplecopy.txt, displays the content from both files. You may send it to another file also.
- cp -
copy command is used to copy files and directories. You have to give
source and destination of files and directories. For eg, when you copy
file, you may give the destination as either file name or directory name
to which the file should be copied. You may copy entire directory also
to another location. Some sample examples are given below.
cp sample.txt newsample.txt, this
copies the file sample.txt to newsample.txt. You have to give source
file name as first argument followed by destination. Verify the result.
Now
let us copy the file sample.txt to our testdir directory which we
already created. Type the following command and hit [ Enter ].
cp sample.txt testdir, this copies the file to directory. Here we provide directory name as second argument. Verify the result using cd and ls commands. Now let us copy the directory testdir. Go to your home directory and type the following command. Hit [ Enter ].
cp -R testdir newdir,
this command copies testdir and its content to another directory
newdir. Verify the result. Check whether sample.txt file in testdir is
also available in newdir directory. -R argument is used to copy directories recursively.
- mv
– move command is used to rename files and directories. It is also used
to move files from source to destination. Let us try some examples. Go
to your home directory and execute following commands.
mv sample.txt log.txt, this command rename sample.txt to log.txt. source file name is given as first argument.
mv testdir logdir, this rename testdir directory to logdir directory. Please note that the content inside directory are not renamed.
mv log.txt logdir, this command moves log.txt file to logdir directory.
- rm – remove command is used to delete files and directories. Some examples are given below.
rm newsample.txt,
deletes newsample.txt file. You can give multiple file names in single
command delimited by space to delete multiple files in a stretch. For
eg, rm file1 file2 remove both files.
rm -R logdir, this command deletes logdir directory recursively.
- chmod
– this command is used to change file permissions. Every file or
directory in Linux is associated with file permissions. There are three
levels of permissions, read, edit and execute. You can’t modify the file
if you don’t have write permission. Similarly if you want to execute a
shell script file, you need execute permission on it.
Users are categorized based on the following groups.
- Owner – the owner of the file, basically if the file is created by you, you are the owner of the file.
- Owner Group
– the group in which the owner is a member. Owner group level
permission allow other users who also belong to this group to share the
same permission level.
- Others – those users who do not belong to the above categories.
The command ls -lt gives
details about file permissions in first column. For eg, in the above
screen shot let us examine the permission of newdir directory. The first character d specifies that it is a directory. Next three characters is about owner permissions. rwx
shows that owner has all rights. Next three characters are related to
owner group file permission. Here the members of owner group have no
write permission. Similarly last three characters are related to other
members permission. Here also they don’t have write permission.
chmod could be used with different argument types, but I always like to use number arguments.
4 – read access
2 – write access
1 – execute access
You should add these integers according to the given permission. Let us try some examples.
chmod 777 samplecopy.txt,
this command gives full rights to all three types of users. First
number is for owner, second for owner group and third for others.
Suppose you want to protect your file so that others are not allowed to
read it. You may give the command as
chmod 700 samplecopy.txt
Similarly if you want to give just read access other than you, you may give command as,
chmod 744 samplecopy.txt
- passwd – this
command is used to change the password of the current user. It asks for
current password, once you supply it you may type the new password to
change it.
- grep
– this command is used to search for a particular pattern in a file or
other input. This is commonly used with other commands like ls, cat
etc as input using pipe symbol. One of the common use of the command is
to search for a particular file in a directory. Suppose you have an
image directory and you would like to know whether a particular image
exists in directory. Then you may give command like this,
ls -lt | grep image.gif, this gives output row from detailed listing. For eg, just go through the following screenshot.
In the above example, we just want to search for a pattern “copy” which is found in samplecopy.txt file listing record.
- vi - vi
is a text editor used to edit files which is a very import command in
Linux. Some Linux versions like Ubuntu give an improved version of vi
editor called vim. vi has three modes; insert mode, command mode and line mode.
- Insert mode – For making any changes in the file, you should be in insert mode.
- Command mode
– When you open vi editor you will be in this mode. This mode is used
to do many shortcut tasks like delete words, switch to insert mode, copy
and paste operation etc. When you are in insert mode you may press [
Esc ] key to get into command mode.
- Line mode – this mode is used to issue commands for saving file, quitting editor etc. By pressing [ : ] key, you may get into this mode from command mode.
vi
editor has large number of commands, you should be reasonably familiar
with it in order to use Linux system. Let us do some examples here. Go
to your home directory, type the following command and hit [ Enter ].
vi example
The
editor appears. Now we are in command prompt. If you want to get into
insert mode press [ i] key. Once you press [ i ] key, you may enter text
into the editor. Just type “Hello how are you?” in the editor. Once you
made modifications in the editor, you should go to command prompt by
pressing [ Esc ] key. Now you are in command prompt. Now let us save the
file. Save command is executed at line mode. Press [ : ] key. For saving the file, you have to give the command wq and hit enter. This command saves the file and quit the editor.
Now verify the content of the file using cat command. Ok now let us examine some vi commands executed in command mode. In command mode, almost all commands do not require to press [ Enter ] key.
i – switch to insert mode, content is inserted just before the current cursor position.
a – switch to insert mode, text is appended just after the current cursor position.
A – switch to insert mode, start appending text after the end of the last character.
x – deletes single character in the current cursor position.
dw – deletes word in the current cursor position.
dd – delete current line.u – undo last change.
h – move cursor to left like left arrow key.
l – move cursor to right like right arrow key.
j – move cursor down to next line similar to down arrow key.
k – move cursor up to next line similar to up arrow key.
Some
commands useful in line mode. Almost all commands in this mode are
followed by pressing [ Enter ] key. You go to line mode from command
mode by pressing [ : ] key.
- w – save file, got back into command mode.
- wq – save and quit editor.
- q – exit editor.
- q! – quit editor without saving last session changes.
- /[pattern]
– search for pattern in the file. It go back to command prompt and if
any match is found, it keeps the cursor in the first match line.
- $ – move the cursor to the last line in the file.
- man - man <command name>
is used to get help regarding a particular command in Liux. If you want
to know more about a command you should use this command. For eg, man ls is used to get help for list command. Type the command in shell prompt and hit [ Enter ].
- more
– Suppose you are making a detailed listing of large number of files in
a directory. If you want to see the listing page by page, more command comes at your help. Just type the following command and hit [ Enter ].
ls -lt /etc | more, here the listing of ls command is used as input for more
command using pipe symbol. Here the number of records is showing in a
page and waits for another key press. If you want to go to next page,
you may press [ Space ] key .
- tail – tail
command is used to print the last lines of input text. The default
value of number of lines is 10. But you can limit the number of lines
using -n or –lines argument. Go to your home directory and type,
ls -lt | tail -n 1, this command prints the last line of listing. You may use the command to print the last portion of file also. For eg, tail -n 1 example prints the last line of example file.
- wc – The word count command gives count about words, lines and characters in input text. -l argument gives number of lines, -w gives number of words, -m gives number of characters. For eg,
wc -w example gives number of words in example file.