Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Linux Shell Commands

Uploaded by

elon musk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Linux Shell Commands

Uploaded by

elon musk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Linux 

Shell Commands
Introduction

ƒ Many people says that Linux  is a command based 
operating system.
ƒ So many of us thinks that Linux is not so user friendly 
OS.
ƒ But it is not true. Linux is a GUI based OS with a Shell 
which is more powerful than its counter part in Windows 
OS.
ƒ We will be familiar with some shell commands.
Identity

ƒ Type uname and Linux will tell his name to you
ƒ If you want to know your name type whoami
Manual

ƒ For each command Linux contains manual.  To view the 
manual :  man name
ƒ man uname
Editors

ƒ To view files a large number of editors are available. 
They are:
ƒ kwrite
ƒ emacs
ƒ gedit
ƒ vi 
ƒ To view :  editorname  filename
ƒ kwrite file.txt
User

ƒ In Linux , root is the most powerful user. But other users 
can be created easily. Each linux user must be under 
certain group.
ƒ To add a group : groupadd group1
ƒ To delete a group : groupdel group1
ƒ To add a user : useradd –g groupname username
ƒ To delete a user : userdel username
ƒ To change a user : su user1
ƒ To update the passwd : passwd user1
View Text

ƒ To view a line of text in the shell:  echo
ƒ echo ‘welcome to linux’
ƒ To clear the shell : clear
Directory and File Permissions

ƒ Each file or directory has 3 security groups.
ƒ Owner 
ƒ Group 
ƒ All Others 
ƒ Each security group has 3 flags that control the access 
status : read, write, execute
ƒ They are listed as 'rwx' or a "‐" if the access is turned off.
ƒ rwxrwxrwx [read, write and executable for owner, 
group and all others]
ƒ rw‐r‐‐r‐‐ [read and write by owner, read only for 
group and all others]
Directory and File Permissions

ƒ To change the permissions type chmod
ƒ u, g, o or all [whose permission you are changing ]
ƒ + or ‐ [ type of change: add or subtract permission] 
ƒ combination of r , w or x [ which permission you are 
changing: read, write or execute ]
ƒ file or directory [name of file or directory to change]
ƒ chmod go+rw file1 file2 add read and write 
access for group and others for files 'file1' and 
'file2’
ƒ chmod a+rwx file1  add read, write and execute 
for   everyone for 'file1‘.
ƒ chmod 555 file1 
Directory and File Permissions

ƒ To change the owner of a file or directory type chown.
ƒ chown username <file or directory>
ƒ chown user1 file

ƒ To change the group of a file or directory type chgrp.
ƒ chgrp groupname <file or directory>
ƒ chgrp group1 file1 file2
Directory and File Listings

ƒ To list information about directory or files : ls
ƒ This command contains some options.
ƒ ‐a [ do not hide entries starting with . ]
ƒ ‐A [ do not list implied . and ..]
ƒ ‐h [ print sizes in human readable format ]
ƒ ‐l [ use a long listing format ]
ƒ ‐S [ sort by file size ]
ƒ Permissions.Directories.Owner.Group. Size. Date. Name
drwx‐‐‐rwx .    2  .  oracle . oinstall .  1206 . Jan 22 15:10 . a
Directory Operations

ƒ To print the current directory : pwd
ƒ To change the current directory : cd dirname
ƒ The variable HOME is the default directory.
ƒ To make a new directory : mkdir
ƒ ‐m [set permission mode (as in chmod)]
ƒ ‐v   [print a message for each created directory
ƒ To delete an empty directory : rmdir
Directory Operations

ƒ To move to a directory pushing the current directory to 
stack : pushd dirname 
ƒ Effect:
ƒ adds  a directory to the top of the directory stack 
ƒ or rotates the stack making the new top of the stack 
the current working directory
Directory Operations

ƒ To moves to the directory at the top of the stack as well 
as to remove the topmost entry : popd
ƒ Effect:
ƒ removes the top directory from the stack
ƒ performs a cd to the new top directory.
Directory Operations

ƒ To display the list of currently remembered directories : 
dirs
ƒ The default display is on a single line with directory 
names separated by spaces.
ƒ How to add to the list : pushd
ƒ How to remove from the list : popd
File Operations

ƒ To copy a file : cp
ƒ Copy source to destination or multiple sources to 
directory
ƒ ‐i [ prompt before overwrite ]
ƒ ‐r [ copy directories recursively ]
ƒ ‐u [ copy only when the src file is newer than the dest 
file or when the dest file is missing
File Operations

ƒ To remove a file or directory : rm
ƒ ‐f [ignore nonexistent files, never prompt ]
ƒ ‐i [ prompt before any removal ]
ƒ ‐r [ remove the contents of directories recursively ]
ƒ ‐v [ explain what is being done ]
File Operations

ƒ To move or rename a file : mv
ƒ rename src to dest or move src(s) to directory 
ƒ ‐i  [ prompt before overwrite ]
ƒ ‐u [ move only when the src file is newer than the dest 
file or when the dest file is missing
ƒ ‐v [ explain what is being done ]
File Operations

ƒ To determine file type : file filename 
ƒ File tests each argument in an attempt to classify it. This 
causes the file type to be printed
ƒ ‐ i [ show the mime type].
ƒ ‐v [ Print the version of the file]
ƒ file a.txt : a.txt: very short file
ƒ file a.xls : a.xls: Microsoft Office Document
ƒ file ‐i a.xls : a.xls: \012‐ application/msword
File Operations

ƒ To concat files and print on the standard output : cat  
file1 file2 file3 … 
ƒ ‐n [ number all output lines ]
ƒ ‐s [ never more than one single blank line ]
File Viewing

ƒ To view files in shell use: more or less.
ƒ more filename
ƒ less filename
ƒ The main difference between more and less is that 
ƒ less allows backward and forward movement using 
the arrow keys.
ƒ more only uses the [Spacebar] and the [B] key for 
forward and backward navigation.
File Viewing

ƒ To output the first lines of files : head  file1 file2 file3 … 
ƒ Print the first 10 lines of each file to standard output
ƒ With more than one file , precede each with a header 
giving the file name
ƒ ‐n [ output the first n lines, instead of the first 10 ]
File Viewing

ƒ To output the last lines of files : tail  file1 file2 file3 … 
ƒ Print the last 10 lines of each file to standard output
ƒ With more than one file, precede each with a header 
giving the file name
ƒ ‐n [ output the last n lines, instead of the last 10 ]
File Viewing

ƒ To sort lines of a text files : sort file1 file2 file3…
ƒ Write sorted concatenation of all file(s) to standard 
output.
File Viewing

ƒ To print the number of lines, words and bytes in files : 
wc  file1 file2 file3 … 
ƒ print byte, word, and newline counts for each file and a 
total line if more than one file is specified.
ƒ ‐l  [ print the newline counts ]
ƒ ‐w [ print the word counts ]
Standard I/O/E

ƒ By default, three default files known as standard files 
are automatically opened when a command is executed.
ƒ They are standard input (stdin) ,standard output 
(stdout) and standard error (stderr).
ƒ For example, the command ls  ‐a scans the current 
directory and collects a list of all the files, produces a 
human readable list, and outputs the result to the 
terminal window.
Redirection

ƒ Linux redirection features can be used to detach the 
default files from stdin, stdout and stderr and attach 
other files to them.
ƒ Input redirection: 
ƒ <  ‐ get input from file instead of the keyboard
ƒ Output redirection:
ƒ >  ‐ send output to file instead of the terminal 
window
ƒ Append output:
ƒ >> ‐ command is used to append to a file if it already 
exists
Piping

ƒ The input of a command may come from the output of 
another command.
ƒ This is accomplished with the ‘ | ‘ pipe operator.

ƒ How to view the lines 15‐20 of  a file named ‘a.txt’ ?
Piping

ƒ The input of a command may come from the output of 
another command.
ƒ This is accomplished with the ‘ | ‘ pipe operator.

ƒ How to view the lines 15‐20 of  a file named ‘a.txt’ ?
ƒ head  ‐20 a.txt | tail ‐5 
Grep

ƒ grep matches a pattern in a given a list of files or 
standard input and outputs only the matching lines.
ƒ grep pattern filename
ƒ grep  abc file.txt
ƒ grep patterns are case sensitive by default.
ƒ Some options
ƒ ‐i [ case insensitive search ]
ƒ ‐c [count of total matches]
ƒ ‐E [regular expressions can be provided as patterns]
ƒ ‐n [display the line numbers of the matched lines]
Find
▪ Search for files in a directory hierarchy.
▪ By default, find returns all files below the current
working directory.
▪ find .
▪ To search a pattern : find . -name '*txt*'
▪ To search for a file type :
▪ find . -type d [find all directories]
▪ find . -type f [find all regular files]
▪ Find executes the '-print' action by default. To change it
to style such as ‘ls’ : find . -type f –ls
Find
▪ To search all the directories (not recommended)
▪ find / -name '*.java' -type f
▪ To search a specific directory
▪ find /home/dir1 -name '*.java' -type f
▪ To search multiple directories
▪ find dir1 dir2 -name '*.java' -type f
▪ To search for all files owned by a user
▪ find . -user userid
▪ To take an action
▪ find . -type f -name '*x*' -exec chmod a+rwx {} \;
▪ {} is replaced with the name of the file
▪ The ; indiates the end of the command.
Thanks

You might also like