Search related Linux Command

Search-related Linux commands are commands used for searching files, directories, and content within a file system

Some common search-related Linux commands include:


Man Pages

The man command is used to display the manual pages for a Linux command. It stands for manual.

Syntax: 

man [command]

When used with a command name as an argument, it displays the manual page for that command. The manual pages contain information about the command's usage, options, return codes, etc.

The manual pages are organized into sections, each section providing specific information about the command:

  • Section 1: User commands
  • Section 2: System calls
  • Section 3: Library functions
  • Section 4: Devices and special files
  • Section 5: File formats and conventions
  • Section 6: Games
  • Section 7: Miscellaneous
  • Section 8: System administration commands and daemons

You can also use the option -s followed by a section number to display manual pages from a specific section.

For example, man ls displays the manual page for the ls command and man -s 2 fork displays the manual page for the fork system call.

username@TechnoScience:~$ man ls

This is a man (from the manual) page. Man pages are an essential tool to learn, as a developer. They contain so much information that sometimes it's almost too much.


Command Line Help

The "help" command is a shell built-in command in most Unix-like operating systems. It displays information about built-in shell commands and functions. The information provided by "help" varies by shell and system, but typically includes a brief description of the command and its options. 

username@TechnoScience:~$ ls --help

The "help" command can be useful for quickly looking up information on shell commands without having to switch to a manual page or online documentation. For example, in a Bash shell, typing "help echo" will provide information on the "echo" command.


Which

The "which" command in Linux is used to find the location of a command in the system PATH. It displays the full path of the executable file associated with the given command. It is commonly used to determine the location of a binary file to ensure it is not a malicious file.

Usage:

which [command]

The "which" command takes one argument: the name of the command for which the location is being searched.

Example:

username@TechnoScience:~$ which ls
/bin/ls
username@TechnoScience:~$

In the above example, the "which" command was used to find the location of the ls command. The output indicates that the ls command is located at /bin/ls.


Whereis

"whereis" is a Linux command that can be used to locate the source, binary, and manual page files for a specified command. It is used to search the standard Linux directories for specific executables, libraries, and manual pages, and returns the path to these files.

Syntax:

whereis [OPTIONS] name

Options: 

  • -b, --binary: Search only for executable files 
  • -m, --man: Search only for manual pages 
  • -s, --source: Search only for source files 
  • -B path, --block-size=path: Use a blocksize of path 
  • -M path, --manpath=path: Specify man search path 
  • -f, --fill-holes: Fill in any holes in the manual section with system manuals 
  • -u, --update: Update the database used by whereis


Examples:

username@TechnoScience:~$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
username@TechnoScience:~$ whereis -m ls
ls: /usr/share/man/man1/ls.1.gz
username@TechnoScience:~$ whereis -b -m ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
username@TechnoScience:~$


Locate

locate is a command-line utility in Unix-like systems for finding files based on their names. It uses a pre-built database of file locations for faster search results compared to a full directory search with find.

Syntax:

locate [OPTION]... PATTERN...
Options:
  • -c or --count: Display only the total number of matches.
  • -d or --database=DB: Use the specified database.
  • -i or --ignore-case: Case-insensitive search.
  • -l or --limit=N: Limit the number of results to N.
  • -r or --regexp: Search using regular expressions.
  • -v or --version: Show version information and exit.
Example:

username@TechnoScience:~$ locate bash

This will show all files with the name bash in the database. 

Note that the database must be updated regularly to ensure accuracy, usually through the updatedb command.


Find

The find command is used in Unix/Linux to search for files and directories based on various criteria like name, type, size, date, etc. It recursively searches the directory tree starting from a specified directory and returns a list of files that match the specified conditions.

The basic syntax of the find command is:

find [path] [expression]

where "path" is the starting directory for the search and expression is a set of options and tests used to match the desired files.

Examples:

Find all the files under the current tree that have the ".txt" extension and print the relative path of each file matching:

username@TechnoScience:~$ find . -name "*.txt"
./.local/share/Trash/files/newfile3.txt
./.local/share/Trash/files/newdir/sym_link_file.txt
username@TechnoScience:~$

It's important to use quotes around special characters like "*to avoid the shell interpreting them.

Find all files that were modified in the last 7 days:

username@TechnoScience:~$ find . -mtime -7
.
./Downloads
./Downloads/systemd-udevd.dll
./.vboxclient-vmsvga-session-tty7.pid
./file2
./.tldr
./.bashrc
./.dmrc
./Documents
./dogs
./.config
./.config/pcmanfm
<SNIP>

For complete details and options available, refer to the find manual page by running man find in a terminal. 

These commands are used to search for files or directories based on name, content, or other attributes. They provide quick and efficient ways to search and locate information within a Linux system.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!