How to Search for Files in Linux

The find command allows you to search for files based on name, type, size, and modification time.


A. Search by File Name

find /home -name "file.txt"

/home → Directory to search in.

-name "file.txt" → Searches for a file with an exact name.

Case-Insensitive Search:


find /home -iname "file.txt"

Use -iname to ignore case differences.


B. Search by File Type

find /home -type f -name "*.pdf"

-type f → Searches only for files (not directories).

-name "*.pdf" → Finds all .pdf files.

C. Search by File Size

find / -size +100M

Finds files larger than 100MB.

Replace + with - to find files smaller than the specified size.

D. Search by Modification Date

find / -mtime -7

Finds files modified in the last 7 days.

2. Using locate for Fast Searches

The locate command is faster than find as it searches a pre-built index of files.


A. Install locate (if not installed)

sudo apt install mlocate   # Debian/Ubuntu  

sudo yum install mlocate   # RHEL/CentOS  

sudo pacman -S mlocate     # Arch Linux  

B. Update the File Index

sudo updatedb

C. Search for a File

locate file.txt

Tip: Use locate -i for case-insensitive searches.


3. Using grep to Search Inside Files

The grep command helps locate files containing specific text.


A. Search for a Word in a File

grep -r "error" /var/log/

-r → Recursively searches all files in /var/log/.

B. Search for a Word in .txt Files

grep "password" *.txt

4. Using which, whereis, and type for Executable Files

A. Find the Location of a Program (which)

which python

Returns: /usr/bin/python (or similar).


B. Locate System Files (whereis)

whereis firefox

Displays paths for the binary, source, and manual pages.


C. Check If a Command Exists (type)

type ls

5. Using GUI Search Tools

For users who prefer graphical search tools:


GNOME Files (Nautilus) → Press Ctrl + F in Files.

KFind (KDE) → Run kfind for advanced search options.

Catfish → A lightweight search tool for Linux.

6. Using fd (Faster Alternative to find)

The fd command is a modern and faster alternative to find.


A. Install fd

bash

sudo apt install fd-find   # Debian/Ubuntu  

sudo pacman -S fd          # Arch Linux  

B. Search for a File

fd "file"

Finds all files containing "file" in their name.


7. Using ranger (Terminal File Manager)

For a visual file manager inside the terminal:


A. Install ranger

sudo apt install ranger

B. Open ranger

ranger

Use / inside ranger to search for files interactively.


Comments

Popular posts from this blog

Absolute and relative path in HTML pages

Errors

goto PHP operator