3. Linux Directory Structure
/bin – User Binaries
Contains binary executables.
Common linux commands you need to use in single-
/sbin – System Binaries
user modes/bin,located under this directory.
Just like are /sbin also contains binary executables.
/etc – the linux commands located under this directory
But, Configuration Files
Commands used by all the users of the system are
Contains configuration files required by all programs.
located here.
are used typically by system aministrator, for system
/dev –also contains startup and shutdown shell scripts
Device Files
Thisexample: ps, ls, ping, grep, cp.
For
maintenance purpose.
Contains device individual programs.
files.
used example: iptables, reboot, fdisk, ifconfig, swapon
Forto start/stop Information
/proc – Process
These include terminal devices, usb, or any device
For example: /etc/resolv.conf, /etc/logrotate.conf
Contains information about system process.
attachedVariable Files
This to the system.
/var – is a pseudo filesystem contains information
For example: /dev/tty1,files.
var stands for process./dev/usbmon0
/tmp – running variable For example: /proc/{pid}
about Temporary Files
Contentcontains information about the process with
directory of the files that are expected to grow can be
/found under this directory.
– Root
Everyincludespid.system log files (/var/log); packages
that particular
/usr – Userthat contains temporary files createdroot
Directory Programs
This is a virtualand directory starts from the
single file
— filesystem with text information
by
This and binaries, libraries, documentation, and source-code
Contains users.
system
directory.
and database filesdirectory example: /proc/uptime
about – Home (/var/lib); emails (/var/mail); print
for /media – Removable Media Devices directory.
Files system thisDirectories deleted when system is
/homeunder resources. Forare (/var/lock); temp files
Only root userprograms. privilege under this
second level has write
/boot – (/var/spool); lock root
rebooted.note that /root is files user’s home directory,
queues Boot Loader Files
/usr/bin contains binary files for user programs. If you can’t
Please across reboots (/var/tmp);
needed
find Temporary mount directory forstore under personal
Homeuser same as for all users to look their devices.
under /bin, removable /usr/bin. For
which is not binary /.
a
For directories
– at, examples, related files.
Service Data /media/cdrom
/srvSystem Libraries
example: boot loader scp
Contains awk, cc, less,
/lib – for CD-ROM;
files.
/usr/sbin contains floppy /home/nikita
Kernel initrd,/home/john, drives; /media/cdrecorder for If
/media/floppy for binary files for system administrator.
/opt – Optional add-on grub files are located under
For example: vmlinux, Applications
youCD writerlibrary system binary under /sbin, look under
srv stands for service. that supports the binaries
/bootcan’t find a files
Contains
/usr/sbin. Forserver specific cron, sshd, useradd, userdel
opt stands for Directory services related data.
Contains
For – Mount initrd.img-2.6.32-24-generic, vmlinuz-
locatedexample: optional.
/mnt under /bin and /sbin
example: atd,
2.6.32-24-generic /srv/cvs contains CVS related data.
For example,
/usr/lib contains are applications lib*.so.* /usr/sbin
Library filenames libraries for /usr/bin and individual
Contains add-on either ld* or from
For example: mount users programs sysadmins can
Temporary ld-2.11.1.so, libncurses.so.5.7
/usr/local contains directory where that you install from
vendors.
source. filesystems. when you install installedfrom source, it
add-on applications should be apache under
mount For example,
goes under /usr/local/apache2
either /opt/ or /opt/ sub-directory.
4. Moving around the file system
cd - Change Directory Directory
pwd - Present Working
$ pwd
/home/srahuma
$ cd test
$ cd dir1
$ pwd
$ cd ..
/home/srahuma/test
$ pwd
$ cd $HOME
$ pwd
/home/srahuma/test/dir1
$ cd ~rxbala2
$ $ pwd
/home/srahuma/test
/home/srahuma
$ pwd
/home/rxbala2
$
$
5. Listing directory contents
ls list a directory
ls -l List a directory in long (detailed) format
For example:
drwxr-xr-x 2 srahuma qdefault 4096 Jun 5 01:03 HL4IT1019
-rw-r--r-- 1 srahuma qdefault 4577 Aug 24 21:00 ftplog
owner group size date time name
number of links to file or directory contents
permissions for world
permissions for members of group
permissions for owner of file: r = read, w = write, x = execute -=no permission
type of file: - = normal file, d=directory, l = symbolic link, and others...
ls -a List the current directory including hidden files. Hidden files start with "."
ls -ld * List all the file and directory names in the current directory using long format.
Without the "d" option, ls would list the contents of any sub-directory of the current. With
the "d" option, ls just lists them like regular files.
6. Changing file permissions and attributes
chmod 755 file Changes the permissions of file to be rwx for the owner, and
rx for the group and the world. (7 = rwx = 111 binary. 5 = r-x = 101 binary)
chgrp user file Makes file belong to the group user
chown cliff file Makes cliff the owner of file.
chown -R cliff dir Makes cliff the owner of dir and everything in its directory
tree.
You must be the owner of the file/directory or be root before you can do any of
these things.
7. Moving, renaming and copying files
cp file1 file2 copy a file
mv file1 newname move or rename a file
mv file1 ~/AAA/ move file1 into sub-directory AAA in your home directory.
rm file1 [file2 ...] remove or delete a file
rm -r dir1 [dir2...] recursively remove a directory and its contents BE
CAREFUL!
mkdir dir1 [dir2...] create directories
mkdir -p dirpath create the directory dirpath, including all implied directories
in the path.
rmdir dir1 [dir2...] remove an empty directory
8. Viewing and editing files
cat filename
Dump a file to the screen in ascii.
more filename
Progressively dump a file to the screen: ENTER = one line down SPACEBAR =
page down q=quit
less filename
Like more, but you can use Page-Up too. Not on all systems.
head -n filename
Show the first n lines of a file.
tail -n filename
Show the last n lines of a file.
vi filename
Edit a file using the vi editor. All UNIX systems will have vi in some form.
9. Redirecting output
Echo “This is a sample text” > sample.txt
Echo “This is the first line” > sample.txt
Echo “Adding second line” >> sample.txt
Sample.txt
This is the firsttext
a sample line
Adding second line
10. Piping the output
cat sample.txt | head -1 | cut –d’ ‘ –f4
This is the first line is the first line
This
first
Adding second line
11. Command Substitution
You can use the output of one command as an input to another command in
another way called command substitution. Command substitution is invoked
when by enclosing the substituted command in backwards single quotes.
for example:
cat `find . -name aaa.txt`
which will cat ( dump to the screen ) all the files named aaa.txt that exist in the
current directory or in any subdirectory tree.
You can also use this ` `(backward single quotes) to assign the result of a
command to a variable
for example,
a=5
b=4
value=`expr $a * $b`
12. Searching for files
find search_path -name filename
find . -name aaa.txt
Finds all the files named aaa.txt in the current directory or any subdirectory
tree.
find / -name vimrc
Find all the files named 'vimrc' anywhere on the system.
find /usr/local/games -name "*xpilot*"
Find all files whose names contain the string 'xpilot' which exist within the
'/usr/local/games' directory tree.
13. Searching in the files
Grep command is used to search for a specific pattern in a file. Displays the
line with the matching pattern. It can be executed with more options.
grep -w <pattern> <filename>
Searches for the expression as a complete word, ignoring those matches that
are substrings of larger words.
grep -i <pattern> <filename>
Searches for both uppercase and lowercase characters
grep -v <pattern> <filename>
Inverts the search to display lines that do not match the pattern
grep -n <pattern> <filename>
Precede each matching line with the line number
14. Searching in the files (contd.)
grep ’^pattern’ <filename>
Matches all lines beginning with “pattern”
grep ’pattern$’ <filename>
Matches all lines ending with “pattern”
grep ’p.....n’ <filename>
Matches lines containing a “p,” followed by Five characters, and followed by an
“n” pattern
grep -c <pattern> <filename>
Prints only the total count of the matching lines
15. Archiving
The tar command stands for "tape archive". It is the "standard" way to read and
write archives (collections of files and whole directory trees).
stuff.tar, or stuff.tar.gz
tar examples:
tar cvf archive.tar file1 [file2...]
Create a tar archive as a file "archive.tar" containing file1, file2...etc.
tar xvf archive.tar extract from the archive file
tar cvfz archive.tar.gz dname
Create a gzip compressed tar archive containing everything in the directory
'dname'. This does not work with all versions of tar.
tar xvfz archive.tar.gz
Extract a gzip compressed tar archive. Does not work with all versions of tar.
16. Compressing
The standard UNIX compression commands are compress and uncompress.
Compressed files have a suffix .Z added to their name.
compress part.igs Creates a compressed file part.igs.Z
uncompress part.igs Uncompresses part.igs from the compressed file
part.igs.Z.
Note the .Z is not required.
Another common compression utility is gzip (and gunzip). gzip usually gives
better compression than standard compress, but may not be installed on all
systems. The suffix for gzipped files is .gz
gzip part.igs Creates a compressed file part.igs.gz
gunzip part.igs Extracts the original file from part.igs.gz
17. Looking for help
Most of the commands have a manual page which give sometimes useful, often
more or less detailed, sometimes cryptic and unfathomable descriptions of their
usage.
man ls Shows the manual page for the ls command
You can search through the man pages using apropos
Example:
apropos build Shows a list of all the man pages whose descriptions contain
the word "build"
type – command will give the directory that contains the executable of a Linux
command.
type grep – Will give the result below
grep is a tracked alias for /bin/grep
18. Basics of vi editor
Opening a file
vi filename
Creating text
Edit modes: These keys enter editing modes and type in the text of your
document.
i Insert before current cursor position
I Insert at beginning of current line
a Insert (append) after current cursor position
A Append to end of line
r Replace 1 character
R Replace mode
<ESC>Terminate insertion or overwrite mode
19. Basics of vi editor (contd.)
Deletion of text
x Delete single character
dd Delete current line and put in buffer
ndd Delete n lines (n is a number) and put them in buffer
J Attaches the next line to the end of the current line (deletes carriage
return).
Oops
u Undo last command
20. Basics of vi editor (contd.)
Cut and paste
yy Yank current line into buffer
nyy Yank n lines into buffer
p Put the contents of the buffer after the current line
P Put the contents of the buffer before the current line
Cursor positioning
^d Page down
^u Page up
:n Position cursor at line n
:$ Position cursor at end of file
^g Display current line number
h,j,k,l Left,Down,Up, and Right respectively.
21. Basics of vi editor (contd.)
String substitution
:n1,n2:s/string1/string2/[g] Substitute string2 for string1 on lines n1 to n2. If g
is included (meaning global), all instances of string1 on each line are
substituted. If g is not included, only the first instance per matching line is
substituted.
^ matches start of line
. matches any single character
$ matches end of line
Examples:
:1,$:s/dog/cat/g Substitute 'cat' for 'dog', every instance for the entire
file - lines 1 to $ (end of file)
:23,25:/frog/bird/ Substitute 'bird' for 'frog' on lines 23 through 25. Only
the first instance on each line is substituted.
22. Basics of vi editor (contd.)
Saving and quitting and other "ex" commands
These commands are all prefixed by pressing colon (:) and then entered in the
lower left corner of the window. They are called "ex" commands because they
are commands of the ex text editor - the precursor line editor to the screen
editor vi. You cannot enter an "ex" command when you are in an edit mode
(typing text onto the screen)
Press <ESC> to exit from an editing mode.
:w Write the current file.
:w new.file Write the file to the name 'new.file'.
:w! existing.file Overwrite an existing file with the file currently being edited.
:wq Write the file and quit.
:q Quit.
:q! Quit with no changes.
:e filename Open the file 'filename' for editing.
:set number Turns on line numbering
:set nonumber Turns off line numbering
23. In our upcoming session
Variables ($# $1 …)
External programs
Functions
Escape Characters
Loops – for loops and while loops
Test – if statements
Case
Using awk and sed
Job control and process management