Linux Basic Commands
Working with Fedora
Mr. Chea Samnang
Linux Lecturer
Linux File System Hierarchy
• The Linux file system is the structure in
which all the information on your computer
is stored. Files are organized within a
hierarchy of directories. Each directory
can contain files, as well as other
directories.
Linux Directories
• Some of the Linux directories that may
interest you include the following:
• /bin - Contains common Linux user
commands, such as ls, sort, date,
and chmod.
• /boot - Has the bootable Linux
kernel and boot loader configuration
files (GRUB).
Linux Directory
• /dev - Contains files representing
access points to devices on your
systems. These include terminal devices
(tty*), floppy disks (fd*), hard disks (hd*
or sc*), RAM (ram*), and CD-ROM (cd*).
• /etc - Contains administrative
configuration files.
• /home - Contains directories assigned
to each user with a login account.
Linux Directories
• /media - Provides a location for
mounting devices, such as remote file
systems and removable media (with
directory names of cdrom, floppy, and
so on).
• /root - Represents the root user's
home directory.
• /sbin - Contains administrative
commands and daemon processes.
Linux Directories
• /tmp - Contains temporary files used by
applications.
• /usr - Contains user documentation,
games, graphical files (X11), libraries (lib),
and a variety of other user and
administrative commands and files.
• /var - Contains directories of data used by
various applications. In particular, this is
where you would place files that you share
as an FTP server (/var/ftp) or a Web
server (/var/www). It also contains all
system log files (/var/log).
Linux File Systems versus
Windows-based File Systems
• In MS-DOS and Microsoft Windows file
systems
– Drive letters represent different storage
devices
– Backslash is used to separate directory name
– File name always has extension
– File name is not case sensitive
– DOS and MS Windows began as single-user
systems, file ownership was not built into those
systems when they were designed.
Linux File Systems versus
Windows-based File Systems
• In Linux file Systems
– All storage devices are fit into the file
system hierarchy that mounted to local
directories
– Slash is used to separate directory name
– File name no require extension
– File name is case sensitive
– Every file and directory in a Linux system
has permissions and ownership associated
with it.
Type of Linux Files
• There are four type of Linux Files
- regular file
d directory
l link file
c character device file
b block device file
File and Directory Permission
• Three Basic File Permissions
– Read (r)
Grants permission to read to the contents of the file
– Write (w)
Grants permission to write to a file
– Execute (x)
Grants permission to execute the file
File and Directory Permission
• Each of these three permissions (r, w, x)
can denied to users in three different
communities such as
– User (u)
• Initially who create the file
– Group (g)
• All members of the group that owns the file.
– Other (o)
• Everyone else
File and Directory Permission
• chmod is use to change file permission with
symbol text mode.
chmod [ u,g,o,a ] [ - , + , = ] [ r , w , x ] < file /
directory >
Identities
• u : the user who owns the file (that is, the owner)
• g : the group to which the user belongs
• o : others (not the owner or the owner's group)
• a : everyone or all (u, g, and o)
File and Directory Permission
• Using Binary and Octal values for
permission
Permission Binary value Octal value
rwx 111 7
rw- 110 6
r-x 101 5
r-- 100 4
-wx 011 3
-w- 010 2
--x 001 1
--- 000 0
File and Directory Permission
chown is used to change file owner and
group of file or directory.
chown owner : group <file/directory>
chgrp is used to change group of file or
directory
chgrp group <file or directory>
Linux Basic Commands
ls : lists directory contents
cp : copies file or directory
rm : removes file
mv : moves file of directory
mkdir : makes directory
rmdir : removes directory
cat : display contents of file
more : paginates a file
less : paginates a file ( press q to exit )
head : display the first 10 lines of content’s file
tail : display the last 10 lines of content’s file
cal : display calendar of current month
Linux Basic Commands (Cont.)
clear : clear the screen
which : display the full path of commands
date : print the current date and timestamp
echo : display the line of text
touch : update the timestamp on a file
file : print file type
wc : count words, lines, and character
who : display the list of users currently logged on
to the system.
w : display who is logged in, and what are they
doing
whoami : display current user name
id : display UID and GID of user account
Linux Basic Commands (Cont.)
finger : display user information
chfn : changing user information
whereis : display the location of the command
grep : search string in the content of file
find : find the file from directory
mount : maps a file system to a directory
umount : unmount a currently mounted file
uname : display system information
df : display disk usage for file system
du : disk space usage
Linux Wild Card
\ : Escape character. If you want to reference
a special character, you must “escape” it with
a backslash first
/ : Directory separator, used to separate a string
of directory names
. : Current directory. Can also “hide” files when it
is the first character in a filename
.. : Parent directory
~ : User's home directory
Linux Wild Card (Cont.)
* : Represents 0 or more characters in a
filename, or by itself, all files in a directory
? : Represents a single character in a filename.
[ ] : Can be used to represent a range of values,
e.g. [0-9], [A-Z], etc
| : “Pipe”. Redirect the output of one command
into another command
; : Command separator. Allows you to execute
multiple commands on a single line
Linux Wild Card (Cont.)
> : Redirect output of a command into a new file.
If the file already exists, over-write it
>> : Redirect the output of a command into the end
of an existing file
< : Redirect a file as input to a program
& : Execute a command in the background, and
immediately get your shell back
&& : Command separator as above, but only runs
the second command if the first one finished
without errors
Linux Shell Script
• Why should you write and use shell
scripts?
Shell scripts can save you time and typing,
especially if you routinely use the same
command lines multiple times every day.
• How to create Shell Script File
Create any file with ended “.sh” by using vim,
gedit, nano, emas ………
How to create shell script file
# vi [Link]
Press: “i” (for inserting)
echo –n today is
date
echo The user who log on the server are:
who
Press: Esc (switch mode)
Type: :x (to save and exit)
How to execute shell script
• There are two ways for executing shell
script
If the script file has no execute permission
you use sh, bash, tcsh, csh, zsh.
Ex: #sh [Link]
(or) #bash [Link]
• Add permission to Script file for executing
Ex: #chmod +x [Link]
• Execute script file
Ex: # ./[Link]
Shell script variable
• Using Variables in Shell Scripts
When writing shell scripts for Linux, you
work with three types of variables:
1. Environment variables
Part of the system environment, you can use
them in your shell program.
Ex: HOME, PATH , SHELL , MAIL
LOGNAME , TZ , OSTYPE, ….
Shell script variables
2. Built-in variables
These variables, such as options used on
the command are provided by Linux. Unlike
environment variables, you cannot modify them.
$# - Number of positional parameters passed to
the shell program.
$0 - The name of the shell program.
$* - A single string of all arguments passed at
the time of invocation of the shell program.
Shell script variable
[Link] variables
Defined by you when you write a shell script.
You can use and modify them and will within the
shell program.
In shell programming, variables are not
typed that is, you do not have to specify
whether a variable is a number or a string, and
so on.
User variables
#!/bin/sh
echo "Number of parameters is $#"
echo "Program name is $0"
echo "Parameters as a single string is $*"
first_name=”chan”
last_name=”dara”
echo “My name is $first_name $last_name”
echo “My login name is $LOGNAME”
String Comparison
• The following operators can be used to
compare two string expressions
= To compare whether two strings are equal
!= To compare whether two strings are not equal
-n To evaluate whether the string length is
greater than zero
-z To evaluate whether the string length is equal
to zero
Number Comparison
• The following operators can be used to
compare two numbers
-eq One number is equal to the other number
-ge One number is greater than or equal to the
other number
-le One number is less than or equal to the
other number
-ne Two numbers are not equal
-gt One number is greater than the other
number
-lt One number is less than the other number
File comparison
• The following operators can be used as
file comparison operators
-d To ascertain whether a file is a directory
-f To ascertain whether a file is a regular file
-h To ascertain whether a file is a symbolic link
file
-c To ascertain whether a file is a character
special device file
-b To ascertain whether a file is a block special
device file
File comparison (Cont.)
-s To ascertain whether a file exists and has a
length greater than zero
-r To ascertain whether read permission is set
for a file
-w To ascertain whether write permission is set
for a file
-x To ascertain whether execute permission is
set for a file
Logical Operators
• Logical operators
Are used to compare expressions using Boolean
logic, which compares values using characters
representing NOT, AND, and OR.
! To negate a logical expression
-a To logically AND two logical expressions
-o To logically OR two logical expressions
Control Statement
• The if Statement
The if statement evaluates a logical expression
to make a decision.
if [ expression ]; then
Statements
elif [ expression ]; then
Statements
else
Statements
fi
Control structure
#!/bin/sh
echo –n “input user id: “
read id
if [ $id –eq 0 ]; then
echo “It is super user”
elif [ $id -gt 0 -a $id -lt 500 ] ; then
echo “It is system user”
elif [ $id -ge 500 -a $id -ne 65534 ] ; then
echo “It is regular user”
fi
Case Statement
• The case Statement
The case statement is used to execute
statements depending on a discrete value or a
range of values matching the specified variable
case str in
str1 | str2) Statements;;
str3 |str4) Statements;;
*) Statements;;
esac
Case Statement
#!/bin/sh
case $1 in
01 | 1) echo "Today is Monday ";;
02 | 2) echo "Today is Tuesday ";;
03 | 3) echo "Today is Wednesday ";;
04 | 4) echo "Today is Thursday ";;
05 | 5) echo "Today is Friday ";;
06 | 6) echo "Today is Saturday";;
07 | 7) echo "Today is Sunday";;
*) echo "Invalid parameter";;
esac
Control Structure
• The for Statement
The for statement is used to execute a set
of commands once each time a specified
condition is true. The first format used by
bash is as follows:
for var in list
do
statements
done
Control structure
#!/bin/bash
for i in $( ls ); do
echo item: $i
done
#!/bin/bash
for i in `seq 1 10`; do
echo $i
done
Control structure
• The while Statement
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
Control structure
• The until Statement
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
Function
• As with other programming languages,
shell programs also support functions.
function fun_name {
statements
}
fun_name() {
statement
}
Function
function device {
if [ -c $file -o -b $file ]; then
echo $file is device file.
else
echo $file is not device file.
fi
}
read file
device
Function
device( ) {
if [ -c $file -o -b $file ]; then
echo $file is device file.
else
echo $file is not device file.
fi
}
read file
device
The End