Author : Gurunadha Bottu
Created: 21-Jan-2019
Updated: 28-Dec-2021
Release : 1.0.0
Purpose: Unix commands with the explanation.
History: N/A
1)mkdir - It is used to create a directory or multiple directories
To create single directory
syntax : mkdir <directory_name>
command : mkdir guru
To create multiple directories
syntax : mkdir <directory_name1> <directory_name2> <directory_name3> ...etc.,
command : mkdir guru nammu mohan
Note : With the above command 3 directories will be created, namely guru,nammu and mohan.
To create parent directories only.
syntax : mkdir -p <directory_name1>/<directory_name2>/<directory_name3> ...
command : mkdir -p guru/nammu/mohan
Note : With the above command a directory structure will be created.
guru is parent to nammu and nammu is parent to mohan.i.e,
guru contains nammu sub-directory and nammu contains mohan sub-directory.
2)cd - Used to change directory
Assume currnetly you are in /home/nammu
syntax : cd <directoryname>
command : cd guru
With above command you will be moved one step ahead from current directory.
it means, you will be changed from nammu directory to guru directory.
you can see this by using a command "pwd"
command : pwd
output : /home/nammu/guru
To change directory one step backward(i.e, change to its parrent directory)
syntax : cd<emptysapce>..
command : cd ..
Above command changes your directory to its immediate parent directory
If you use "pwd" here
command : pwd
output : /home/nammu
3)pwd - stands for present working directory.
syntax: pwd
command: pwd
output : It displays the present working directory location(path)
4)cat - It is used to create a file/appends content to an existing file / concatenate 2 or more files
to form a new file.
Ex : To Create a file
Syntax : cat > <filename>
command :
cat>file1
one
two
three
Press CTRL+D (To save)
Ex : To Add contents to existing file "file1"
Syntax : cat>><filename>
command :
cat>>file1
four
five
Ex : To Display the contents of file "file1"
Syntax : cat <filename>
command :
cat file1
one
two
three
four
five
Ex : Create another file named "file2"
cat>file2
six
seven
Ex : To Display the contents of file "file2"
cat file2
six
seven
Ex : To combine two files "file1" & "file2" to create a new file "file3"
Syntax : cat <filename1> <filename2> > <new_filename>
command :
cat file1 file2>file3
cat file3
one
two
three
four
five
six
seven
5)touch: Creates a new file or updates its timestamp. Also can create empty files.
Syntax 1 : touch <filename>
command : touch a //creates single empty file a
Syntax 2 : touch <filename1> <filename2> <filename3>...etc.,
command : touch a b c d e //creates 5 empty files namely a,b,c,d and e.
1)cal - Displays the callender
ex : cal -- Displays current month's calender
ex : cal 2018 -- Displays all month's callender in the given year.
ex : cal december 2018 -- Displays december'2018 calander only.
2)date - Displays current system date & time
ex:date
output : Fri Jan 18 13:31:56 IST 2019
ex: date +%d/%m/%y
output : 27/12/18
ex:date +%d-%m-%y
output : 27-12-18
ex:date +%d-%m-%Y3
output : 27-12-2018
3)banner: Prints a large banner on the standard output(Monitor).
ex: banner guru
output : Displays guru in larger size
4)who: Displays the list of users currently logged in
ex : who
output : gvp-88 :0 2018-12-27 10:15 (:0)
5) whoami: Displays the user id of the currently logged in user.
ex:whoami
output : gvp-88
7)cat - used to create a file/ append content to an existing file/concatinate 2 or more files
to form a new file.
Ex : To Create a file
cat>file1
one
two
three
Press CTRL+D (To save)
Ex : To Add contents to existing file "file1"
cat>>file1
four
five
Ex : To Display the contents of file "file1"
cat file1
one
two
three
four
five
Ex : Create another file named "file2"
cat>file2
six
seven
Ex : To Display the contents of file "file2"
cat file2
six
seven
Ex : To combine two files "file1" & "file2" to create "file3"
cat file1 file2>file3
cat file3
one
two
three
four
five
six
seven
8)rm: Remove files and directories
rm <filename>
ex: rm abc.c // here abc.c is file name
rm -R <directoryname> (or) rm -r <directoryname>
ex : rm -R guru // here guru is directory name
rm -i <filename>, it asks you for confirmation
ex : rm -i file3
Note : The following message will be displayed,
rm: remove regular file 'file3'?
type "y" or "n" after questing mark followed by enter key.
rm: remove regular file 'file3'? y
9)rmdir: To remove empty directory
ex:rmdir guru // removes guru directory if it is empty.
// if the directory is not empty, we go for rm -R.
ex : rm -r guru (or) rm -R guru
6)ls - Used to list out the contents a directory
command : ls
It Displays contents of directories and their subdirectories.
command : ls -a
It lists all files, including the ones whose filenames begin in a dot(Hidden files)
command : ls -1
It display One File Per Line [ in this command option is ONE ]
Consider the following output:
drwxr-xr-x 2 gvp-85 gvp-85 4096 Jan 18 14:58 guruora
-rw-rw-r-- 1 gvp-85 gvp-85 6923 Jan 18 15:44 guru_unix.txt
Note : In the above two outputs it contains 7 Fields. First Field contains 10 characters.
Description about First Character in First Field of 10 Characters.
If 1st Character is Description
– (hyphen) : it is a normal file
d : it is a directory
s : it is a socket file
l : it is a link file
Next(remaining) 9 Characters specifies file permissions.
Description about remaining 9 Characters in the First Field.
Every 3 characters specifies read, write, execute permissions
for user(root), group and others respectively in order.
If all three permissions are given to user(root)(u), group(g) and others(o),
the format looks like -rwxrwxrwx
Second Part specifies number of links for that file/directory
Third field specifies owner of the file.
Fourth field specifies the group of the file.,the file belongs to which group.
Fifth field specifies the size of file in bytes.
Sixth field specifies the date and time of the last modification of the file.
Seventh and the last field is the name of the file/directory
command : ls /
It displays contents of root directory.
command : ls -l
It shows long listing information about the file / directory [ in this command option is alphabet 'l' ].
shows file / directory permissions.
command : ls -r
It displays contents in reverse order.
command : ls -t
It displays contents sorted order by time & date.
command : ls -s
It displays contents sort by size.
command : ls -lh
It displays contents in human readable size(Mb,Kb,Gb,.etc).
command : ls -d */
It displays only directories.
command : ls -lrt>tmp
The output will be redirected (copied) to a file tmp.
Note : Use cat tmp to see the contents
command : ls -R
It displays all the contents of directories and their subdirectories.
7)cal - Displays the callender
Note : Year range starts from 1 to 999999999, we can use any number in the range.
for a 4 digit year we shoud use all 4 digits, it is manditory.
command : cal -- Displays current month's calender
Syntax : cal <year>
command : cal 2018
output : Displays all month's callender in the given year.
Syntax : cal <month_name> <year>
command : cal december 2018
output : Displays december'2018 calander only.
8)date - Displays current system date & time
command : date
output : Tue Jan 22 16:02:59 UTC 2019
d - day number, m - month number, y - year in last two digits, Y - year in four digits
command : date +%d/%m/%y
output : 22/01/19
command : date +%d-%m-%y
output : 22-01-19
command : date +%d-%m-%Y
output : 22-01-2019
9)cp - to make duplicate copy of a file
To copy a file in the same location, source and destination file names should be different
syntax : cp <source_filename> <destination_filename>
command : cp a.txt b.txt
Note : In above command a new file b.txt is created
and a.txt contents are copied to b.txt file.
To copy a file in different location,i.e, not at source file location.
Here source and destination file names can be same.
syntax : cp <source_filename> <path/destination_filename>
command : cp a.txt /home/guru/a.txt
Note : In the above command a.txt is copied to the path /home/guru
Here "home" is a subdirectory to "root(/)" directory and "guru" is subdirectory to "home" directory.
To coyp all files in source path to destination path
syntax : cp * <destination_path>
command : cp * /home/nammu
In the above command all files in the current location will be copied to /home/nammu location
10)mv - It is used to move file(s),directory or to rename file,directory
To move a single file
syntax : mv <filename> <destination_path>
command : mv a.txt /home/guru/mohan
In the above command a.txt will be moved to the location /home/guru/mohan
To move multiple files
syntax : mv <*.java> <*.c> <*.sh> <destination_path>
command : mv *.java *.c *.sh /home/guru/mohan
In the above command all java,c and shell scripts will be moved to the location /home/guru/mohan
To rename a single file
syntax : mv <filename> <destination_path>
command : mv a.txt /home/guru/mohan
In the above command a.txt will be moved to the location /home/guru/mohan
To move multiple files
syntax : mv <*.java> <*.c> <*.sh> <destination_path>
command : mv *.java *.c *.sh /home/guru/mohan
In the above command all java,c and shell scripts will be moved to the location /home/guru/mohan
To move a directory
syntax : mv <directoryname> <destination_path>
command : mv pqr /home/guru/mohan
In the above command pqr will be moved to the location /home/guru/mohan
To rename a file
syntax : mv <filename> <new_filename>
command : mv a.txt b.txt
In the above command a.txt will be renamed with b.txt
command : mv a.txt /home/guru/mohan/b.txt
In the above command a.txt will be renamed with b.txt & moved to the location /home/guru/mohan
To rename a directory
syntax : mv <directory_name> <new_directoryname>
command : mv abc def
In the above command directory abc will be renamed as def
11)more - Used the display screen full contents of a file,
syntax : more <filename> -- shows the first part of a file, just as much as will fit on one screen.
Just hit the space bar to see more or press q to quit.
command : more tst.txt
syntax : more +<linenumber> <filename>
command : more +11 tst.txt
Note : Above command displays tst.txt file contents from line number 11.
You can use pattern with more command,to search for a pattern.
syntax : more +/<pattern> <filename>
command : more +/guru tst.txt
13)rm: Remove files and directories
syntax : rm <filename>
command : rm abc.c // here abc.c is file name
syntax : rm -R <directoryname> (or) rm -r <directoryname>
command : rm -R guru // here guru is directory name
syntax : rm -i <filename>, it asks you for confirmation
command : rm -i file3
Note : The following message will be displayed,
rm: remove regular file 'file3'?
type "y" or "n" after questing mark followed by enter key.
rm: remove regular file 'file3'? y
14)rmdir : To remove empty directory
syntax : rmdir <directoryname> // removes guru directory if it is empty.
command : rmdir guru // if the directory is not empty, we go for rm -R.
command : rm -r guru (or) rm -R guru
15)more --shows the first part of a file, just as much as will fit on one screen.
Just hit the space bar to see more or q to quit.
syntax : more <filename>
command : more tst.txt
syntax : more +<linenumber> <filename>
command : more +11 tst.txt
Above command displays tst.txt file contents from line number 11.
You can use pattern with more command,to search for a pattern.
syntax : more +/<pattern> <filename>
command : more +/guru tst.txt
16)who : Displays the list of users currently logged in
syntax : who
command : who
output : gvp-88 :0 2018-12-27 10:15 (:0)
17)whoami : Displays the user id of the currently logged in user.
syntax : whoami
command : whoami
output : gvp-88
18)zip - To compress a file or direcrory
To compress a file
syntax :zip <zip_file_name.zip> <filename>
command :zip k.zip sum1.c
To decompress a file,more than one file or a directory
syntax for file : unzip <zip_file_name.zip>
command : unzip k.zip // here k is filename
syntax for directory : unzip <zip_directory_name.zip>
command : unzip tmp.zip // here tmp is directory name
To compress more than one file
syntax : zip <zip_file_name.zip> <filename1> <filename2> <filename3>...etc.,
command : zip m.zip *.java *.c *.sh
To compress a directory
syntax : zip -r <zip_file_name> <directory_name>
command : zip -r dr.zip guruji
19)gzip -- It compresses files, so that they take up much less space. Usually text files compress to about half their
original size, but it depends very much on the size of the file and the nature of the contents. Gzip produces
files with the ending '.gz' appended to the original filename. We may use gzip or gunzip to uncompress files.
To compress a file
syntax : gzip <file_name>
command : gzip sum1.c
To decompress a file
syntax : gzip <zip_file_name>
command : gzip -d sum1.c.gz
(OR)
syntax : gunzip <zip_file_name>
command : gunzip sum1.c.gz (It removes compressed file upon decompressing)
syntax : gunzip <zip_file_name>
command : gunzip -c sum1.c.gz (It retains compressed file)
To see the contents of compressed file
command : gunzip -c sum1.c.gz
To compress all files in a directory and its subdirectories if any
syntax : gzip -r <directory_name>
command : gzip -r guruji
Note : Above command compresses all files in guruji directory
and its sub-directories if any.
20)To change hostname perminently
syntax : hostnamectl set-hostname <new_hostname>
command : hostnamectl set-hostname nammu
21)wc -- displays number of lines, words, size and filename of the given filename.
syntax : wc <filename>
Assume tst has following contents,let us see contents using cat
command : cat tst1
This is a test1 file
This line is added to tst1
Now use wc
command : wc tst1
output : 2 11 47 tst1
Note : In the above output 2 indicates number of lines,11 indicates words,
47 indicates size and tst1 is the filename.
22)history - displays history of commands in the current session
syntax : history
command : history
23)clear - clears the terminal screen
syntax : clear
command : clear
24)which - Locate a command from where that command can run.
syntax : which [-a] filename
command : which -a cat
output : /bin/cat
command : which cal
output : /usr/bin/cal
24)su - used to change user/user-id or become a super-user.
To change user
syntax : su [options] [username]
command : su guru
If you are in admin user, then you will be changed to user "guru"
If you are in a normal user, then it asks for a password for "guru"
provide it, only then you will be able to change to "guru" user.
10)ps - displays a snapshot of all current processes
ex : ps
gvp-85@gvp85:~/guru$ ps (Terminal 1)
output:
PID TTY TIME CMD
2165 pts/0 00:00:00 bash
4689 pts/0 00:00:00 ps
ex : ps
gvp-85@gvp85:~/guru$ ps (Terminal 2)
output:
PID TTY TIME CMD
4701 pts/1 00:00:00 bash
4711 pts/1 00:00:00 ps
ex : ps
gvp-85@gvp85:~/guru$ ps (Terminal 3)
output:
PID TTY TIME CMD
4723 pts/2 00:00:00 bash
4731 pts/2 00:00:00 ps
ex: ps -ef
output : displays currently running processes in a table format as follows
UID PID PPID C STIME TTY TIME CMD
ex : gvp-85 4723 2155 0 14:18 pts/2 00:00:00 bash
11)grep searches for PATTERN in each FILE.
The current directory has 4 files namely samp,samp1,samp2
and samp3
ex:cat samp
output:
one
two
three
four
five
six
seven
eight
nine
ten
ex : grep 't' samp
output: Displays lines containing 't'
two
three
eight
ten
ex : grep -n 't' samp
output : Displays lines containing 't' & shows line numbers
2:two
3:three
8:eight
10:ten
ex: grep 't' *
output: Displays all the lines in all the files containing 't'
samp:two
samp:three
samp:eight
samp:ten
samp1:two
samp1:three
samp1:eight
samp1:ten
samp3:two
samp3:three
samp3:eight
samp3:ten
Note : In above output samp,samp1 and samp3 are filenames.
ex : grep 'T' *
output: Displays lines containing 'T' in all files
samp2:TWO
samp2:THREE
samp2:EIGHT
samp2:TEN
Note : In the above example samp2 is filename.
ex : cat *|grep -c 'T'
(It displays no of words containing 'T' in all files in the corrent directory)
output:4
ex : cat *|grep -c 't'
(It displays no of words containing 't' in all files in the corrent directory)
output : 12
ex:
grep -w "one" samp
output:
one
ex:
grep -w "one" *
output:
samp:one
samp1:one
samp3:one
ex:
grep -w "ONE" *
output:
samp2:ONE
12)sed : The name stands for Stream EDitor,and here "stream" can be a file, a pipe or simply stdin.
Sed command doesn’t update data. It only sends the changed text to STDOUT. The original file remains unchanged. If you need to overwrite the existing content, you need to use redirection operator(see ex:2).
ex1:
echo "Welcome to Gurunath page" | sed 's/page/website/'
Explnation : In the above command it searches for word "page"
and replaces it with "website"
output : Welcome to Gurunath website.
ex2:sed 's/FOUR/foUr/g' samp5>samp6
samp5:
one
two
three
FOUR
five
After above command the pattern 'FOUR' of the file samp5,
will be changed as 'foUr' and the output is redirected to samp6
output:
one
two
three
foUr
five
To run multiple sed commands, you can use -e option
Note : separate each pattern by semicolon
ex3: sed -e 's/two/Two/; s/five/fiVE/' samp
output: "two" will be replaced by "Two" and "five" by "fiVE"
one
Two
three
four
fiVE
six
seven
eight
nine
ten
13)ls Displays contents of directoyes and their subdirectories.
ls -a -- lists all files, including the ones whose
filenames begin in a dot(Hidden files)
ls -1 -- Display One File Per Line
ls -l -- To show long listing information about the file/directory.
ex : drwxr-xr-x 2 gvp-85 gvp-85 4096 Jan 18 14:58 guruora
ex : -rw-rw-r-- 1 gvp-85 gvp-85 6923 Jan 18 15:44 guru_unix.txt
Note : In the above two outputs it contains 7 Fields.
First Field contains 10 characters.
First character Its Description
– : normal file
d : directory
s : socket file
l : link file
Next(remaining) 9 Characters specifies file permissions.
Every 3 characters specifies read, write, execute permissions
for user(root), group and others respectively in order.
If all three permissions are given to user(root)(u), group(g) and others(o),
the format looks like -rwxrwxrwx
Second Part specifies number of links for that file/directory
Third field specifies owner of the file.
Fourth field specifies the group of the file.,the file belongs to which group.
Fifth field specifies the size of file in bytes.
Sixth field specifies the date and time of the last modification of the file.
Seventh and the last field is the name of the file/directory
14)more
ex : more <filename> -- shows the first part of a file, just as much as will fit on one screen.
Just hit the space bar to see more or q to quit.
ex: more tst.txt
ex : more +<linenumber> <filename>
more +11 tst.txt
It displays tst.txt file from line number 11.
You can use pattern with more command,to search for a pattern.
ex : more +/<pattern> <filename>
more +/guru tst.txt
It searches tst.txt file for the pattern guru and displays all
lines containing guru
15)diff
ex : diff <filename1> <filename2> -- compares files, and shows where they differ
16)wc
ex : wc <filename> -- displays number of lines, words, size and filename
of the given filename.
cat tst1
This is a test1 file
This line is added to tst1
wc tst1
output : 2 11 47 tst1
Note : In the above output 2 indicates number of lines,11 indicates words,
47 indicates size and tst1 is the filename.
17)chmod
ex : chmod <options> <filename> -- allows us to change read, write and execute
permissions on files, for all categories of users like user(u),group(g)
and other(o) respectively.
Note : In chmod command it uses octal number system as well as alphabets
ls -lrt
-rw-r--r-- 1 gvp-85 gvp-85 334559382 Oct 30 16:11 hadoop-3.1.1.tar.gz
drwxr-xr-x 2 gvp-85 gvp-85 4096 Jan 18 14:58 guruora
-rw-r--r-- 1 gvp-85 gvp-85 6854 Jan 18 15:25 bd
-rw-r--r-- 1 gvp-85 gvp-85 49 Jan 18 15:58 camp
-rw-r--r-- 1 gvp-85 gvp-85 49 Jan 18 16:00 clamp
-rw-r--r-- 1 gvp-85 gvp-85 50 Jan 18 16:23 samp
-rw-rw-r-- 1 gvp-85 gvp-85 7998 Jan 19 12:06 guru_unix.txt
ls -lrt
drwxr-xr-x 2 root root 4096 Jan 21 16:17 guru
-rw-r--r-- 1 root root 20 Jan 21 16:18 file1
chmod 000 file1
ls -lrt
drwxr-xr-x 2 root root 4096 Jan 21 16:17 guru
---------- 1 root root 20 Jan 21 16:18 file1
chmod 246 file1
In the above command user(root) only get write,group get read and
others will get read&write permissions only
ls -lrt
drwxr-xr-x 2 root root 4096 Jan 21 16:17 guru
--w-r--rw- 1 root root 20 Jan 21 16:18 file1
chmod a-x guru (here guru is a directory)
In the above command all users(user,group,others will get execute permission)
ls -lrt
drw-r--r-- 2 root root 4096 Jan 21 16:17 guru
18)gzip
filename --- compresses files, so that they take up much less space. Usually text files compress to about half their original size, but it depends very much on the size of the file and the nature of the contents. There are other tools for this purpose, too (e.g. compress), but gzip usually gives the highest compression rate. Gzip produces files with the ending '.gz' appended to the original filename.
To compress a file
gzip a
ls -lrt
-rw-r--r-- 1 root root 44 Jan 21 16:49 a.gz
To decompress a file
gzip -d a.gz
ls -lrt
-rw-r--r-- 1 root root 55 Jan 21 16:49 a
No comments:
Post a Comment