lecture 3
lecture 3
Lecture 3:
Getting help and Command’s
Documentation
● Finding Files
● Getting a Command’s Documentation
● Pipes and filters
● File contents
Finding files (1- The locate Command)
• The “Locate” command is a fast and efficient tool for finding files and directories on a
system.
• It works by searching through a pre-built database that contains an index of file paths.
• Database updated periodically by system scheduler like cron using the updatedb
command
• Performs a rapid database search of path names, and then outputs every name that
matches a part or all of given substring. $locate filename
Locate command
• The locate command only will return results of files that the current user would normally have
access to.
• The locate command uses the -c option to count the number of matching entries
• The locate command is case sensitive use i option to ignore case: $locate i filename
• locate supports wildcard characters like * for pattern matching for example finds all files with a
.txt extension: locate '*.txt'
Page - 4
The locate Command
● Advantages
- Fast. Searches a database of all files on the computer verses the live filesystem the find command uses
● Disadvantages:
- Since locate relies on a periodically updated database, newly created or deleted files might not appear until the
next update.
- To Update the database manually using: sudo updatedb
- It doesn't show additional file metadata (e.g., file size, permissions). Use find for more detailed searches.
- The results depend on the permissions of the mlocate database file (/var/lib/mlocate/mlocate.db ) and the files
themselves.
Page - 5
2- Find command
• The find command in Linux is a powerful utility used to search for files and directories in a
directory hierarchy. Unlike locate, which relies on a pre-built database, find performs real-
time searches directly on the file system, making it more accurate and versatile.
• find Supports different search criteria options
• Searches the live filesystem which can take a large amount of time
• Slower than the locate command
• Example: find [path] [expression]
• Ex: search for files named myfile.txt in /home/user and its subdirectories by name:
find /home/user –name myfile.txt
• Ex: Searching for files by extension: find /path –name “*.txt”
• Ex: Finds directories with the specified name: find /path –type d –name dirname
Page - 6
Search with size
Ex: find directory_path -size value
● Search about the file with the size equal to the value
Ex: find directory_path -size - value
● Search about the file with the size less than the value
Page - 7
Page - 8
Which Command
• Displays the directory location(s) of the specified command or script
• Returns the location of the real command
• Searches only the directories defined by the $PATH variable
• The -a option is used to locate multiple executable files. Useful to know if an
executable script was inserted maliciously to override an existing command.
• Example:
● $ which grep
● grep: /bin/grep /usr/share/man/man1/grep.1.gz
Page - 9
nautilus Command
nautilus /path/to/directory
Page - 10
Getting command’s documentation
● Most executable programs intended for command line use provide a formal piece of
documentation called a manual or man page.
● Man pages vary somewhat in format but generally contain the following:
● A title (the page's name)
● A synopsis of the command’s syntax
● A description of the command’s purpose
● A listing and description of each of the command’s options
Page - 12
man page organization
Page - 13
man Command
• The man command will display documentation for commands: man pwd
• Control the display with movement commands:
● Space bar = Go down one page
● b = Go up one page
● g = Go to beginning of man page
● G = Go to end of man page
● h = display help screen
● /term [Enter]= Search for term
Page - 14
- Man Page Sections
• The aggregate man pages are broken into different sections based on what they do
• Nine by default, more available for custom software
• Searched in order
• Can specify a specific category:
$man 5 passwd
Page - 15
- Man Page Sections
• Each man page displays section at top:
• man -f name will return all man pages that match "name"
• man -k keyword will search all man pages for "keyword“:
$man –k “passwd”
• apropos—Display Appropriate Commands
• It is also possible to search the list of man pages for possible matches based
on a search term.
Example: search for man pages using the search term partition: $ apropos
partition
- The first field in each line of output is the name of the man page, and the
second field shows the section.
Page - 16
Whatis command
● The whatis program displays the name and a one-line description of a man page matching a
specified keyword.
Page - 17
2- info Command
• Alternative way of displaying documentation: info command.
● The info command is used to display detailed documentation about commands and programs in Linux.
● It provides more structured and in-depth information than the man (manual) pages.
• Not all commands have info pages
Navigating info Pages
Once inside an info page, you can use:
•Arrow keys → Scroll up/down Spacebar → Move forward
•Backspace → Move backward Tab → Navigate links
•Enter → Select a menu item l → Go back one node
• q → quite
● Use pinfo for Better Navigation
pinfo is an alternative info viewer that works more like less/man
Page - 18
3- help command
• Bash has a built-in help facility available for each of the shell built-ins.
● To use it, type help followed by the name of the shell built-in.:
$help cd
● Many executable programs support a --help option that displays a description
● of the command's supported syntax and options.
$cat --help
• System documentation located in either /usr/share/doc or /usr/doc directories
● on-line help: http://tldp.org/ (the linux documentation project)
Page - 19
Pipes and Filters
• You can connect two commands together so that the output from one program
becomes the input of the next program. Two or more commands connected in this
way form a pipe.
• To make a pipe, put a vertical bar (|) on the command line between two commands.
• When a program takes its input from another program, it performs some operation
on that input, and writes the result to the standard output. It is referred to as a filter.
Page - 21
Pipeline
• In Linux, a pipeline is a mechanism that allows two or more processes to be combined or
executed concurrently. That means the process output will be handled as an input for the
next one, and so on.
• It's not called a pipeline for nothing: It refers to the concept of a process flow being
channeled through a pipe from a source to a destination The "|" symbol is used between
two commands to represent a command pipeline
Page - 22
The grep Command
• The grep command searches a file or files for lines that have a certain pattern.
• The syntax is : $grep pattern file(s)
• The name "grep" comes from the ed (a Unix line editor) command g/re/p which
means “globally search for a regular expression and print all lines containing it”.
• A regular expression is either some plain text (a word, for example) and/or special
characters used for pattern matching.
• The simplest use of grep is to look for a pattern consisting of a single word
Page - 23
The grep Command
• It can be used in a pipe so that only those lines of the input files containing a given
string are sent to the standard output.
• There are various options which you can use along with the grep command −
Sr.No. Description
1 -n Sorts numerically (example: 10 will sort after 2),
ignores blanks and tabs.
2 -r Reverses the order of sort.
3 -f Sorts upper and lowercase together.
4 +x Ignores first x fields when sorting.
Page - 25
Pipe
• More than two commands may be linked up into a pipe. Taking a previous pipe
example using grep, we can further sort the files modified in August by the order of
size.
• The following pipe consists of the commands ls, grep, and sort − $ls -l | grep "Aug"
| sort +4n
• This pipe sorts all files in your directory modified in August by the order of size, and
prints them on the terminal screen.
• The sort option +4n skips four fields (fields are separated by blanks) then sorts the
lines in numeric order.
Page - 26
Filters
● The capability of commands to read data from standard input and send to standard
output is utilized by a shell feature called pipelines.
● These filters are very small programs that are designed for a specific function which
can be used as building blocks.
● Using the pipe operator |, the standard output of one command can be piped into the
standard input of another.
● Linux Filter Commands
cat, cut, grep, awk, comm, sed, tee, tr, uniq, wc, od, sort, gzip
Page - 27
Cat Filters
Page - 28
cut Command
● Linux cut command is useful for selecting a specific column of a file.
● It is used to cut a specific sections by byte position, character, and field and writes them to
standard output.
● To cut a specific section, it is necessary to specify the delimiter. A delimiter will decide
how the sections are separated in a text file. Delimiters can be a space (' '), a hyphen (-), a
slash (/), or anything else. After '-f' option, the column number is mentioned.
● To cut by using the hyphen (-) as the delimiter:
$cut -d- -f(columnNumber) <fileName>
● To cut by using space as a delimiter:
$cut -d’ ‘ -f(columnNumber) <fileName>
● To cut by specified character:
$cut -c < characters> <file name>
Page - 29
awk command
● The awk command is a powerful text-processing tool in Linux used for searching, filtering, and
manipulating text files.
● It processes text line by line, splitting fields, and applying actions.
● Basic Syntax: awk 'pattern {action}' filename
•pattern → Specifies when to apply the action.
•action → Specifies what to do when the pattern matches.
If no pattern is specified, awk applies the action to every line
•Ex: awk '{print $1, $3}' file.txt this means that:
•$1 → First column
•$3 → Third column
•print → Prints the selected fields
Page - 31
Linux comm
● The 'comm' command compares two files or streams. By default, 'comm' will always
display three columns.
● First column indicates non-matching items of first file. (unique for file1)
● Second column indicates non-matching items of second file. . (unique for file2)
● Third column indicates matching items of both the files. (Common lines in both)
● Both the files has to be in sorted order for 'comm' command to be executed.
● Syntax: $ comm <file1> <file2>
● Ex. comm file1.txt file2.txt
● If you want to output a single column, you have to specify number of the columns which
are not to be displayed.
● Syntax: comm -23(To display first column)
Page - 32
wc: Print Line, Word, and Byte Counts
● The wc (word count) command is used to display the number of lines, words, and bytes
contained in files.
● In this case, it prints out three numbers: lines, words, and bytes contained in file.
● The -l option limits its output to report only lines.
Page - 33
Quiz time
1) The ______command is used for searching files and directories which uses a database.
a) find b) man c) Locate d) info
2) ___command displays the directory location of the specified command or script
who b)which c) whereis d) whoami
3) The ____ command searches a file or files for lines that have a certain pattern.
a) file b) grep c) locate d) which
4) To view file contents type, use the ____command
a) which b) file c) touch d) whatis
5) Which options of rm command is used to remove a directory with all its subdirectories forcibly?
a) delete b) rmdir c) rm –rf d) rm
6) How do you specify a different field separator in awk?
a) awk -F "separator“ b) awk -S "separator"
c) awk -d "separator“ d) awk -sep "separator"
7) What is the primary purpose of the awk command in Linux?
a) Searching and filtering files b) Compiling programs
c) Managing user permissions d) Formatting disk partitions
8) What does the tac command do?
a) Prints file content in reverse order b) Removes duplicate lines from a file
c) Filters specific columns from a file d) Compares two files line by line
Page - 34
Quiz time
1) The ______command is used for searching files and directories which uses a database.
a) find b) man c) Locate d) info
2) ___command displays the directory location of the specified command or script
who b)which c) whereis d) whoami
3) The ____ command searches a file or files for lines that have a certain pattern.
a) file b) grep c) locate d) which
4) To view file contents type, use the ____command
a) which b) file c) touch d) whatis
5) Which options of rm command is used to remove a directory with all its subdirectories forcibly?
a) delete b) rmdir c) rm –rf d) rm
6) How do you specify a different field separator in awk?
a) awk -F "separator“ b) awk -S "separator"
c) awk -d "separator“ d) awk -sep "separator"
7) What is the primary purpose of the awk command in Linux?
a) Searching and filtering files b) Compiling programs
c) Managing user permissions d) Formatting disk partitions
8) What does the tac command do?
a) Prints file content in reverse order b) Removes duplicate lines from a file
c) Filters specific columns from a file d) Compares two files line by line
Page - 35
orl
Thank you !