20
UNIX /LINUX FILE/DIRECTORY-3
PERMISSIONS
LECTURE AGENDA
Manipulating Files:
grep, uniq, wc
find, sort
cut, paste
Permission commands
FIND COMMAND
4
Allows you to find files and directories in a directory
hierarchy and perform subsequent operations on
them.
Syntax: $ find [pathname] [-name filename]
Example
Search for a file by pattern
$ find /home -name "*.txt"
Search for a file by specific name
$ find /home -name file.txt
SORT COMMAND
5
sorts lines contained in a group of files alphabetically
Syntax: $ sort [options] [filenames]
EXAMPLE
$ sort file.txt displays records in ascending order
$ sort -n file.txt (Numeric numbers)
$ sort -r file.txt(reverse order)
$ sort –k 2 file1.txt(sorting table)
Apple orange
Grapes banana
Pears plum
Cherries lemon
GREP COMMAND
6
Print Lines Matching a Pattern
$ grep[-options] pattern filenames
EXAMPLE
$ grep hello *.txt
$ grep ‘boo’ /etc/passwd
GREP COMMAND
Perform case insensitive search for the
string boo
$ grep -i ‘boo’ /etc/passwd
To search and display total number of
times that string appears in that file
$ grep -c ‘boo’ /etc/passwd
DISCOVERY QUESTION
14
What does the option (-r) do in case of the grep
command?
UNIQ COMMANDS
7
uniq accepts a sorted list of data from either
standard input or a single filename argument
(see the uniq man page for details) and, by
default, removes any duplicates from the list.
Example: $ uniq food.txt
apple
apple
banaba
banaba
cherry
grapes
WC COMMAND
8
The wc (word count) command is used to display
the number of lines, words, and bytes contained
in files. For example:
Example: $ wc food.txt
4 8 53
food.txt
Apple orange
Grapes banana
Pears plum
Cherries lemon
CUT COMMAND
10
The cut command is used to extract a section of
text from a line and output the extracted section
to standard output. It can accept multiple file
arguments or input from standard input.
Syntax : cut [options] filename
CUT COMMAND
11
Option Description
-c char_list Extract the portion of the line defined by char_list.
The list may consist of one or more comma-separated
numerical ranges.
-f field_list Extract one or more fields from the line as defined by
field_list. The list may contain one or more fields or
field ranges separated by commas.
-d delim_char When -f is specified, use delim_char as the field
delimiting character. By default, fields must be
separated by a single tab character.
--complement Extract the entire line of text, except for those
portions
specified by -c and/or -f.
CUT COMMAND: EXAMPLE
12
Example: $ cut -d' ' –f2 food.txt
orange
banana
plum
lemon
Apple orange
Grapes banana
Pears plum
Cherries lemon
PASTE COMMAND
13
Combines the contents of one or more files
to output to the screen or another file
Syntax: paste [-options] source files[>destination file]
Example: paste –d ‘,’ vegetables.txt meat.txt > food.txt
Carrots Chicken Carrots,Chicken
Spinach Turkey Spinach,Turkey
Lettuce Beans Beef Lamb Lettuce,Beef
vegetable.txt meat.txt Beans,Lamb
food.txt
DISCOVERY QUESTION
14
What does the option (-s) do in case of the
paste command?
PERMISSIONS
21
As a multi-user system Unix/Linux relies on permissions to
make sure the right user is able to do the right things.
Permissions manage who can read, write, or execute files.
PERMISSION
22
$ > foo.txt
$ ls -l
foo.txt
EXPLAINED
23
Attribute File Type
- A regular file.
d A directory.
l A symbolic link. Notice that with symbolic links, the remaining file
attributes are always rwxrwxrwx and are dummy values. The real
file attributes are those of the file the symbolic link points to.
c A character special file. This file type refers to a device that
handles data as a stream of bytes, such as a terminal or modem.
b A block special file. This file type refers to a device that handles
data in blocks, such as a hard drive or CD-ROM drive.
TYPES OF FILES - PERMISSION
We can use ls –l command to know the type of the file
Example1: ls –l /dev/ram
lrwxrwxrwx 1 root root 4 Mar 22 2020 /dev/ram -> ram1
symbolic link.
Example 2: ls –l /dev/tty
crw-rw-rw- 1 root tty 5, 0 Jan 24 11:29 /dev/tty
b or c indicates a device file
Example 3: ls –ld directoryname
drwx------ 3 Alice staff 4096 Jan 26 16:57 directoryname
d indicates directory
Example 4: ls –l filename
-rw------- 1 n01547728 staff 0 Jan 26 16:18 week3
- indicates a regular file
PERMISSIONS EXPLAINED
24
Attributes Files Directories
r (read) Allows a file to be opened and read. Allows a directory’s contents to
be listed if the execute attribute
is also set.
w (write) Allows a file to be written to or truncated; Allows files within a directory to
however, this attribute does not allow files to be be created, deleted, and
renamed or deleted. renamed if the execute attribute
The ability to delete or rename files is determined is also set.
by directory attributes.
x (execute) Allows a file to be treated as a program and Allows a directory to be entered;
executed. Program files written in scripting e.g.,
languages must also be set as readable to be cd directory.
executed.
CHMOD
COMMAND
23
This command is used to change the mode
(permissions) of a file or directory.
$ chmod [options] filename(s)
CHMOD COMMAND
24
Only the file’s owner or the superuser can change
the mode of a file or directory.
chmod supports two distinct ways of specifying
mode changes:
octal
number representation
symbolic representation.
…
CHMOD COMMAND
(CONT’D)
25
Permission Mapping
Octal Binary File Mode
0 000 ---
1 001 --x
2 010 -w-
3 011 -wx
4 100 r--
5 101 r-x
6 110 rw-
7 111 rwx
CHMOD COMMAND
(CONT’D)
26
Octal representation: permissions may be
specified as a sequence of 3 octal digits
Example:
$ chmod 600 private.txt
$ chmod 711 data
CHMOD COMMAND (CONT’D)
27
Symbolic representation: Permissions may be specified
symbolically
Using symbols: u (user), g (group), o (other), a (all), r
(read), w (write), x (execute), + (add permission),
- (take away permission) and = (assign permission).
Example:
$ chmod u=rw,go-rw private.txt
$ chmod g-w,o+x prog1
CHMOD COMMAND
(CONT’D)
28
The chmod command can be used with the
–R option:
$ chmod -R go+r Playground
READING ASSIGNMENTS
37
Sobell, Mark G. A Practical Guide to Ubuntu
Linux. (Humber library eBook):
Chapter 5, Basic Utilities:
Pages 160-170.
Chapter 6, The Linux File System:
Pages 215-220.
REFERENCES
38
Palmer, Michael, Jack Dent, and Tony Gaddis.
Guide to UNIX using Linux. 3rd ed. Boston, Mass:
Thomson/Course Technology, 2005.
Petersen, Richard. Linux, Programmer's Reference.
2nd ed. Berkeley, Calif: Osborne McGraw-Hill,
2000.
Sobell, Mark G. A Practical Guide to Ubuntu Linux.
Fourthition. ed. Upper Saddle River, NJ: Prentice
Hall, 2015.