Linux Unit 3 notes
Linux Unit 3 notes
Cat command:
• cat is one of the most frequently used commands on Unix-like operating systems.
• It has three related functions with regard to text files: displaying them, combining copies of them and creating
new ones.
wc :stands for word count. As the name implies, it is mainly used for counting purpose.
It is used to find out number of lines, word count, byte and characters count in the files specified
in the file arguments.
By default it displays four-columnar output.
First column shows number of lines present in a file specified, second column shows number of
words present in the file, third column shows number of characters present in file and fourth column
itself is the file name which are given as argument.
Syntax:
wc Filename
wc --char filename - To show hidden char
wc --lines filename - To show file lines
cmp : command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out
whether the two files are identical or not.
When cmp is used for comparison between two files, it reports the location of the first mismatch to
the screen if difference is found and if no difference is found i.e the files compared are identical.
cmp displays no message and simply returns the prompt if the files compared are identical.
Syntax:
cmp Filename1 Filename2
comm: comm compare two sorted files line by line and write to standard output; the lines that are
common and the lines that are unique.
Syntax:
comm Filename1 Filename2
diff: diff stands for difference. This command is used to display the differences in the files by
comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which lines in one
file have is to be changed to make the two files identical.
The important thing to remember is that diff uses certain special symbols and instructions that are
required to make two files identical. It tells you the instructions on how to change the first file to make it
match the second file.
Special symbols are:
a: add
c: change
d: delete
Syntax:
diff [options] File1 File2
OR
diff filename1 filename2
Attributes in Linux
Some filesystems support additional attributes (other than those described in the preceding sections). In
particular, some Linux-native filesystems support several attributes that you can adjust with the chattr
command. The files and directories can have the following attributes:
a - append only
c - compressed
d - no dump
e - extent format
i - immutable
j - data journaling
s - secure deletion
t - no tail-merging
u - undeletable
A - no atime updates
D - synchronous directory updates
S - synchronous updates
T - top of directory hierarchy
File Permissions:
When you execute an “ls” command, you are not given any information about the security of the files,
because by default “ls” only lists the names of files. You can get more information by using an “option”
with the “ls” command. All options start with a ‘-‘. For example, to execute “ls” with the “long listing”
option, you would type ls -l
When you do so, each file will be listed on a separate line in long format. There is an example in the
window below.
user – The user permissions apply only the owner of the file or directory, they will not impact the actions
of other users.
group – The group permissions apply only to the group that has been assigned to the file or directory,
they will not effect the actions of other users.
others – The others permissions apply to all other users on the system, this is the permission group that
you want to watch the most.
For example, consider that the user’s permissions for some files is “rw-” as the first three characters.
This means that the owner of the file (“aditya314”, i.e. me) can “read” it (look at its contents) and “write”
it (modify its contents). I cannot execute it because it is not a program; it is a text file.
If “r-x” is the second set of 3 characters it means that the members of the group “aditya314” can only
read and execute the files.
The final three characters show the permissions allowed to anyone who has a UserID on this Linux
system. Let us say we have the permission (“r–“). This means anyone in our Linux world can read, but
they cannot modify the contents of the files or execute it.
You can also change multiple permissions at once. For example, if you want to take all permissions
away from everyone, you would type
Using the octal notations table instead of ‘r’, ‘w’ and ‘x’. Each digit octal notation can be used of either of
the group ‘u’,’g’,’o’.
So, the following work the same.
chmod ugo+rwx [file_name]
chmod 777 [file_name]
Both of them provides full read write and execute permission (code=7) to all the group.
Same is the case with this..
Both the commands give all permissions (code=7) to user and group, read and execute (code=5) for
others.
The grep family consists of the commands grep, egrep, and fgrep. The grep command globally searches for
regular expressions in files and prints all lines that contain the expression. The egrep and fgrep commands are
simply variants of grep. The egrep command is an extended grep, supporting more regular expression
metacharacters. The fgrep command, called fixed grep, and sometimes fast grep, treats all characters as
literals; that is, regular expression metacharacters aren't special—they match themselves. The Free Software
Foundation provides a free version of grep, called GNU grep. These versions of grep are the ones used ...
Syntax:
Example:
Syntax:
Example:
grep 9 marks.txt
Look at the above snapshot, grep command do the same work as earlier example but without pipe.
grep options
o grep -vM: The 'grep -v' command displays lines not matching to the specified word.
Syntax:
Example:
grep -v 9 marks.txt
Look at the above snapshot, command "grep -v 9 marks.txt" displays lines hwich don't contain our
search word '9'.
o grep -i: The 'grep -i' command filters output in a case-insensitive way.
Syntax:
Example:
Look at the above snapshot, command "grep -i red exm.txt" displays all lines containing 'red' whether
in upper case or lower case.
grep -C command is used to display the line after and line before the result.
You can use (A1, A2, A3.....)(B1, B2, B3....)(C1, C2, C3....) to display any number of lines.
Syntax:
Look at the above snapshot, command "grep -A1 yellow exm.txt" displays searched line with next
succeeding line, command "grep -B1 yellow exm.txt" displays searched line with one preceding line and
command "grep -C1 yellow exm.txt" displays searched line with one preceding and succeeding line.
Note: The egrep command used mainly due to the fact that it is faster than the grep command. The
egrep command treats the meta-characters as they are and do not require to be escaped as is the case
with grep. This allows reducing the overhead of replacing these characters while pattern matching
making egrep faster than grep or fgrep. Options: Most of the options for this command are same
as grep.
-c: Used to counts and prints the number of lines that matched the pattern and not the lines.
-v: It prints the lines that does not match with the pattern
-l: Prints only the names of the files that matched. It does not mention the matching line numbers
or any other information.
-L: Prints only the names of the files that did not have the pattern. Opposite of
l flag.
-e: Allows to use a ‘-‘ sign in the beginning of the pattern. If not mentioned the shell tries to execute
the pattern as an option and returns an error.
-w: Prints only those lines that contain the whole words. Word-constituent characters are letters,
digits and underscore. The matching substring must be separated by non-word constituent
characters.
-x: Prints only those lines that matches an entire line of the file
-m NUMBER: Continue to search for matches till the count reaches NUMBER mentioned as
Argument.
-o: Prints only the matched parts of the line and not the entire line for each match.
-n: Prints each matched line along with the respective line numbers. For multiple files, prints the
file names along with line
numbers.
-r: Recursively search for the pattern in all the files of the directory. The last argument is the
directory to check. ‘.’ (dot) represents the current directory
Syntax:
fgrep [options] [ -e pattern_list] [pattern] [file]
Consider below file as input. Here it is create using cat command and “name of the file is para”.
Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.
Geeks*forgeeks is best for learni\ng.
-l : Used to print the names of files with matching lines once, separated by new-lines. It will not
repeat the names of files when the pattern is found more than once.
fgrep -l "geeks*forgeeks" para para2
-n : It is used precede each line by its line number in the file (first line is 1).
$ fgrep -n "learni\ng" para
-e pattern_list : Search for a string in pattern-list (useful when the string begins with a “-“).
Linux tr
The command 'tr' stands for 'translate'. It is used to translate, like from lowercase to uppercase and vice versa
or new lines into spaces.
Syntax:
Change Case
The 'tr' command can change case.
Syntax:
Example:
Look at the above snapshot, all p,r,c,u are converted into upprecase P,R,C,U.
Syntax:
Example:
Syntax:
command | tr -s <'letter'>
Example:
Look at the above snapshot, command "cat jtp.txt | tr -s 'l'" has squeezed all the letters 'l' into one and
command "cat spaces.txt | tr -s ' '" has squeezed all the spaces into single space.
Example:
Syntax:
command | tr -d <letter>
Example:
cat exm.txt | tr -d o
Look at the above snapshot, all the 'o' letters are deleted from the file 'exm.txt'.