0% found this document useful (0 votes)
3 views

Filter Commands

The document provides an overview of various command-line tools in Unix/Linux, including AWK, WC, TR, HEAD, TAIL, SED, FIND, and GREP, detailing their syntax and functionalities. Each command is explained with examples, demonstrating how to manipulate text, search for files, and process data efficiently. Additionally, it covers the use of the xfs_admin command for managing XFS file systems.

Uploaded by

bharathbabu2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Filter Commands

The document provides an overview of various command-line tools in Unix/Linux, including AWK, WC, TR, HEAD, TAIL, SED, FIND, and GREP, detailing their syntax and functionalities. Each command is explained with examples, demonstrating how to manipulate text, search for files, and process data efficiently. Additionally, it covers the use of the xfs_admin command for managing XFS file systems.

Uploaded by

bharathbabu2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

 Awk Command

AWK is a programming language or a tool which can be used to perform a wide


variety of tasks, from simple tasks such as printing a message or displaying a file, to
complex programs of solving SU-DO-KU. Awk is not just any tool but a programming
language, by Aho, Weinberger, and Kernighan. It is a field or column processor (i.e.
it works basically on columns), that supports regular expressions. It can perform
search actions like “grep”.

Awk Command Syntax


awk '/optional_pattern/ { action }' file
Ex: awk '{ print }' file

awk ‘/print/’ file name

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.

Ex: $ awk -F : '{ print $1 }' /etc/passwd

 WC Command

wc counts the number of bytes, characters, whitespace-separated words, and


newlines in each given file

wc [option]… [file name]…


‘-c’ ‘--bytes’ Print only the byte counts.
‘-m’ ‘--chars’ Print only the character counts.
‘-w’ ‘--words’ Print only the word counts.
‘-l’ ‘--lines’ Print only the newline counts.
‘-L’ ‘--max-line-length’ Print only the maximum display widths. Tabs are
set at every 8th column. Display widths of wide characters are considered. Non-
printable characters are given 0 widths.
 Tr command Translate, squeeze, and/or delete characters
Syntax tr [option]… set1 [set2]

tr copies standard input to standard output, performing one of the following


operations:
1. translate, and optionally squeeze repeated characters in the result,

2. squeeze repeated characters,

3. delete characters,

4. delete characters, then squeeze repeated characters from the result.

 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 (stream editor) is a non-interactive command-line text editor.

# Example: delete the 4th line in a file

$ sed '4d' input.txt > output.txt

# Example: replace every occurrence of 'hello' with 'world' on lines 10-


20

$ sed '10,20s/hello/world/' input.txt > output.txt

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)

The options are, -name -----> search files and directories

-prem -----> search for permissions

-size -----> search for sizes (=,-, +)

-user -----> search for the owner

-uid -----> search for files/directories of uid)

-gid -----> search for files/directories of gid)

-group -----> search for group owner

-empty -----> search for empty files

-amin -----> search for access time

-mmin -----> " "

-cmin -----> " "

-atime -----> search for access day (access day, minutes, hrs, ...etc)

-mtime -----> search for modify day (change the content)

-ctime -----> search for change day (permissions, .....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 / -empty (to search empty files or directories)


# find / -empty -type f (to search for empty files only)

# find / -empty -type d (to search for empty directories only)

# 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 (to search for student user files/directories)

# find / -group student (to search for student group 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)

# find / -user student -exec cp -a { } /ram \;

(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:

grep [options] pattern [files]

Options Description

-c : This prints only a count of the lines that match a pattern

-h : Display the matched lines, but do not display the filenames.

-i : Ignores, case for matching

-l : Displays list of a filenames only.

-n : Display the matched lines and their line numbers.

-v : This prints out all the lines that do not matches the pattern

-e exp : Specifies expression with this option. Can use multiple times.

-f file : Takes patterns from file, one per line.

-E : Treats pattern as an extended regular expression (ERE)

-w : Match whole word

-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.

#2) Wildcard Character: ‘.’ Is used to match any character.

Example:“^.$” will match all lines with any single character.


#3) Escaped Characters: Any of the special characters can be matched as a regular character by escaping
them with a ‘\’.

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”.

$grep -i "UNix" file.txt

2. Displaying the count of number of matches: We can find the number of lines that matches the given
string/pattern

$grep -c "unix" file.txt

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

$grep -l "unix" f1.txt f2.txt f3.xt f4.txt

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.

$ grep -w "unix" file.txt

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.

$ grep -o "unix" file.txt

6. Show line number while displaying the output using grep -n : To show the line number of file with the
line matched.

$ grep -n "unix" file.txt

7. Inverting the pattern match : You can display the lines that are not matched with the specified search
sting pattern using the -v option.

$ grep -v "unix" file.txt


8. Matching the lines that start with a string : The ^ regular expression pattern specifies the start of a
line. This can be used in grep to match the lines which start with the given string or pattern.

$ grep "^unix" file.txt

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.

$ grep "os$" file.txt

10.Specifies expression with -e option. Can use multiple times :

$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" file.txt


1. Get xfstools with sudo apt-get install xfsprogs
2. Unmount the decvice if it is mounted (sudo umount /dev/sdb1)
3. Label it with xfs_admin -L media /dev/sdb1
4. Mount it.

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"

You might also like