Shreyaunix
Shreyaunix
Shreyaunix
sudo command
The sudo command stands for "superuser do". It prompts you for your personal password and
confirms your request to execute a command by checking a file, called sudoers, which the system
administrator configures. Using the sudoers file, system administrators can give certain users or
groups access to some or all commands without those users having to know the root password. It
also logs all commands and arguments so there is a record of who used it for what, and when.
To use the sudo command, at the command prompt, enter:
sudo command
2. su command
The su command stands for "switch user", and allows you to become another user. To use
the su command on a per-command basis, enter:
su user
Replace user with the name of the account which you'd like to run the commands as.
The user feature is optional; if you don't provide a user, the su command defaults to
the root account, which in Unix is the system administrator account. In either case, you'll be
prompted for the password associated with the account for which you're trying to run the
command. If you supply a user, you will be logged in as that account until you exit it. To do so,
press Ctrl-d or type exit at the command prompt.
3. who-
who is used to find out who is logged on to the system, what line they’re on, and how long they’ve
been logged in.
4. whoami –
This command lists the account name associated with the current login.
5. who am i-
This command tells about my identity on the computer, including the name of the computer
itself, my account name, and where and when I logged in.
6. which
Locates the executable file associated with a given command.
The which command shows the full path for its given arguments. This command searchers for
an executable or script in the user’s PATH.
It does this by searching the paths in the PATH environment variable for executable files
matching the names of the arguments.
7. mail
Opens the mail program and displays the first message in the mailbox, if any
9. Hostname
hostname is used to display the system's DNSname, and to display or set
its hostname or NIS (Network Information Services) domain name.
The hostname command shows or sets the system hostname.
When called without any arguments, hostname will display the name of the
system as returned by the gethostname function.
OPTIONS-
1. –i (IP address)
hostname –i prints the IP address of the computer.
2. –d (Domain name)
Print the domain name. To know the domain name where the computer resides, use the -d
option with hostname command.
3. –h (help)
To get help about the hostname command either use the man command or the -h
option with hostname command.
10. Uname
This command displays certain features of the operating system running on your machine.
By default, it displays name of the operating system
OPTIONS-
1. –r ( the current version)
uname –r displays the version of operating system (here version number of kernel)
x
While x will show all process even the current process is not associated with any TTY (terminal)
For some situation we may want to filter processes by user. To do this, we can use -u option. Let
say we want to see what processes which run by user pungki. So the command will be like below
$ ps -u pungki
Another thing that you might want to see is filter the result by CPU or memory usage. With this, you
can grab information about which processes that consume your resource. To do this, we can
use aux options. Here’s an example of it :
$ ps -aux | less
Since the result can be in a long list, we can pipe less command into ps command.
By default, the result will be in unsorted form. If we want to sort by particular column, we can add --
sortoption into ps command.
To to this, we can use -C option followed by the keyword. Let say, we want to show processes
named getty. We can type :
$ ps -C getty
If we want to show more detail about the result, we can add -f option to show it on full format listing.
The above command will looks like below :
$ ps -f -C getty
12. cd ( changing the current directory)
We can move around in the file system using cd command.
When used with an argument, it changes the current directory to the directory specified as
argument.
When you need to switch to the file present in another directory, absolute pathname must be
used as –
cd can also be used without any arguments. Cd used without arguments reverts to the home
directory.
cd..
An argument to cd to move to the parent directory.
11. -1
This option lists only one file in a line
13. Cat command
1. Display Contains of File
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
narad:x:500:500::/home/narad:/bin/bash
Hello everybody
Hi world,
# cat >test2
Awaits input from user, type desired text and press CTRL+D (hold down Ctrl
Key and type ‘d‘) to exit. The text will be written in test2 file. You can see contains
of file with following cat command.
# cat test2
If file having large number of contains that won’t fit in output terminal and screen
scrolls up very fast, we can use parameters more and less with cat command as
show above.
With -n option you could see the line numbers of a file song.txt in the output
terminal.
# cat -n song.txt
In the below, you can see with -e option that ‘$‘ is shows at the end of line and also
in space showing ‘$‘ if there is any gap between paragraphs. This options is useful
to squeeze multiple lines in a single line.
# cat -e test
# cat -T test
We can redirect standard output of a file into a new file else existing file with ‘>‘
(greater than) symbol. Careful, existing contains of test1 will be overwritten by
contains of test file.
Appends in existing file with ‘>>‘ (double greater than) symbol. Here, contains
of test file will be appended at the end of test1 file.
When you use the redirect with standard input ‘<‘ (less than symbol), it use file
name test2 as a input for a command and output will be shown in a terminal.
This will create a file called test3 and all output will be redirected in a newly created
file.
This will create a file test4 and output of cat command is piped to sort and result
will be redirected in a newly created file.
14. chmod
Used for setting file and directory permissions.
Syntax.
chmod [0-7][0-7][0-7] filename (Absolute Mode)
u is for user,
g is for group,
and o is for others.
Following are the symbolic representation of three different permissions:
7. Change execute permission only on the directories (files are not affected)
$ chmod u+X *
The -c flag outputs the compressed copy of file.txt to stdout, this is then sent to
file.txt.gz, keeping the original file.txt file in place. Newer versions of gzip may
also have -k or –keep available, which could be used instead with “gzip -k
file.txt”.
./example:
.:
./example:
In the above example there are 3 .txt files in the test directory which is our
current working directory, there is also an example sub directory which
contains example.txt. Upon running gzip with the -r flag over everything, all
files were recursively compressed.
This can be reversed by running “gzip -dr *”, where -d is used to decompress
and -r performs this on all of the files recursively.
OR
gunzip file.txt.gz
Both of these commands will produce the same result, decompressing file.txt.gz
to file.txt, removing the compressed file.txt.gz file.
Similar to example 3, it is possible to decompress a file and keep the original .gz
file as below.
In this example, a gzipped copy of the Linux kernel has compressed to 78.3% of
its original size, taking up 121MB of space rather than 554MB.
16. Links:
Every file has a data structure known as an inode that stores information about the file,
and the filename is simply used as a reference to that data structure.
A link is simply a way to refer to the contents of the file. They are of two types:
1. Hard Links
A file can have multiple filenames. When that happens then the file has more
than one link.
If the content of one file gets changed, it will be reflected in other (linked) file as
well.
We can link third filename emp.dat
We can also link multiple files but in that case destination filename must be a
directory.
ln chap?? Projects_dir
2. Symbolic Links
We can’t have two linked filenames in two file systems. Or we can simply
say that we can’t link a filename in the /usr file system to another in the
/home file system.
We can identify symbolic link with l in the permission field. The pointer notation
(->) suggests that note.sym contains the pathname for the filename note.
Only note contains the data and not note.sym.
cat note.sym opens the file the link points to. The size of the symbolic link is 4
that is length of the pathname it contains.
We have two files and they are not identical. So removing note.sym won’t affect
us. But if we remove note, we would lose the file containing the data. In that case,
note.sym would point to a non-existent file and become a dangling symbolic link.
Symbolic links can be used with relative pathnames. They can also span multiple
file systems and also link directories. It can link all filenames in a directory to
another directory. Symbolic link has a separate directory entry with its own
inode number.
We can use rm to remove a symbolic link
17. mkdir ( making directories)
This command is used to create a directory. The command is followed by names of the
directories to be created.
OPTIONS-
mkdir –m mode
Sets the access mode for the new directory ( as with chmod command)
mkdir -p
Create parent directories as necessary. When this option is used, no error is reported if a
specified DIRECTORY already exists.
OPTIONS-
rmdir –p
-p, --parents remove DIRECTORY and its ancestors
2 ways-
1. rmdir ABC/abc ABC
2. rmdir –p ABC/abc
OPTIONS-
1. –l
To count no. of lines
2. –w
To count no. of words
3. –c
To count no. of characters
When used with multiple files, wc produces a line for each file as well as total count.
20. Sort:
Helpful to sort/order lines in text files. You can sort the data in text file and display the
output on the screen, or redirect it to a file. Based on your requirement, sort provides
several command line options for sorting data in a text file.
$ cat test
zzz
sss
qqq
aaa
BBB
ddd
AAA
And, here is what you get when sort command is executed on this file without any
option. It sorts lines in test file and displays sorted output.
$ sort test
aaa
AAA
BBB
ddd
qqq
sss
zzz
$ cat test
22 zzz
33 sss
11 qqq
77 aaa
55 BBB
The following sort command sorts lines in test file on numeric value in first word of line
and displays sorted output.
$ sort -n test
11 qqq
22 zzz
33 sss
55 BBB
77 aaa
$ cat test
2K
2G
1K
6T
1T
1G
2M
The following sort command sorts human readable numbers (i.e 1K = 1 Thousand, 1M =
1 Million, 1G = 1 Giga, 1T = 1 Tera) in test file and displays sorted output.
$ sort -h test
1K
2K
2M
1G
2G
1T
6T
$ cat test
sept
aug
jan
oct
apr
feb
mar11
The following sort command sorts lines in test file as per month order. Note, lines in file
should contain at least 3 character name of month name at start of line (e.g. jan, feb,
mar). If we will give, ja for January or au for August, then sort command would not
consider it as month name.
$ sort -M test
jan
feb
mar11
apr
aug
sept
oct
If we want to check data in text file is sorted or not, then we can use -c or –check, –
check=diagnose-first option.
Create the following test file for this example:
$ cat test
The following sort command checks whether text file data is sorted or not. If it is not,
then it shows first occurrence with line number and disordered value.
$ sort -c test
5. Reverse the Output and Check for Uniqueness using -r and -u options
The following sort command sorts lines in test file in reverse order and displays sorted
output.
$ sort -r test
1
The following sort command sorts lines in test file in reverse order and removes
duplicate lines from sorted output.
$ sort -r -u test
6. Selectively Sort the Content, Customize delimiter, Write output to a file using -k, -t,
-o options
If we want to sort on the column or word position in lines of text file, then “-k” option
can be used. If we each word in each line of file is separated by delimiter except ‘space’,
then we can specify delimiter using “-t” option. We can get sorted output in any
specified output file (using “-o” option) instead of displaying output on standard output.
$ cat test
aa aa zz
aa aa ff
aa aa tt
aa aa kk
The following sort command sorts lines in test file on the 3rd word of each line and
displays sorted output.
aa aa ff
aa aa kk
aa aa tt
aa aa zz
$ cat test
aa|5a|zz
aa|2a|ff
aa|1a|tt
aa|3a|kk
Here, several options are used altogether. In test file, words in each line are separated by
delimiter ‘|’. It sorts lines in test file on the 2nd word of each line on the basis of numeric
value and stores sorted output into specified output file.
aa|1a|tt
aa|2a|ff
aa|3a|kk
aa|5a|zz
23. grep : The grep utility searches text files for a pattern and prints all lines
that contain that pattern.
grep:
SYNOPSIS
[filename]...
DESCRIPTION
General syntax :
grep –options pattern filename
OPTIONS
tern.
ons.
once.
-n Precedes each line by its line number in the file
tern.
test.txt test.txt
24. egrep (grep -E):
expressions .it can be used to search more than one pattern at a time.
SYNOPSIS
Options:
this command will first read input file and then the content of this file will be considered as pattern and
then this pattern will be searched in file.
-e : What if a pattern has a leading ‘-’? in that case –e option is useful.
ter string and prints all lines that contain that string. it
Miscellaneous:
grep ^$ *
Rather we have to use Ctrl+v+v (for ^V) and Ctrl+v+m (for ^M), see below results:
3) We also have –A , –B , –C options but it works only in Linux not in unix box.
25. Awk
Awk is one of the most powerful tools in Unix used for processing the rows and columns in a file.
Awk has built in string functions and associative arrays. Awk supports most of the operators,
conditional blocks, and loops available in C language.
One of the good things is that you can convert Awk scripts into Perl scripts using a2p utility.
Here the actions in the begin block are performed before processing the file and the actions in the
end block are performed after processing the file. The rest of the actions are performed while
processing the file.
Examples:
Create a file input_file with the following data. This file can be easily created using the output of ls -l.
From the data, you can observe that this file has rows and columns. The rows are separated by a
new line character and the columns are separated by a space characters. We will use this file as the
input for the examples discussed here.
Here $1 has a meaning. $1, $2, $3... represents the first, second, third columns... in a row
respectively. This awk command will print the first column in each row as shown below.
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
To print the 4th and 6th columns in a file use awk '{print $4,$5}' input_file
Here the Begin and End blocks are not used in awk. So, the print command will be executed for
each row it reads from the file. In the next example we will see how to use the Begin and End blocks.
This will prints the sum of the value in the 5th column. In the Begin block the variable sum is
assigned with value 0. In the next block the value of 5th column is added to the sum variable. This
addition of the 5th column to the sum variable repeats for every row it processed. When all the rows
are processed the sum variable will hold the sum of the values in the 5th column. This value is
printed in the End block.
3. In this example we will see how to execute the awk script written in a file. Create a file
sum_column and paste the below script in that file
#!/usr/bin/awk -f
BEGIN {sum=0}
{sum=sum+$5}
This will run the script in sum_column file and displays the sum of the 5th column in the input_file.
This will print the squares of first numbers from 1 to 5. The output of the command is
square of 1 is 1
square of 2 is 4
square of 3 is 9
square of 4 is 16
square of 5 is 25
Notice that the syntax of “if” and “for” are similar to the C language.
You have already seen $0, $1, $2... which prints the entire line, first column, second column...
respectively. Now we will see other built in variables with examples.
So far, we have seen the fields separted by a space character. By default Awk assumes that fields in
a file are separted by space characters. If the fields in the file are separted by any other character,
we can use the FS variable to tell about the delimiter.
15 t1
38 t2
38 t3
39 t4
39 t5
By default whenever we printed the fields using the print statement the fields are displayed with
space character as delimiter. For example
center 0
center 17
center 26
center 25
center 43
center 48
center:17
center:26
center:25
center:43
center:48
Note: print $4,$5 and print $4$5 will not work the same way. The first one displays the output with
space as delimiter. The second one displays the output without any delimiter.
index(string,search)
length(string)
split(string,array,separator)
substr(string,position)
substr(string,position,max)
tolower(string)
toupper(string)
Advanced Examples:
The awk split function splits a string into an array using the delimiter.
The syntax of split function is
split(string, array, delimiter)
Now we will see how to filter the lines using the split function with an example.
1 U,N,UNIX,000
2 N,P,SHELL,111
3 I,M,UNIX,222
4 X,Y,BASH,333
5 P,R,SCRIPT,444
Required output: Now we have to print only the lines in which whose 2nd column has the string
"UNIX" as the 3rd field( The 2nd filed in the line is separated by comma delimiter ).
The ouptut is:
1 U,N,UNIX,000
3 I,M,UNIX,222
awk '{
split($2,arr,",");
if(arr[3] == "UNIX")
print $0
} ' file.txt