2 B.
BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Linux Commands – Basics - 1
To view the present working directory:
[root@redhat ~]# pwd
/root
To display the current date and time of the machine:
[root@redhat ~]# date
Thu Aug 18 09:00:00 IST 2016
To display the current calendar of the machine:
[root@redhat ~]# cal
To list the files & directories in a present working directory:
[root@redhat ~]# ls
To long list the files & directories in the present working directory:
[root@redhat ~]# ls -l
To list the hidden files in a present working directory:
[root@redhat ~]# ls -a
To long list the particular directory:
[root@redhat ~]# ls –ld data
To view the file size in human readable format:
[root@redhat ~]# ls –lh
To list the directory recursively:
[root@redhat ~]# ls –R /data/
To view the inode number of files & directories:
[root@redhat ~]# ls -li
To list the files in modification time:
[root@redhat ~]# ls -lt
To display security context of the files & folders:
[root@redhat ~]# ls –Z /data/test
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 3
To create a new directory:
[root@redhat ~]# mkdir /test
To create a multiple directory using “-p” option:
[root@redhat ~]# mkdir –p /data/test/linux
To change the directory:
[root@redhat ~]# cd /test/
To check the present working directory:
[root@redhat ~]# pwd
/test
Go to the previous working directory:
[root@redhat ~]# cd ..
Now the present working directory is “ / ”:
[root@redhat ~]# pwd
/
To change the previous working directory:
[root@redhat ~]# cd –
/test
To change user home directory:
[root@redhat ~]# cd ~
To create the file:
[root@redhat ~]# cat > rec
Rajalakshmi Engineering College
Chennai
^d
To view the file:
[root@redhat ~]# cat rec
Rajalakshmi Engineering College
Chennai
To append the existing file:
[root@redhat ~]# cat >> rec
Tamil Nadu
India
4 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Appended file viewed here:
[root@redhat ~]# cat rec
Rajalakshmi Engineering College
Chennai
Tamil Nadu
India
To number all output lines in the file:
[root@redhat ~]# cat –n rec
1 Rajalakshmi Engineering College
2 Chennai
3 Tamil Nadu
4 India
To create a empty file:
[root@redhat ~]# touch rit
To remove the directory using this command:
[root@redhat ~]# rm –rvf test
To check the „test‟ directory removed :
[root@redhat ~]# ls
Used to display list of options available in mkdir:
[root@redhat ~]# mkdir --help
The mkdir options are listed below.
To view the general information about given command:
[root@redhat ~]# info ls
The info command todisplay the general information.
To view manual page & full information about given command:
[root@redhat ~]# man ls
The manual page of ls command.
It is used to copy the file without modifying the permission, time & date:
[root@redhat ~]# cp –a tom /root/
[root@redhat ~]# cd /root/
[root@redhat ~]# ls –l tom
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 5
To view the permission of the “tom” file:
[root@redhat ~]# chmod 777 tom
[root@redhat ~]# ls –l tom
-rwxrwxrwx. 1 root root 0 Aug 18 09:00:00 tom
It copied the “tom” file to “/mnt” with verbose:
[root@redhat ~]# cp –v tom /mnt/
‘tom’ -> ‘/mnt/tom’
The rhce is a folder so it omitting the directory to copy:
[root@redhat ~]# cp /rhce/ /mnt/
cp: omitting directory ‘/rhce/’
Use –r option to copy the directory in recursive manner:
[root@redhat ~]# cp –r /rhce/ /mnt/
To move or rename files or directories:
[root@redhat ~]# mv tom sam
Here the file “john” has been moved to “/ test” directory:
[root@redhat ~]# mv tom sam
[root@redhat ~]# ls
jerry sam
To determine the file type:
[root@redhat ~]# file install.log
install.log: ASCII text
6 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Standard Input & Output Errors
Output of “ls –l” stored to long file:
[root@redhat ~]# ls –l > long
Output is displayed.
Add few lines to it:
[root@redhat ~]# cat > file
REC
CSE
To save a file using ctrl+d
Error is displayed:
[root@redhat ~]# cat –ls –l > hi
cat: invalid option – ‘l’
Try ‘cat –help’ for more information.
Saving that error message:
[root@redhat ~]# cat ls –l 2> hi
Use 2> to save an error message to a file
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 7
Pipe line commands
‘/data’ directory contains three files:
[root@redhat ~]# ls –R /data/
To view output at stage by stage:
[root@redhat ~]# ls –lR /data/ | tee stage1
To view output at stage by stage.
8 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Text Processing Tools
To view first ten lines in the file:
[root@redhat ~]# head /etc/passwd
To view first “5” lines in the file:
[root@redhat ~]# head -5 /etc/passwd
To view last ten lines in the file:
[root@redhat ~]# tail /etc/passwd
To view last five lines in the file:
[root@redhat ~]# tail -5 /etc/passwd
To show updates to the file:
[root@redhat ~]# tailf /etc/passwd
To view a file one page at a time:
[root@redhat ~]# less /etc/passwd
Press “space” key, it moves ahead one full screen.
It shows first page of the file.
To view a big file little by little:
[root@redhat ~]# more /etc/passwd
This command to get the string pattern:
[root@redhat ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nlogin
This command to get the string pattern except given name:
[root@redhat ~]# grep –l tom /data/*
/data/blue
/data/gren
This command used to get the number of line it will presented:
[root@redhat ~]# grep –n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nlogin
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 9
This command used to count the no. of lines found the string pattern:
[root@redhat ~]# grep –c root /etc/passwd
2
This command is used to get the ROOT pattern from passwd file:
[root@redhat ~]# grep –i ROOT /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nlogin
This command used to count the lines, words & characters in that file:
[root@redhat ~]# wc /etc/passwd
47 76 2269 /etc/passwd
It display the number of lines for the /etc/passwd file.
It display the words for the /etc/passwd file.
It display the characters for the /etc/passwd file.
If we need no. of lines presented means by using this option “–l”.
[root@redhat ~]# wc –l /etc/passwd
47 /etc/passwd
If we need only no. of words presented means by using this option “–w”:
[root@redhat ~]# wc –w /etc/passwd
76 /etc/passwd
If we need no. of characters presented means by using this option “-c”:
[root@redhat ~]# wc –c /etc/passwd
2269 /etc/passwd
To view the file:
[root@redhat ~]# cat test
hi how are you
hi how are you
hi
1
2
8
9
hi how are you
10 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
To sort the text data:
[root@redhat ~]# sort test
1
2
8
9
hi
hi how are you
hi how are you
hi how are you
To perform a numeric sort:
[root@redhat ~]# sort –n test
hi
hi how are you
hi how are you
hi how are you
1
2
8
9
To perform a reverse (descending) sort:
[root@redhat ~]# sort –r test
hi how are you
hi how are you
hi how are you
hi
9
8
2
1
To ignores case of characters in strings:
[root@redhat ~]# sort –f test
1
2
8
9
hi
hi how are you
hi how are you
hi how are you
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 11
To remove duplicate lines in output:
[root@redhat ~]# sort –u test
1
2
8
9
hi
hi how are you
To view the file:
[root@redhat ~]# cat test
hi how are you
hi how are you
hi
1
2
8
9
hi how are you
Numbering the repeated lines:
[root@redhat ~]# uniq –c test
2 hi how are you
1 hi
1 1
1 2
1 8
1 9
1 hi how are you
To print the unique lines:
[root@redhat ~]# uniq –u test
hi
1
2
8
9
hi how are you
To see the content of both files name called as file & file1:
[root@redhat ~]# cat file
hai how are you...
[root@redhat ~]# cat file1
I am fine
here
12 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
This command used to differentiate two files:
[root@redhat ~]# diff file file1
1c1,2
< hai how are you...
---
> i am fine
> here
It shows difference of two files.
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 13
File Permission
Linux is a multi-user OS that is based on the Unix concepts of file ownership and
permissions to provide security, at the file system level. If you are planning improving your Linux
skills, it is essential that have a decent understanding of how ownership and permissions work.
There are many intricacies when dealing with file ownership and permissions.
Viewing Ownership and Permissions
In Linux, each and every file is owned by a single user and a single group, and has its own
access permissions. Let's look at how to view the ownership and permissions of a file.
The most common way to view the permissions of a file is to use ls with the long listing
option, e.g. ls -l myfile. If you want to view the permissions of all of the files in your current
directory, run the command without an argument, like this:
[root@redhat ~]# ls -l
[root@redhat ~]# ll
Understanding Mode
14 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Let's look at which users belong to each permissions class:
User (u) : The owner of a file belongs to this class.
Group (g) : The members of the file's group belong to this class.
Other (o) : Any users that are not part of the user or group classes belong to this class
Consider a file testfile :
[root@redhat ~]# ls -l testfile
Adding write and execute permission for others:
[root@redhat ~]# chmod o+wx testfile
Adding read and write permission to testfile for groups:
[root@redhat ~]# chmod g=rw testfile
Removing write permission to others, adding execute permission to users, read write and execute to
group:
[root@redhat ~]# chmod o-w,u+x,g=rwx testfile
Using chmod with Absolute Permissions:
Consider a file testfile:
[root@redhat ~]# ls -l testfile
[root@redhat ~]# chmod 775 testfile
users= rwx , groups= rwx, others= r-x
[root@redhat ~]# chmod 773 testfile
users= rwx , groups= rwx, others= -wx
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 15
[root@redhat ~]# chmod 043 testfile
users= ------, groups= r--, others= r-x
Consider a Directory:
[root@redhat ~]# ls –ld /share
[root@redhat ~]# chmod 775 /share
users= rwx , groups= rwx, others= r-x
[root@redhat ~]# chmod 773 /share
users= rwx , groups= rwx, others= -wx
[root@redhat ~]# #chmod 043 /share
users= ------, groups= r--, others= r-
Change Group and permission for /sym Directory:
Change ownership:
To add a new user & press enter:
[root@redhat ~]# useradd bhuvan
16 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
YUM
Installing And Managing Software
YUM (Yellowdog Updater Modified) is an open source command-line as well as graphical
based package management tool for RPM (RedHat Package Manager) based Linux systems. It
allows users and system administrator to easily install, update, remove or search software packages
on a systems. It was developed and released by Seth Vidal under GPL (General Public License) as
an open source, means anyone can be allowed to download and access the code to fix bugs and
develop customized packages. YUM uses numerous third party repositories to install packages
automatically by resolving their dependencies issues.
Basic modes for YUM command
#yum option packagename
Options:
Install : It is used to install any package.
Remove/Erase : It is used to erase, remove or un-Install any package.
Update : It is used to update the existing package.
To list packages by keywords:
[root@redhat ~]# yum search httpd
To give detailed information about the package:
[root@redhat ~]# yum info httpd
To clean temp:
[root@redhat ~]# yum clean all
To see available packages on repositories:
[root@redhat ~]# yum list all
To install a package:
[root@redhat ~]# yum install virt-manager
To update the package:
[root@redhat ~]# yum install update virt*
To remove the package:
[root@redhat ~]# yum remove virt*
To view the group packages:
[root@redhat ~]# yum grouplist
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 17
18 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai