You are on page 1of 87

Linux Directory Structure

(File System Structure)


– 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, which is not
same as /.
/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
/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
/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
/dev – Device Files
• Contains device files.
• These include terminal devices, usb, or any device attached to the
system.
• For example: /dev/tty1, /dev/usbmon0
/proc – Process Information
• Contains information about system process.
• This is a pseudo filesystem contains information about running
process. For example: /proc/{pid} directory contains information
about the process with that particular pid.
• This is a virtual filesystem with text information about system
resources. For example: /proc/uptime
/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);
/tmp – Temporary Files
• Directory that contains temporary files created by system and users.
• Files under this directory are deleted when system is rebooted.
/usr – User Programs
• Contains binaries, libraries, documentation, and source-code for second
level programs.
• /usr/bin contains binary files for user programs. If you can’t find a user
binary under /bin, look under /usr/bin. For example: at, awk, cc, less, scp
• /usr/sbin contains binary files for system administrators. If you can’t find a
system binary under /sbin, look under /usr/sbin. For example: atd, cron,
sshd, useradd, userdel
• /usr/lib contains libraries for /usr/bin and /usr/sbin
• /usr/local contains users programs that you install from source. For
example, when you install apache from source, it goes under
/usr/local/apache2
/home – Home Directories
• Home directories for all users to store their personal files.
• For example: /home/john, /home/nikita
/boot – Boot Loader Files
• Contains boot loader related files.
• Kernel initrd, vmlinux, grub files are located under /boot
• For example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic
/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
/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.
/mnt – Mount Directory
• Temporary mount directory where sysadmins can mount filesystems.
/media – Removable Media Devices
• Temporary mount directory for removable devices.
• For examples, /media/cdrom for CD-ROM; /media/floppy for floppy
drives; /media/cdrecorder for CD writer
/srv – Service Data
• srv stands for service.
• Contains server specific services related data.
• For example, /srv/cvs contains CVS related data.
Linux Basic Commands
• Linux has a back end access known as shell. You can control and activate all
the process in Linux from the shell.
• So it is very important to learn few basic commands to work with Linux
operating system.
• First we will learn how to login for shell access. There are 7 terminals for
Linux. 6 terminals are non - GUI and 1 terminal for GUI access.
• You can login to each terminal using Alt + Ctrl + F1 , F2, .. F7. Each terminals
will request your username and password for login.
• If you want to use the shell in the graphical interface (GUI), press Alt + F2
and type "konsole".
• As a user you will have permission to access only your /home/user
directory and other directories in it.

• *Note: user denotes the username.


Navigation Commands - [ pwd, cd ]
• pwd
This command is used to find the current location or current working
directory.
• Eg:
[user@ws26 ~]$ pwd
/home/user
• cd
This command id used to change the directory. You can move from one
directory to another using this command. Few examples are given below.
• Concider you have a directory structure /home/user/test/test1/ . test and
test1 are directories in user home.
Navigation Commands - [ pwd, cd ]
• Example 1:
Consider you have a directory "test" in /home/user. Your current
working directory is /home/user. You want to change your current
working directory from /home/user to /home/user/test, use the
following command
[user@ws26 ~]$ pwd
/home/user
[user@ws26 ~]$ cd test
[user@ws26 ~]$ pwd
/home/user/test
Navigation Commands - [ pwd, cd ]
• Example 2:
If you want to move back to /home/user, use the following command
[user@ws26 ~]$ cd ..
[user@ws26 ~]$ pwd
/home/user
Navigation Commands - [ pwd, cd ]
• Example 3:
To get back to the home directory of the user
[user@ws26 ~]$ cd ~
[user@ws26 ~]$ pwd
/home/user.
Listing contents in a directory
- [ ls, ls -l, ll ]
• ls
This command is used to list all the files and directory in the current
directory.
• Eg:
[user@ws26 ~]$ ls
1152696870.jpg book OperaDownloads snapshot52.png
image300.jpg Desktop test spiderman2.jpg
Listing contents in a directory
- [ ls, ls -l, ll ]
• ls -l or ll
This command is also used to list all the files and directories. Here you
will get more details about the files and directories present in the
current directory. You will see the permission set, creation date, file /
directory size etc.
Listing contents in a directory
- [ ls, ls -l, ll ]
• Eg:
[user@ws26 ~]$ ls -l
total 5
-rw-rw-r-- 1 user user 33188 Dec 22 02:56 1152696870.jpg
-rw-rw-r-- 1 user user 17647 Aug 19 2006 534458.gif
-rw-rw-r-- 1 user user 26817 Aug 19 2006 534477.gif
drwxrwxr-x 2 user user 4096 Jan 13 04:20 book
drwx------ 3 user user 4096 Feb 4 02:34 Desktop
Reading files in Linux - [ cat, more, less ]
• cat
This command is used to display the contents of a file. You can read
the file contents using the cat command.
• Example:
[user@ws26 ~]$ cat testfile
this is a test file.. You can read me..
Reading files in Linux - [ cat, more, less ]
• Less and More
Both the less and more commands serve similar function. They are
used to display file one screen at a time. You can press spacebar to
continue reading the file. These commands are mainly used while
reading large files.
• Example 1:
[user@ws26 ~]$ less testfile
this is a test file.. You can read me..
• Example 2:
[user@ws26 ~]$ more testfile
this is a test file.. You can read me..
Manipulating files - [ cp, mv, rm, mkdir ]
• cp
This command is used to copy files/directory.
• Example:
[user@ws26 ~]$ cp file1 directory1
This will copy the file1 into the directory1
Manipulating files - [ cp, mv, rm, mkdir ]
• cp -R
This command is used to copy directories recursively (copy all the files
and folders inside the directory).
• Example:
[user@ws26 ~]$ cp directory2 directory1
This will copy the whole directory2 into directory1
Manipulating files - [ cp, mv, rm, mkdir ]
• mv
This command is used to move the file or directory.
• Example:
[user@ws26 ~]$ mv directory2 directory1
This will move the whole directory2 into directory1
Manipulating files - [ cp, mv, rm, mkdir ]
• rm
This command is used to remove or delete files and directories.
• Example:
[user@ws26 ~]$ rm directory2
This will delete the directory - directory2
Manipulating files - [ cp, mv, rm, mkdir ]
• mkdir
This command is used to create a new directory
• Example:
[user@ws26 ~]$ mkdir directory3
This will create a new directory - directory3.
Create new file in Linux - vi editor
• The Vi editor is mainly used to create new files in Linux.
Example:
vi testfile
The command will open a new window and you can insert text to it.
To begin press "i". After adding your contect press ":wq" to save the
file.
Command line vs. GUI
Topic Command line (CLI) GUI

Due to a higher degree of memorization


Because a GUI is much more visually
and familiarity needed for operation and
intuitive, users typically pick up on how to
Ease navigation, new users find operating a
use a GUI faster than a command line
command line interface more difficult
interface.
than a GUI.
A GUI offers a lot of access to files,
Users have a good bit of control over
software features, and the operating
both the file and operating systems in a
system as a whole. Being more user
Control command line interface. However, for
friendly than a command line, especially
new or novice users, it is not as user
for new or novice users, a GUI is utilized
friendly as a GUI.
by more users.
Although many command line
GUI users have windows that enable a
environments are capable of multitasking,
user to view, control, manipulate, and
Multitasking they do not offer the same ease and
toggle through multiple programs and
ability to view multiple things at once on
folders at same time.
one screen.
Topic Command line (CLI) GUI

While newer technology is making a GUI


Command line users only need to utilize a faster and more efficient than ever
Speed keyboard to navigate the interface, often before, using both a mouse and keyboard
resulting in faster performance. to navigate and control the GUI is still a
bit slower than a command line interface.
A GUI requires more system resources
because of the elements that require
A computer that is only using the
loading, such as icons and fonts. Video,
Resources command line takes a lot less of the
mouse, and other drivers need to be
computer's system resources than a GUI.
loaded, taking up additional system
resources.
Creating scripts using a GUI has become
much easier with the help of
programming software, which allows
A command line interface mostly requires
users to write the scripts without having
users to already know scripting
Scripting to know all the commands and syntax.
commands and syntax, making it difficult
Programming software provides guides
for new or novice users to create scripts.
and tips for how to code specific
functions, as well as preview options to
see if and how the script will work.
Topic Command line (CLI) GUI

Remotely access another computer or


When accessing another computer or
server is possible in a GUI and easy to
device over a network, a user can
navigate with little experience. IT
manipulate the device or its files with a
Remote access professionals typically use a GUI for
command line interface. However, you
remote access, including the
must know the commands to do so and is
management of servers and user
not as easy for new or novice users.
computers.
After you've learned how to navigate and Each GUI has a different design and
use a command line, it's not going to structure when it comes to performing
change as much as a new GUI. Although different tasks. Even different iterations
Diversity
new commands may be introduced, the of the same GUI, such as Windows, can
original commands almost always remain have hundreds of different changes
the same. between each version.
A command line interface is often very The use of shortcut keys and more
basic and can be more of a strain on a frequent movement of hand positions,
user's vision. Carpal Tunnel Syndrome can due to switching between a keyboard and
also be a bit of a risk when using a a mouse, strain may be reduced. Visual
Strain
command line interface because users strain can still be a risk, but a GUI has
are only using a keyboard. There is little more colors and is more visually
need to change hand positions and strain appealing, leading to a potential
to the wrists or even fingers can occur. reduction in visual strain.
Network Account Management
Unix File System
The Unix file system looks like an inverted
tree structure.
You start with the root directory, denoted by
/, at the top and work down through sub-
directories underneath it.
File System
• Each node is either a file or a directory of files, where the latter can
contain other files and directories.
• You specify a file or directory by its path name, either the full, or
absolute, path name or the one relative to a location.
• The full path name starts with the root, /, and follows the branches of
the file system, each separated by /, until you reach the desired file,
e.g.:
• /home/condron/source/xntp
File System
• A relative path name specifies the path relative to another, usually
the current working directory that you are at. Two special directories :
• . the current directory
• .. the parent of the current directory
File System
• So if I'm at /home/frank and wish to specify the path above in a
relative fashion I could use:
• ../condron/source/xntp
• This indicates that I should first go up one directory level, then come
down through the condron directory, followed by the source directory
and then to xntp.
Directories, Files and Inodes
• Every directory and file is listed in its parent directory.
• In the case of the root directory, that parent is itself.
• A directory is a file that contains a table listing the files contained
within it, giving file names to the inode numbers in the list.
• The information about all the files and directories is maintained in
INODE TABLE
• An Inode (Index Nodes) is an entry in the table containing information
about a file (metadata) including file permissions, UID, GID, size, time
stamp, pointers to files data blocks on the disk etc.
Introduction to Linux

Users, Groups and Access Permissions

• In UNIX/LINUX, there is a concept of user and an associated group


• The system determines whether or not a user or group can access a
file or program based on the permissions assigned to them.
• Apart from all the users, there is a special user called Super User or
the root which has permission to access any file and directory
Introduction to Linux

Access Permissions

• There are three permissions for any file, directory or application


program.
• The following lists the symbols used to denote each, along with a
brief description:
• r — Indicates that a given category of user can read a file.
• w — Indicates that a given category of user can write to a file.
• x — Indicates that a given category of user can execute the file.
Access Permissions

• Each of the three permissions are assigned to three defined categories


of users.
• The categories are:
• owner — The owner of the file or application.
• group — The group that owns the file or application.
• others — All users with access to the system.
Access Permissions

• One can easily view the permissions for a file by invoking a long
format listing using the command ls -l.
• For instance, if the user juan creates an executable file named test,
the output of the command ls -l test would look like this:

• -rwxrwxr-x 1 juan student 0 Sep 26 12:25 test


Access Permissions

• The permissions for this file are listed are listed at the start of the line,
starting with rwx.
• This first set of symbols define owner access.
• The next set of rwx symbols define group access
• The last set of symbols defining access permitted for all other users.
Access Permissions
• This listing indicates that the file is readable, writable, and executable
by the user who owns the file (user juan) as well as the group owning
the file (which is a group named student).

• The file is also world-readable and world-executable, but not world-


writable.
Listing the Content of a Directory
• ls is used to list the contents of a directory.

• If the command ls is written with parameter –l then the command lists


contents of the working directory with details. Example:

• $ ls –l
Moving in Directories
• cd try_it
– Changes the directory to try_it
• pwd
– Prints present working directory (e.g. /home/smith/try_it)
• cd .. Move to superior directory
pwd : Prints /home/smith
• cd /home The absolute path
pwd : Prints /home
• cd The system is returned to the user home directory
pwd : Print /home/smith
Make Directory

• The command mkdir my_dir

• makes new directory my_dir (the path is given relative) as a


subdirectory of the current directory.
Remove Directory

• The command rmdir your_dir

• removes directory your_dir if it is empty.


Copy File

• The command cp file_1 file_2

• copies file_1 to file_2. The both files must be in the same working
directory. If they are in various directories, the path must be given.
Rename and/or Move the File

• The command mv file_1 file_2

• moves file_1 to file_2

• The both files must be in the same working directory.

• If they are in different directories, the path must be given.

• The file_1 is removed from the disk.


Remove File
• The command rm file_a

removes the file_a from the system


• If you use wildcard. For example

rm h*c

• you will remove all files beginning with h and ending with c which are
in working directory.
Remove File
• If you write

rm *

you will erase all files from your working directory.


Access Permission of File/Directory
• The ownership of the file or directory can be changed using the
command

chown <owner> <file/directory name>

• The group of the file or directory can be changed using the command

chgrp <group> <file/directory name>


Access Permission of File/Directory
• The permissions of the file can be changed using chmod command

chmod -R ### <filename or directory>

• -R is optional and when used with directories will traverse all the sub-
directories of the target directory changing ALL the permissions to ###.
Access Permission of File/Directory
• The #'s can be:

0 = Nothing
1 = Execute
2 = Write
3 = Execute & Write (2 + 1)
4 = Read
5 = Execute & Read (4 + 1)
6 = Read & Write (4 + 2)
7 = Execute & Read & Write (4 + 2 + 1)
Shell Scripting
Shell script/program
• A series of shell commands placed in an ASCII text file
• Commands include
• Anything you can type on the command line
• Shell variables
• Control statements (if, while, for)
Script execution
• Provide script as an argument to the shell program (e.g. bash
my_script)
• Or specify which shell to use within the script
• First line of script is #!/bin/bash
• Make the script executable using chmod
• Make sure the PATH includes the current directory
• Run directly from the command line
• No compilation is necessary!
Simple example script
#!/bin/bash Output:
Hello world!
echo “Hello world!” /home/grads/sgifford
cd ~
pwd
Quoting
• Quoting is necessary to use special characters in a variable’s value or
string
• ”” - shell only interprets $ and ‘‘ and \
• $ - variable substitution
• ` - Command substitution
• \” - Literal double quote
• \ is used to escape characters
• echo ‘`date +%D`‘ will print: 10/06/03
• ’’ - shell doesn’t interpret special characters
• echo ‘`date +%D`‘ will print: `date +%D`
Shell variables
 Numeric • var refers to the name, $var
to the value
 Strings • t = 100 #Sets var t to value
 Arrays 100
• echo ”\$t = $t” #will print:
 Command line arguments $t = 100
 Functions • Remove a variable with unset
var
 Read only • Names begin with alpha
characters and include alpha,
numeric, or underscore
Numeric variables
• Integer variables are the only pure numeric variables that can be used
in bash
• Declaration and setting value:
declare -i var=100
• Expressions in the style of C:
• (( expression ))
• e.g. (( var+=1 ))
• +, -, *, /, %, &, |, ~, <, >, <=, >=, ==, !=, &&, ||
String variables
• If you do not use the declare keyword with option –i when
using a variable for the first time, it will be a string
• var=100 makes var the string ‘100’.
• However, (( var=100 )) will treat var as an integer even though it is a
string
Array variables
• Array is a list of values
• Don’t have to declare size
• Reference a value by ${name[index]}
• ${a[3]}
• $a (same as ${a[0]})
• Use the declare -a command to declare an array
• declare –a sports
• sports=(ball frisbee puck)
• sports[3]=bat
Arrays
• Array initialization
• sports=(football basketball)
• moresports=(${sports[*]} tennis)
• ${array[@]} or ${array[*]} refers to the entire array
contents
• echo ${moresports[*]} produces
football basketball tennis
Command line arguments
• If arguments are passed to a script, they can be referenced by $1, $2,
$3, …
• $0 refers to the name of the script
• $@ - array filled with arguments excluding $0
• $# - number of arguments
Exporting variables
• The export command, when used with a variable name, allows
child processes of the shell to access the variable
Output
• We have already seen echo
• More common in other shells including ksh is print (does not exist
in bash)
• echo –n does not print newline after output
Return values
• Scripts can return an integer value
• Use exit N
• The variable $? will contain the return value of the last command run
• Can be used to test conditions
Conditions
• If using integers: (( condition ))
• If using strings: [[ condition ]]
• Examples:
• (( a == 10 ))
• (( b >= 3 ))
• [[ $1 = -n ]]
• [[ ($v != fun) && ($v != games) ]]
• Special conditions for file existence, file permissions, ownership, file
type, etc.
Conditions (continued…)
• [[ -e $file]] – File exists?
• [[ -f $file]] – Regular file?
• [[ -d $file]] – Directory?
• [[ -L $file]] – Symbolic link?
• [[ -r $file]] – File has read permission?
• [[ -w $file]] – File has write permission?
• [[ -x $file]] – File has execute perm?
• [[ -p $file]] – File is a pipe?
If statements
• Syntax:
if condition
then
statements
elif condition
then
statements
else
statements
fi
If statement
• Example
if [[ -r $fname ]]
then
echo ’$fname is readable’
elif [[ -w $fname && -x $fname ]]
then
echo ’$fname is writeable and executable’
fi
For loops
• Syntax:
for var [in list]
do
statements
done
• If list is omitted, $@ is assumed
• Otherwise ${list[*]}
• where list is an array variable.
For loop example
for colors in Red Blue Green Yellow Orange Black Gray
White
do
echo $colors
done
echo
While loops
• Syntax:
while condition
do
statements
done
• The keywords break, continue, and return have the same
meaning as in C/C++
Case statements
• Syntax:
case expression in
pattern1)
statements ;;
pattern2)
statements ;;

*)
statements ;;
esac
Case statement example
case $1 in
-a)
statements related to option a ;;
-b)
statements related to option b ;;
*)
all other options ;;
esac
Command substitution
• Use the output of a command in a variable, condition, etc. by:
• `command`
• $(command)
Examples
• Arguments printed in a for loop
• Reformatting the wc command
• Performing a depth-first search

You might also like