Unix File Permissions and Shell Programming
Unix File Permissions and Shell Programming
Prepared by
Mr. Basavaraj Nayak
Assistant Professor
Institute Vision
To be a premier Institution by imparting quality Technical education, Professional Training and Research.
Institute Mission
M1: To provide an outstanding Teaching, Learning and Research environment through Innovative Practices in Quality
Education.
M2: Develop Leaders with high level of Professionalism to have career in the Industry, Zeal for Higher Education, focus on
Entrepreneurial and Societal activities.
Department Vision
To empower the students with knowledge and skills to develop the competency in the field of Artificial Intelligence and
Machine Learning.
Department Mission
M1: To craft the students with Novel and Intellectual skills to capability in the field of Artificial Intelligence and Machine
Learning.
M2: To train the students to have Professional career in the field of AI and ML and zeal for Higher Studies and Research.
PROGRAMME SPECIFIC OUTCOMES (PSOs)
PSO1: Students will have the ability to understand analyse and demonstrate the knowledge of Human cognition, Artificial
Intelligence, Machine Learning in terms of real world problems to meet the challenges of future.
PSO2: Students will have the knowledge of software, Hardware, Algorithms, Modelling Networking and Application
Development.
PSO3: Students will have the ability to develop computational knowledge using Innovative tools and techniques to solve
problems in the areas related to Machine learning and Artificial Intelligence.
PEO2: Graduates will have good Leadership Qualities, Self Learning abilities and zeal for higher studies and Research.
PEO3: Graduates will follow Ethical Practices and exhibit high level of professionalism by participating and addressing
Technical, Business and Environmental challenges.
Module-2
File attributes and permissions: The ls command with options. Changing file permissions:
the relative and absolute permissions changing methods. Recursively changing file permissions.
Directory permissions
. The shells interpretive cycle: Wild cards. Removing the special meanings of wild cards. Three
standard files and redirection. Connecting commands: Pipe. Basic and Extended regular
expressions. The grep, egrep. Typical examples involving different regular expressions. Shell
programming: Ordinary and environment variables. The. profile. Read and read-only commands.
Command line arguments. exit and exit status of a command. Logical operators for conditional
execution. The test command and its shortcut. The if, while, for and case control statements. The set
and shift commands and handling positional parameters. The here (<<) document and trap
command. Simple shell program examples.
Outline
File attributes and permissions:
The ls command with options.
Changing file permissions: the relative and absolute permissions changing methods.
Recursively changing file permissions. Directory permissions.
The shells interpretive cycle:
Wild cards. Removing the special meanings of wild cards. Three standard files and redirection.
Connecting commands:
Pipe. Basic and Extended regular expressions.
The grep, egrep. Typical examples involving different regular expressions.
Shell programming:
Ordinary and environment variables.
The .profile. Read and readonly commands.
Command line arguments.
exit and exit status of a command.
Logical operators for conditional execution.
The test command and its shortcut. The if, while, for and case control statements.
The set and shift commands and handling positional parameters.
The here ( << ) document and trap command. Simple shell program examples.
5
File attributes and permissions and knowing them.
ls - l : Listing file Attributes
ls command is used to obtain a list of all filenames in the current directory.
ls looks up the file’s inode to fetch its attributes.
ls –l to lists seven attributes of all files in the current directory and they are:
1. File type and Permissions
2. Links
3. Ownership
4. Group ownership
5. File size
6. Last Modification date and time
7. File name
Example:
$ ls –l
total 72
- rw - r- - r- - 1 kumar metal 19514 may 10 13:45 chap01
- rw - r- - r- - 1 kumar metal 4174 may 10 15:01 chap02
- rw - rw - rw - 1 kumar metal 84 feb 12 12:30 [Link]
- rw - r- - r- - 1 kumar metal 9156 mar 12 1999 [Link]
d rwx r - x r - x 2 kumar metal 512 may 9 10:31 helpdir
d rwx r - x r - x 2 kumar metal 512 may 9 09:57 progs
When the system administrator creates a user account, he has to assign these parameters to the user:
The user-id (UID) – both its name and numeric representation
The group-id (GID) – both its name and numeric representation
The file /etc/passwd and /etc/group contains the UID and GID
9
File attributes and permissions and knowing them.
File Permissions
UNIX follows a three-tiered file protection system that determines a file’s access rights.
It is displayed in the following format:
Filetype owner (rwx) groupowner (rwx) others (rwx)
Example:
- rwx r-x r-- 1 kumar metal 20500 may 10 19:21 chap02
rwx r-x r--
owner/user group owner others
Each group here represents a category and contains three slots, representing the read, write and execute permissions of the file
3 types of categories
Owner(user): (rwx)
It contains all three permissions(rwx).
The file is readable, writable and executable by the owner of the file.
group :(r – x)
Group has a hyphen in the middle slot, which indicates the absence of write permission by the group owner of the file.
Other: (r - -)
It has the write and execute bits absent. This set of permissions is applicable to others.
You can set different permissions for the three categories of users – owner, group and others.
10
chmod : Changing File Permissions
chmod : Changing File Permissions
A file or a directory is created with a default set of permissions, which can be determined by umask.
Let us assume that the file permission for the created file is - r w - r - - r - -.
Using chmod command, we can change the file permissions and allow the owner to execute his file.
11
chmod : Changing File Permissions
1. Relative Permissions
chmod only changes the permissions specified in the command line and the other permissions unchanged.
syntax
chmod category operation permission filename(s)
Initially,
-rw-r--r-- 1 kumar metal 1906 sep 23:38 priya
$ chmod u+x priya ; ls –l priya
will assign all permissions to the owner, read and write permissions for the group and only execute permission to the others.
If a directory has write permission for group and others also, be assured that every user can remove every file in the directory.
As a rule, you must not make directories universally writable unless you have definite reasons to do so.
If sharma copies a file of kumar, then sharma will become its owner and he can manipulate the attributes
18
chown: changing file owner
Changing ownership requires superuser permission, so use su command
$ ls -l note
-r w x r - - - - x 1 kumar metal 347 may 10 20:30 note
chown sharma note; ls -l note
-r w x r - - - - x 1 sharma metal 347 may 10 20:30 note
Once ownership of the file has been given away to sharma, the user file permissions that previously applied to Kumar now apply to sharma.
Thus, Kumar can no longer edit note since there is no write privilege for group and others. He cannot get back the ownership either. But he can copy the file to his
own directory, in which case he becomes the owner of the copy.
1. The shell issues the prompt and waits for you to enter a command.
2. After a command is entered, the shell scans the command line for metacharacters
and expands abbreviations (like the * in rm *) to recreate a simplified command
line.
3. It then passes on the command line to the kernel for execution.
4. The shell waits for the command to complete and normally can’t do any work while
the command is running.
5. After command execution is complete, the prompt reappears and the shell returns to
its waiting role to start the next cycle. You are now free to enter another command.
You can reverse the matching criteria by using ! As the first character in the class.
$ ls *.[!co] matches all filenames with a single-character extension but not .c and .o (Not
in C Shell)
$ ls [!a-zA-Z]* matches all filenames that don’t begin with an alphabetic character
(Not in C Shell)
Quoting: Enclosing the wild-card, or even the entire pattern, within quotes. When a
command argument is enclosed in quotes, the meanings of all enclosed special characters are
turned off.
Example:
$ rm ‘chap*’ Removes file chap*
25
Redirection: The three standard files
In the context of redirection, the terminal is a generic name that
represents the screen or keyboard.
We see the command output and error messages on the terminal (display)
and provide command input through the terminal (keyboard).
The shell associates three files with the terminal-two for the display and one for the keyboard.
These files are actually streams of characters which many commands see as input and output.
The kernel maintains a table of the file descriptors. The first three slots are allocated to the three standard
streams.
0 – Standard Input
1 – Standard Output
2 – Standard Error
27
Redirection: The three standard files
Standard Input
When a command is used without arguments, it reads the file representing the standard
input. It can represent three input sources viz.,
The keyboard, the default source
A file using redirection with the < symbol
Another program using a pipeline
Example:
wc (without any arguments)
wc < [Link]
ls | wc
Note: When a command takes input from multiple sources (e.g., a file and standard input) the – symbol must
be used to indicate the sequence of taking input.
cat file1 – file2 First from file1, then standard input, and then from file2
28
Redirection: The three standard files
Standard Output
All commands displaying output on the terminal actually write to the standard output file as
a stream of characters. There are three possible destinations of this stream:
The terminal, the default destination
A file using the redirection symbols > and >>
As input to another program using a pipeline
Example:
wc [Link]
wc [Link] > outputFile
who | wc –l
cat *.c > all_C_progs.txt
29
Redirection: The three standard files
Standard Error
When you enter an incorrect command, or try to open nonexistent file, certain diagnostic
messages show up on the screen. This is the standard error stream.
For example, trying cat on a nonexistent file produces the error stream.
We can redirect this stream to a file. But, standard error cannot be redirected in the same
way standard output can be (with > or >>). To capture the standard error and redirect to a
file we have to use 2> symbols.
Example: cat file1 2> errorfile
cat file2 2>> errorfile
30
Redirection: The three standard files
Filters
UNIX commands can be grouped into 4 categories viz.,
[Link]-oriented commands like mkdir, rmdir and cd, and basic file handling
commands like cp, mv and rm use neither standard input nor standard output.
[Link] like ls, pwd, who etc. don’t read standard input but they write to standard
output.
[Link] like lp that read standard input but don’t write to standard output.
[Link] like cat, wc, bc etc. that use both standard input and standard output.
The commands in fourth category are called filters. When a program/command performs
operations on input and writes the result to the standard output, it is called a filter.
Example: $ bc < [Link] > [Link]
Standard input and standard output constitute two separate streams that can be individually
manipulated by the shell.
With piping, the output of a command can be used as input (piped) to a subsequent
command.
Without pipes,
$ who | wc -l
32
Splitting the output: tee.
tee handles a character stream by duplicating its input. It saves one copy in a file and writes
the other to standard output.
The following command sequence uses tee to display the output of who and saves this
output in a file as well:
33
Command substitution
Command substitution: Any command surrounded by backquotes is executed by the shell, which then
replaces the standard output of the command into the command line.
While a pipe enables a command to obtain its standard input from the standard output of another command,
the shell enables one or more command arguments to be obtained from the standard output of another
command. This feature is called command substitution.
Example:
$ echo Current date and time is `date`
Current date and time is Mon Oct 30 09:35:16 IST 2017
Observe the use of backquotes around date in the above command. Here the output of the command
execution of date is taken as argument of echo. The shell executes the enclosed command and replaces the
enclosed command line with the output of the command.
$ echo “There are `ls | wc –l` files in the current directory”
There are 47 files in the current directory
The backquote is not interpreted by the shell if enclosed in single quotes.
$ echo ‘There are `ls | wc –l` files in the current directory’
There are `ls | wc –l` files in the current directory
Command substitution has interesting application possibilities in shell scripts. It speeds up work by letting you combine a number of
instructions in one.
Department of Computer Science, RLJIT 34
Korn Shell, Bash Shell
Command substitution
Command substitution has interesting application possibilities in shell scripts. It speeds up work by letting you combine a number of
instructions in one.
35
FILTERS USING REGULAR EXPRESSIONS – grep
Example:
grep “sales” [Link]
Patterns with and without quotes is possible
Its generally safe to quote the pattern
Quote is mandatory when pattren involves more than one word
It returns the prompt in case the pattren can’t be located
grep president [Link]
When grep is used with multiple filenames, it displays the filenames along with the output
grep “director” [Link] [Link]
Where it shows filename followed by the contents
Department of Computer Science, RLJIT 36
grep options
i ignores case for matching
-v doesn’t display lines matching expression
-n displays line numbers along with lines
-c displays count of number of occurrences
-l displays list of filenames only
-e exp specifies expression with this option
-x matches pattern with entire line
-f file takes pattrens from file, one per line
-E treats pattren as an extended RE
-F matches multiple fixed strings
38
The character class
grep supports basic regular expressions (BRE) by default and extended regular expressions (ERE) with the –
E option
A regular expression allows a group of characters enclosed within a pair of [ ], in which the match is
performed for a single character in the group
grep “[aA]g[ar][ar]wal” [Link]
A single pattern has matched two similar strings
The pattern [a-zA-Z0-9] matches a single alphanumeric character. When we use range, make sure that the
character on the left of the hyphen has a lower ASCII value than the one on the right
THE *
* Zero or more occurrences of the previous character
g* nothing or g, gg, ggg, etc.
grep “[aA]gg*[ar][ar]wal” [Link]
Notice that we don’t require to use –e option three times to get the same output!!!!!
THE DOT
A dot matches a single character
.* signifies any number of characters or none
Department of Computer Science, RLJIT 39
^ and $
Most of the regular expression characters are used for matching patterns, but there are two that can match a
pattern at the beginning or end of a line
^ for matching at the beginning of a line
$ for matching at the end of a line
41
Shell variables
Environmental variables are used to provide information to the programs you use. You can have both global
environment and local shell variables.
Global environment variables are set by your login shell and new programs and shells inherit the
environment of their parent shell.
Local shell variables are used only by that shell and are not passed on to other processes. A child process
cannot pass a variable back to its parent process.
Some global environment variables are,
HOME Path to your home directory
HOST The hostname of your system
LOGNAME The name you login with
PATH Paths to be searched for commands
SHELL The login shell you’re using
42
Shell variables
Local Shell Variables
A variable assignment is of the form variable=value, but its evaluation requires the $ as prefix to the variable
name.
count=5
echo $count
total=$count You can assign value of one variable to another variable
echo $total
Note: There should not be any space around =. i.e. if we say x =5 then the shell interprets x as command
running with the =5 as argument!
43
Shell variables
Effects of Quoting and Escaping
To assign a multiword string to a variable, quoting is preferred than escaping.
message=You\ didn\’t\ enter\ the\ filename
message=“You didn’t enter the filename”
44
Shell Scripts/Shell Programs
• Group of commands have to be executed regularly, they should be Stored in a file and File
itself executed as a shell script or a shell program by the user.
• Shell scripts are executed in a separate child shell process which may or may not be same
as the login shell.
45
Shell Scripts
Example: [Link]
#! /bin/sh
# [Link]: Sample Shell Script
echo “Welcome to Shell Programming”
echo “Today’s date : `date`”
echo “This months calendar:”
cal `date “+%m 20%y”`
echo “My Shell :$ SHELL”
The # character indicates the comments in the shell script and all the characters that follow the # symbol
are ignored by the shell. However, this does not apply to the first line which beings with #.
This because, it is an interpreter line which always begins with #! followed by the pathname of the shell to
be used for running the script. In the above example the first line indicates that we are using a Bourne
Shell.
To run the script we need to first make it executable. This is achieved by using the chmod command as
shown below:
$ chmod +x [Link]
Then invoke the script name as:
$ [Link]
Department of Computer Science, RLJIT 46
The .profile
the .profile lets you to customize your operating system environment to suit your
requirements.
This environment remains in effect throughout the login session.
Every time you make changes to this file.. You should either logout and login again,or use
special command to execute file:
$ .profile
47
Read: Making scripts interactive
• The read statement is the shell’s internal tool for making scripts interactive (i.e. taking input from the user). It is used with one or more variables. Inputs
supplied with the standard input are read into these variables. For instance, the use of statement like
read name
• causes the script to pause at that point to take input from the keyboard. Whatever is entered by you will be stored in the variable
name.
• Example: A shell script that uses read to take a search string and filename from the terminal.
#! /bin/sh
# [Link]: Interactive version, uses read to accept two inputs
echo “Enter the pattern to be searched: \c”
read pname
echo “Enter the file to be used: \c”
read fname
echo “Searching for pattern $pname from the file $fname”
grep “$pname” $fname
echo “Selected records shown above”
51
exit and EXIT status of the command
C program and shell scripts have a lot in common, and one of them is that they both use the
same command to terminate a program.
• The shell offers a variable $? And a command (test) that evaluates the exit status.
|| this indicates inverse role , the second command executed only first command fails
Example 2:
$ grep ‘clerk’ [Link] || echo “Pattern not found”
Output:
Pattern not found
Department of Computer Science, RLJIT 53
Using test and [ ] to Evaluate Expressions
Test statement is used to handle the true or false value returned by expressions, and it is not possible with if
statement.
Test uses certain operators to evaluate the condition on its right and returns either a true or false exit status,
which is then used by if for making decisions.
Test works in three ways
Compare two numbers
Compares two strings or a single one for a null value
Checks files attributes
Numeric Comparison
Operator Meaning
-eq Equal to
-ne Not equal to
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal
Operators always begin with a – (Hyphen) followed by a two word character word and enclosed on either side by white space.
Numeric comparison in the shell is confined to integer values only, decimal values are simply truncated
55
The if Conditional
The if statement makes two way decision depending on the fulfillment of a certain condition.
Form 1:
if command is successful
then
execute commands
else
execute commands
fi
Form 2:
If command is successful
then
execute commands
fi
57
String Comparison
58
Example:
#!/bin/sh
#[Link] checks user input for null values finally turns [Link] developed previously
#
if [ $# -eq 0 ] ; then
echo “Enter the string to be searched :\c” Output1:
$[Link]
read pname Enter the string to be searched :[Enter]
if [ -z “$pname” ] ; then You have not entered the string
Output2:
echo “You have not entered the string”; exit $[Link]
fi Enter the string to be searched :root
Enter the filename to be used:/etc/passwd
echo “Enter the filename to be used :\c” Root:x:0:1:Super-user:/:/usr/bin/bash
read flname
if [ ! –n “$flname” ] ; then
echo “ You have not entered the flname” ; exit
fi
grep “$pname” $flname
fi
Department of Computer Science, RLJIT 59
File Tests
• test can be used to test various file attributes like its type (file, directory or symbolic links)
• its permission (read, write. Execute, SUID, etc).
$ ls –l [Link]
-rw-rw-rw- 1 kumar group 870 jun 8 15:52 [Link]
Syntax:
for variable in list
do
Commands
done
Example:
for file in ch1 ch2
do
cp $file ${file}.bak
echo $file copied to $[Link]
done
Output:
ch1 copied to [Link]
ch2 copied to [Link]
List from command substitution: Command substitution is used for creating a list. This is used when list is
large.
Example:
$ for var in `cat clist`
63
While: looping
List from wildcards: Here the shell interprets the wildcards as filenames.
Example:
for file in *.htm *.html ; do
sed ‘s/strong/STRONG/g
s/img src/IMG SRC/g’ $file > $$
mv $$ $file
done
• Syntax:
case expression in
Pattern1) commands1 ;;
Pattern2) commands2 ;;
Pattern3) commands3 ;;
…
esac
67
set and shift: Manipulating the Positional Parameters
The set statement assigns positional parameters $1, $2 and so on, to its arguments. This is used for picking up
individual fields from the output of a program.
Example 1: $ set 9876 2345 6213
This assigns the value 9876 to the positional parameters $1, 2345 to $2 and 6213 to $3
Shift: Shifting Arguments Left
• Shift transfers the contents of positional parameters to its immediate lower numbered one. This is done as
many times as the statement is called. When called once, $2 becomes $1, $3 becomes S2 and so on
Example 1:
$ echo “$@” $@ and $* are interchangeable
Mon Oct 8 08:02:45 IST 2007
$ echo $1 $2 $3
Mon Oct 8
$ shift
$ echo $1 $2 $3
Oct 8 08:02:45
$ shift 2
08:02:45 IST 2007
69
trap: interrupting a Program:
• shell scripts terminate - interrupt key is pressed.
• lot of temporary files will be stored on disk.
• The trap statement lets you do the things you want to do when a script receives a signal.
• trap placed at the beginning of the shell script uses two lists:
trap ‘command_list’ signal_list
When a script is sent any of the signals in signal_list, trap executes the commands in command_list.
The signal list can contain the integer values or names (without SIG prefix) of one or more signals – the ones
used with the kill command.
Example: To remove all temporary files named after the PID number of the shell:
trap ‘rm $$* ; echo “Program Interrupted” ; exit’ HUP INT TERM
trap is a signal handler.
Removes all files expanded from $$*, echoes a message and finally terminates the script when signals
SIGHUP (1), SIGINT (2) or SIGTERM(15) are sent to the shell process running the script.
A script can also be made to ignore the signals by using a null command list.
Example: trap ‘’ 1 2 15
72
7/3/2025