Professional Documents
Culture Documents
Revathi J
Bharathidasan University Technology Park (BUTP)
Bharathidasan University
Standard Files
• Linux provides three I/O channels to processes.
▫ Standard input, Standard output & Standard error.
Keyboard
Program
Standard Error
Standard I/O and Pipes
• Linux supports shell redirection operators allow
standard I/O channels to be redirected to or from
a file.
• Many linux commands, like cat will take their input from a
file if one is given as an argument.
Examples
$ cat test.txt
$ cat < test.txt
• Some commands are operate on standard input,
like tr command
Example :
$ ls -l > outputfile
Example
$ find / -name resolv.conf 2> errorfile
$ cat errorfile
Pipe | command
• This symbol is like two vertical slashes, one on top
of the other.
Example
$ ls -l /proc
$ ls -l /proc | more
• Scroll down with the ENTER key
$ ls -l /proc | less
• Scroll with arrows/pgup/pgdown
Tee command
• Lets you tee a pipe: redirect output to a file when
still piping it to another program.
Example
$ set | tee set.out | less
▫ The output from set is written to file set.out while
also piped to less
Example
$ <cmd> | tee file1.out | sort | tee file2.out
Cat Command
• Contents of the files are displayed sequentially
with no break.
Syntax
$ cat [options] [file...]
Example
$ cat /etc/profile
$ cat /etc/profile | /etc/passwd >> output.txt
$ cat /etc/profile | tee out.txt
The Linux Security Model
• Users and groups are used to control
access to file and resources.
• Users log in to the system by supplying
their user name and password.
• Every file on a Linux system is owned by a
user and associated with a group.
• Every process has an owner and group
affiliation, and can only access the
resources its owner or group can access
Users
• Every person that logs into the computer is
considered to be a user.
• Every user has a unique User ID number (the uid)
- no permission
Examining Permissions
• File permissions may be viewed using
▫ $ ls -l /bin/login
-rwxr-x-r-x 1 root root 19080 Apr 1 18:30 /bin/login
▫ $ ls -l /bin
drwxr-x-r-x 2 root root 4096 Apr 15 18:30 /bin
• sysadmin:x:601:user1,user2
Changing your identity
• The su command is used to change identities.
Example,
Find out who you are
whoami
Find out what groups you belong to
groups, id
Find out who is logged in
users, who, w
Find login/reboot history
last
Default permissions and Umask
Default permission for files is 666
Default permission for directories is 777
umask is subtractor from default to determine new
file/directory permissions
non-privileged user’s umask is 002
files will have permissions of 664
directories will have permissions of 775
root’s umask is 022
Special permissions
• A fourth permission set (in addition to
user/group/other)
• Applicable in four cases:
▫ Set user ID or suid for an executable
▫ Set group ID or sgid for an executable
▫ Set group ID or sgid for a directory
▫ Sticky bit for a directory
• Use the chmod command, preceding the usual
three digits with a digit representing the
special permission :
▫ 4 for suid, 2 for sgid, 1 for the sticky bit.
• Example:
# chmod 3775 groupdir
Special Permissions for Executables
• The Set UID Permission
$ chmod 2770
$ ls –l groupdir
drwxrws- - - 2 john eric 4089 Mar 9 10:20 groupdir
$ ls - li password /etc/passwd
30338 -rw-r--r-- 1 root root 1729 Aug 24 11:43 /etc/passwd
30376 lrwxrwxrwx 1 ram ram 11 Aug 24 10:43 password /etc/passwd
Hard links
• One physical file on the filesystem
• $ df -h /opt
Filesytem Size Used Available Use% Mounted on
/dev/hda7 16G 4.9G 11G 20% /opt
du command
• du – reports disk space usage
▫ Reports kilobytes used per directory
▫ Includes subtotals for each subdirectory
▫ -s option only reports single directory summay
▫ Also takes -h option
Example :
$ du -s /dev
11630 /dev
Removable media
• “Mounting” means making a foreign filesystem
look like part of the main tree
• Before accessing, media must be mounted.
# gzip –v file
# gunzip file.gz
• bzip2, bunzip2
▫ newer Linux compression utility
▫ Generally archieves better compression than gzip.
# bzip2 -v file
# bunzip2 file.bz2
String Processing - head
• The head command displays first few lines of the
text in a file. (i.e) it display from top of the file.
Example:
# head –n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:
daemon:x:2:2:daemon:/sbin:
• -n or --lines specifies the number of lines to
display.
tail
• The tail command displays last few lines of the
text in a file. (i.e) it display from bottom of the
file.
Example:
# tail -n 2 /etc/passwd
ntp:x:38:38: :/etc/ntp:/sbin/nologin
gdm:x:42:42: :/var/gdm:/sbin/nologin
• -n or --lines specifies the number of lines to
display.
wc
• wc - word count is used to count lines and
characters.
Example:
# wc text.txt
20 200 1500 text.txt
l - for line count
w - for word count
c - for character count
grep
• grep – general regular expression processor,
displays the lines in a file that match a pattern. It
can also process standard input.
# ls | grep d*
# grep ‘[Dd]og’ testfile
-it displays either the string “Dog” or
the string “dog” from the file testfile.
find
• Finds one or more files assuming that you know
their approximate filenames.
• Syntax:
$ find <location> -name <filename>
Examples:
$ find / -name 'myhome.html'
In the above command the system would search for
any file named myhome.html in the current
directory and any subdirectory
Investigating and Managing Processes
• Process :
It is an executing program with several components and
properties.
• pstree shows the process ancestry for all process
running on the system.
# pstree
• ps – displays processes information
Viewing Processes
• ps – displays processes information
• # ps -[option]
# at <time>
• Example
# at 8:30pm July 10
> ls –l
<ctrl –d>
# atq // list the current at jobs
pending.
# atrm [jobID] // used to remove pending
at jobs
To see the job result,
• # mail // used to view the executed
jobs
Scheduling Periodic Processes
• cron
Querying Packages
• Using the query options (-q) of rpm, you can get
information about RMP package.
# rpm -qa
list all the installed packages
Thank You
Command list continued ....
For this Purpose Use this vi command Syntax
Go to the first line in the file ESC +g
Go to the last line in the file ESC +G
Go to first line in file(any number can be given ESC +1G or H
and cursor will jump to that line)
Go to middle line on screen ESC +M
Go to last line on screen ESC +L
Go forward one full screen <Ctrl –f>
Go back one full screen <Ctrl –b>
Go down half a screen <Ctrl –d>
Go up half a screen <Ctrl –u>
To Find all occurrence of given word and ESC +:$s/word-to-find/word-to-replace/g
Replace then globally without confirmation
To Find all occurrence of given word and ESC +:$s/word-to-find/word-to-replace/cg
Replace then globally with confirmation
To run shell command like ls, cp or date etcESC +:!shell-command For e.g. :!pwd
within vi
Inodes
• An inode table contains a list of all files in an ext2
or ext3 filesystem.
• An inode (index node) is an entry in the
table, containing information about a file
(the meta data), including :
▫ File type, permissions, link count, UID, GID
▫ The file's size and various time stamps
▫ Pointers to the file's data blocks on disk
▫ Other data about the file
Inode - example
• $ ls -il
80777 -rw-r----- 1 ram ram 5120 Sep 11 12:50 txtfile
59907 drwxrw---- 2 ram ram 4098 Sep 11 12:50 data
80787 -rw-r----- 1 ram ram 1500 Sep 11 12:52 file
Example:
$ ls - li txtfile
24567 -rw-rw-r-- 1 ram ram 26 Sep 25 20:16 txtfile
$ cp txtfile new
24567 -rw-rw-r-- 1 ram ram 26 Sep 25 20:16 txtfile
24568 -rw-rw-r-- 1 ram ram 26 Sep 25 20:16 new
mv and inode
• When a file is moved, the inode number remains
the same.
• The data on the file system is not moved.
Example:
$ ls - li txtfile
24568 -rw-rw-r-- 1 ram ram 26 Sep 25 20:16 txtfile
$ mv txtfile new
$ ls - li txtfile new
24568 -rw-rw-r-- 1 ram ram 26 Sep 25 20:16 new
rm and inodes
• The rm command :
▫ Decrements the link count, thus freeing the inode
number to be reused
▫ Places data blocks on the free list
▫ Removes the directory entry
Example:
# cut -f3 -d: /etc/passwd
Will display a list of uids from /etc/passwd,
because uids are stored in field three and “:” is
used as a delimiter.
# uniq uniqfile
Sunday
Monday
Tuesday
Friday
Sort
• Sorts text to stdout – original file unchanged
• By default sort sorts the file in ascending order using
the entire line as a sorting key.
Syntax:
# sort [option] filename
-r reverses sort
-n numeric sort
-u unique (remove duplicate lines in output)
tr
• tr is used to translate characters; that is given two
ranges of characters, any time a character in
range 1 is found, it is translated into the
equivalent character in range 2.
# look le
Leave
Letter
Lead
Least
….
Regular Expressions
• Regular expressions are pattern matching engine
• sed - stream editor. It will perform a search and
replace on all lines in the file, sending the modified
data to standard output; that is, it does not actually
modity the existing file.