OS Unit 1 Linux Commands
OS Unit 1 Linux Commands
commands
08/06/11
Understanding the Linux File System
2
Understanding the Standard Tree Structure
3
Vishal
Kaushal
File System
1. / – Root
– Every single file and directory starts from the root directory.
– Only root user has write privilege under this directory.
– Please note that /root is root user’s home directory
2. /bin – User Binaries
– Contains binary executables.
– Common linux commands you need to use in single-user modes are
located under this directory.
– Commands used by all the users of the system are located here.
– For example: ps, ls, ping, grep, cp.
3. /sbin – System Binaries
– Just like /bin, /sbin also contains binary executables.
– But, the linux commands located under this directory are used typically
by system aministrator, for system maintenance purpose.
– For example: iptables, reboot, fdisk, ifconfig, swapon
4. /etc – Configuration Files
– Contains configuration files required by all programs.
– This also contains startup and shutdown shell scripts used to start/stop
individual programs.
– For example: /etc/resolv.conf, /etc/logrotate.conf
5. /dev – Device Files
– Contains device files.
– These include terminal devices, usb, or any device attached to the
system.
– For example: /dev/tty1, /dev/usbmon0
6. /proc – Process Information
– Contains information about system processes.
– It contains runtime system info like: system memory, devices mounted,
hardware configuration, etc.
7. /var – Variable Files
– var stands for variable files.
– Content of the files that are expected to grow can be found
under this directory.
– This includes — system log files (/var/log); packages and
database files (/var/lib); emails (/var/mail); print queues
(/var/spool); lock files (/var/lock); temp files needed across
reboots (/var/tmp);
8. /tmp – Temporary Files
– Directory that contains temporary files created by system and
users.
– Files under this directory are deleted when system is rebooted.
9. /usr – all programs are installed here.
- /usr/local -The place for locally installed software and other files.
10. /home – Home Directories
– Home directories for all users to store their personal files.
– For example: /home/john, /home/nikita
11. /boot – Boot Loader Files
– Contains boot loader related files.
– Kernel initrd, vmlinuz, grub files are located under /boot
– For example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-
generic
12. /lib – System Libraries
– Contains library files that supports the binaries located under
/bin and /sbin
– Library filenames are either ld* or lib*.so.*
– For example: ld-2.11.1.so, libncurses.so.5.7
13. /opt – Optional add-on Applications
– opt stands for optional.
– Contains add-on applications from individual vendors.
– add-on applications should be installed under either /opt/ or
/opt/ sub-directory.
– Google chrome need to be installed separately.
14. /mnt – Mount Directory
– Temporary mount directory where sysadmins can mount
filesystems.
08/06/11
Logging
• You are now logged into the computer and will have a
prompt that reflects the computer's name.
sammy@webapp:~$
10
Every unix command is a sequence of letters,
numbers and characters. But there are no
spaces.
It is also case-sensitive. This means that cat
and Cat are different commands.
The prompt is displayed by a special program
called the shell.
Shells accept commands, and run those
commands.
They can also be programmed in their own
language. These programs are called “shell
scripts”.
11
When you first login, the prompt is
displayed by bash, and you are
running your first unix program, the
bash shell.
As long as you are logged in, the bash
shell will constantly be running.
12
13
Each command performs [variations of] a
single task
◦ “options” can be used to modify what a command does
◦ different commands can be “glued together” to perform
more complex tasks
Syntax: Options can (usually)
command options arguments be combined
together:
Examples: these are equivalent
16
'ls -al' gives detailed information of the
files.
Help for commands
19
Figuring out how to use a command
man command
“displays the on-line manual pages”
20
items within
square brackets
are optional
To exit
Press “q” 21
22
Logging In
• To log in to a Unix machine you can either:
◼ sit at the console (the computer itself)
◼ access remotely, via SSH, e.g.
OS linux X ls who
Pathnames
• The pathname of a file includes the file name and the
name of the directory that holds the file, and the name
of the directory that holds the directory that holds the
file, and the name of the … up to the root
• The pathname of every file in a given filesystem is
unique.
• To create a pathname you start at the root (so you start
with "/"), then follow the path down the hierarchy
(including each directory name) and you end with the
filename.
• In between every directory name you put a "/".
Relative Pathnames
Prefixed w/the current directory, $PWD
So, relative to the current working
directory
$ cd /home/VIT
$ pwd
/home/VIT
$ ls linux/Syllabus
linux/Syllabus
$ ls X
ls: X: No such file or directory
$ ls /home/student/X
/home/student/X
Special Relative paths…
. – The current directory
.. – The parent directory
$ pwd
/home/VIT
$ ls ./os
./os
$ ls ../student
X
Commands for Traversing
Filesystem
ls – lists contents of a directory
◼-a – all files
◼-l – long listing
pwd – print working (current)
directory
cd – change directory
◼w/out argument, takes you home
Listing files (ls)
• It shows the files /directories in your current directory.
• OPTIONS:
-l Lists all the files, directories and their mode, Number of links,
owner of the file, file size, Modified date and time and filename.
• For example,
sariyer:~> passwd
Changing password for dag.
Old password:
New passwd:
Retype new passwd:
sariyer:~>
37
Basic Commands (cont)
File & Directory Related Commands
1. cp <fromfile> <tofile>: Copy from the <fromfile> to the <tofile>
2. mv <fromfile> <tofile> : Move/rename the <fromfile> to the
<tofile>
3. rm <file>:Remove the file named <file>
4. mkdir <newdir>:Make a new directory called <newdir>
5. rmdir <dir>:Remove an (empty) directory
6. cd <dir> :Change the current working directory to dir
7. pwd : Print (display) the working directory
8. cat > <file> :To create new file and save it by pressing ^d
9. cat >> <file>: To append contents into existing file
10. cat <file>:To see the contents of existing file
11. more <file>:Paging out the contents of file
Basic Commands (cont)
12. file <file>:To check the type of file
13. wc <file>:To count lines,words,charaters of file
14. cmp <file1> <file2>:To compare two files
15. comm <file1> <file2>:To display common values between two
files
16. diff <file1> <file2>:To convert one file to another
17. gzip <file>:To compress the file
18. gunzip <file>:To unzip the contents of zipped file
19. ls :List the files in the current working directory
20. ls <dir>:List all files & directories in given directory
21. ln <fromfile><tofile>: Creates a symbolic link to a file
Advance commands
1. pr <file> :Paginating the file
Ex: pr –h “test” –d –n fname
2. head <file>:Display first 10 lines of file
Ex: head –n -3 fname
3. tail <file> :To display last 10 lines of file
Ex: tail -3 fname ; tail –c 100 fname
4. cut <file> :Splitting file vertically
Ex: cut –c 2-10,12-14 fname
cut –d “|” –f 2,4 fname
5. paste <file1> <file2> :To combine two file vertically rather than
horizontally
Ex: paste –d “|” fname1 fname2
6. sort <file>:To sort file in order by field wise
Ex: sort –t”|” –k 2 fname
sort –r fname
Advance commands
7. uniq <file> :Locate repeated & nonrepeated lines
Ex: uniq fname; uniq –d fname
8. tr ch1 ch2 < <file1>:To translate occurrence of ch1 by ch2
Ex: tr ‘|’ ‘+’ < fname1
9. tee: read from standard input and write to standard output and
files
Ex: ls *.txt | wc -l | tee count.txt
Some of the things these commands manipulate:
42
File Time Attributes
Time Attributes:
◼ when the file was last changed ls -l
◼ sort by modification time ls -lt
Types of Users
access permissions
− normal file
file type d directory
l (ell) symbolic link
47
File access permissions
48
File Permissions
Each file has a set of permissions
that control who can mess with
the file.
There are three types of
permissions:
◼ read abbreviated r
◼ write abbreviated w
◼ execute abbreviated x
There are 3 sets of permission:
1. user
2. group
3. other (the world, everybody else)
ls -l and permissions
-rwxrwxrwx
User Group Others
Type of file:
- – plain file
d – directory
b-block device file
c- character device file
l – symbolic link
p- pipe
s- socket
rwx
Files:
◼ r - allowed to read.
◼ w - allowed to write
◼ x - allowed to execute
Directories:
◼ r - allowed to see the names of the file.
◼ w - allowed to add and remove files.
◼ x - allowed to enter the directory
Access Types
Changing Permissions
The chmod command changes the
permissions associated with a file
or directory.
There are a number of forms of
chmod, this is the simplest:
chmod mode file
chmod – numeric modes
Consider permission for each set of
users (user, group, other) as a 3-bit #
◼ r–4
◼ w–2
◼ x–1
A permission (mode) for all 3 classes is
a 3-digit octal #
◼ 755 – rwxr-xr-x
◼ 644 – rw-r—r--
◼ 700 – rwx------
Changing file access privileges
(cont…)
chmod - examples
$ chmod 700 CS571
$ ls –o Personal
drwx------ 10 kschmidt 4096 Dec 19 2004 CS571/
$ chmod 755 public_html
$ chmod 644 public_html/index.html
$ ls –ao public_html
drwxr-xr-x 16 kschmidt 4096 Jan 8 10:15 .
drwx--x--x 92 kschmidt 8192 Jan 8 13:36 ..
-rw-r--r-- 5 kschmidt 151 Nov 16 19:18 index.html
$ chmod 644 .plan
$ ls –o .plan
-rw-r--r-- 5 kschmidt 151 Nov 16 19:18 .plan
chmod – symbolic modes
Can be used to set, add, or
remove permissions
Mode has the following form:
[ugoa][+-=][rwx]
◼ u – user g – group o – other a – all
◼ + add permission - remove permission =
set permission
chmod examples
$ ls -al foo
-rwxrwx--x 1 hollingd grads foo
$ chmod u-r .
$ ls
ls: .: Permission denied
Chown and chgrp: Changing File
Ownership
# ls -lart tmpfile
-rw-r--r-- 1 himanshu family 0 2012-05-22
20:03 tmpfile
# chown root tmpfile
# ls -l tmpfile
-rw-r--r-- 1 root family 0 2012-05-22
20:03 tmpfile
Change the group
# ls -l tmpfile
-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile
# chown :friends tmpfile
# ls -l tmpfile
-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile
# ls -l tmpfile
-rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile
# chown himanshu:friends tmpfile
# ls -l tmpfile
-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03
tmpfile
Using chown command on symbolic link file
ls -l tmpfile_symlnk
lrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile
Using flag ‘-h’, you can forcefully change the owner or group of a symbolic link as
shown below.
# ls -l tmpfile_symlnk
lrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile
# chown -h root:friends tmpfile_symlnk
# ls -l tmpfile_symlnk
lrwxrwxrwx 1 root friends 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile
umask: Default File Permissions
The default permissions are inherited by files and directories
created by all users:
◦ rw-rw-rw- (octal 666) for regular files
◦ rwxrwxrwx (octal 777) for directories
However, these are not the permissions you see. This default is
transformed by subtracting the user mask from it to remove one
or more permissions.
This mask is evaluated by using umask:
$ umask
student@unix-13:~$
umask
0022
student@unix-13:~$
umask: Default File Permissions
$ umask
77
This is an octal number, and subtracting this value from the file
default yields 666 – 077 = 600.
This represents the default permissions (rw-------) when you
create a file.
The default directory permissions are set (rwx------) when a
directory is created.
Filter and Redirection:
Pipes – connecting processes
A pipe is a holder for a stream of
data.
A pipe can be used to hold the
output of one program and feed it
to the input of another.
prog1 prog2
STDOUT STDIN
Asking for a pipe
Separate 2 commands with the “|”
character.
The shell does all the work!
ls -1 | sort
ls -1 | sort > sortedlist
ls -1 | sort | head > top.ten
filters
Programs that read some input (but
don’t change it), perform a simple
transformation on it, and write some
output (to stdout)
Some common filters…
◼ wc – word count (line count, character count)
◼ tr – translate
◼ grep, egrep – search files using regular
expressions
◼ sort – sorts files by line (lexically or numerically)
◼ cut – select portions of a line
◼ uniq – Removes identical adjacent lines
◼ head, tail – displays first (last) n lines of a file
pipes and combining filters
Connect the output of one command to
the input of another command to obtain
a composition of filters
who | wc -l
ls | sort -f
ls -s | sort -n
ls -l | sort -nr -k4
ls -l | grep ‘^d’
• pr
Ex pr –h “test” –d –n fname
⚫ sort –r fname
• cut
⚫ paste <file1> <file2> :To combine two file vertically rather than horizontally
⚫ Ex head –n -3 fname
• tail
⚫ Ex tail -3 fname ;
⚫ tee: read from standard input and write to standard output and files
admin40@admin40-OptiPlex-360:~$ cat
>test
this is test file
admin40@admin40-OptiPlex-360:~$ tr '[a-z]'
'[A-Z]' <test
THIS IS TEST FILE 80
Redirection
1. Standard input redirection: It is used to redirect standard input.
Ex. cat < fname
2. Standard output redirection : It is used to redirect standard
output.
Ex cat >fname
3. Standard error redirection: It is used to redirect standard error.
Ex cat fname 2>Errorfile
Grep: Global Regular Expression Print
• Searching and pattern matching tools
• Searches files for one or more pattern arguments. It does plain
string, basic regular expression, and extended regular expression
searching
• Following are some of the options for grep
-i ignore case for matching
-v doesn’t display lines matching expression
-n display line numbers along of occurrences
-c counting number of occurrences
-l display list of file names
-e exp for matching
-f file take patterns from file
-E treat pattern as an extended reg. exp
-F matches multiple fixed strings (fgrep)
Grep: Global Regular Expression Print
08/06/11
Programming Tools and Utilities
Available under Linux
• Text Editors • Debuggers
– Xemacs – C / C++ debugger - gdb
– Emacs • Interpreters
– Pico – Perl - perl
– vi – Tcl/Tk - tcl & wish
• Compilers • Miscellaneous
– C compiler - gcc – Web Browsers - Mozilla,
– C++ compiler - g++ Netscape, Firefox, and
– Java compiler & Java Lynx (lynx is text based)
Virtual Machine - javac & – Instant Messengers - Gaim
java – Email - Netscape is there,
but we will learn Pine
Try following commnads
• compgen
• fg
• Bg
• Zcat
• du
• df
• touch
• which
08/06/11