Filter Commands
Filter Commands
The default field separator of awk is whitespace, but if we want to specify some
other column separator, we can use -F option. In /etc/passwd file, the columns are
separated by a colon (check the above output), so for this file, we use “:” as our
field separator. $1, $2, etc. are awk variables that specify the column number. $0
represents entire line. “awk -F : '{ print $1 }' /etc/passwd” will print only the first
column of the file.
WC Command
3. delete characters,
Head Command prints the first part (10 lines by default) of each file; it reads
from standard input if no files are given or when given a file of -.
Synopsis:
head [option]… [file]…
If more than one file is specified, head prints a one-line header consisting of:
==> file name <==
Tail command prints the last part (10 lines by default) of each file; it reads from
standard input if no files are given or when given a file of ‘-’.
Synopsis:
tail [option]… [file]…
If more than one file is specified, tail prints a one-line header before the output for
each file, consisting of:
==> file name <==
sed is commonly used to filter text, i.e., it takes text input, performs some operation
(or set of operations) on it, and outputs the modified text. sed is typically used for
extracting part of a file using pattern matching or substituting multiple occurrences
of a string within a file.
Find Command
find command required the specific location. Without specific location we cannot find the files or
Directories.
# find <location> <options> <file or directory> -type f/d (to find the specific file or directory)
-atime -----> search for access day (access day, minutes, hrs, ...etc)
Examples :
# find / -name <file name> (to search for file names in / directory)
# find / -name <file name> -type f (to find file names only)
# find / -name <directory name> -type d (to find directories with small letters only)
# find / -iname <file/directory name> (to search for small or capital letter files/directories)
# find / -name " *.mp3" (to search for .mp3 files only)
# find / -size 10M (to search for exact 10M size file/directories)
# find / -size -10M (to search for less than 10M size files/directories)
# find / -size +10M (to search for greater than 10M size files/directories)
# find / -user student -not -group student (to search for student user files and not
student group files)
# find / -user student -o -group student (to search for student user and student group
files/directories)
# find / -uid <uid no.> (to search for files/directories which belongs to the user
having the specified user id)
# find / -gid <gid no.> (to search for files/directories which belongs to the group
having the specified group id)
# find / -prem 755 (to search file/directories which are having the permissions 755)
# find / -prem -755 (to search file/directories which are having the permissions
below 755 and also at least one match also)
# find / -mmin 20 (to search for files/directories which are modified within 20 minutes,
+20 ----> above 20 minutes and -20 -----> below 20 minutes)
# find / -mtime 2 (to search files/directories which are modified within 2 days)
# find / -name "*.mp3" -exec rm -rf { } \; (to search all .mp3 files and delete them)
# find / -name "*.mp3" -exec cp -a { } /ram \ ;(to search all mp3 files and copy them into /ram
directory)
(to search student user's files and directories and copy them into /ram directory)
# find / -nouser -exec mv -a { } /home/ram \; (to search files/directories which are not
belongs to any user and move them into /home/ram directory)
# du -h / |sort -r |head -n 10
GREP
The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that
pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for
globally search for regular expression and print out).
Syntax:
Options Description
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-o : Print only the matched parts of a matching line, with each such part on a separate output line.
#1) Anchor Characters: ‘^’ and ‘$’ at the beginning and end of the pattern are used to anchor the
pattern to the start of the line, and to the end of the line respectively.
Example: “^Name” matches all lines that start with the string “Name”. The strings “\<” and “\>” are used
to anchor the pattern to the start and end of a word respectively.
Example: “\$\*” will match the lines that contain the string “$*”
#4) Character Range: A set of characters enclosed in a ‘[‘ and ‘]’ pair specify a range of characters to be
matched.
1. Case insensitive search: The -i option enables to search for a string case insensitively in the give file. It
matches the words like “UNIX”, “Unix”, “unix”.
2. Displaying the count of number of matches: We can find the number of lines that matches the given
string/pattern
3. Display the file name that matches the pattern: We can just display the files that contain the given
string/pattern.
$grep -l "unix" *
or
4. Checking for the whole words in a file : By default, grep matches the given string/pattern even if it
found as a substring in a file. The -w option to grep makes it match only the whole words.
5. Displaying only the matched pattern : By default, grep displays the entire line which has the matched
string. We can make the grep to display only the matched string by using the -o option.
6. Show line number while displaying the output using grep -n : To show the line number of file with the
line matched.
7. Inverting the pattern match : You can display the lines that are not matched with the specified search
sting pattern using the -v option.
9. Matching the lines that end with a string : The $ regular expression pattern specifies the end of a line.
This can be used in grep to match the lines which end with the given string or pattern.
You can use the xfs_admin command to modify an unmounted XFS file system. For example, you can enable or disable lazy
counters, change the file system UUID, or change the file system label.
To display the existing label for an unmounted XFS file system and then apply a new label:
# xfs_admin -l /dev/sdb
label = ""
# xfs_admin -L "VideoRecords" /dev/sdb
writing all SBs
new label = "VideoRecords"