How To Use Linux find Command To Locate files

Cpanel/Whm License $3/mo Plesk License $10/mo Cloudlinux License $5/mo

(: July 30, 2019)

Introduction

Searching for a particular file in your system can be such a hassle if you do not have the knowledge of the right tools to use to ease your work. Maybe there is that config file you are not sure where to find it and you are left to search for it. The find command will help you find it.

This command simply searches for files in a directory hierarchy. It is a powerful command and can search for files based on various criteria such as permissions, modification time, ownership, size. It can also find files and modify them such as changing their permissions and even deleting them.

The simplified syntax goes like this

$ find [options] [path or directory…] [your-expression]

Examples of using find command

1. Search files with given extension

Search all files with .pdf extension on the /home/tech/ directory

.td_uid_2_5d9099a4e7b35_rand.td-a-rec-img{text-align:left}.td_uid_2_5d9099a4e7b35_rand.td-a-rec-img img{margin:0 auto 0 0}
$ find /home/tech/ -name "*.pdf"

Search all files with .pdf extension on the current directory

$ find . -name "*.pdf"

2. Search by type using the -type option

You can specify multiple parameters using the -type option as follows:

  • f: To search for a regular file
  • d: To search for a directory
  • l: To search for a symbolic link
  • c: To search for character devices
  • b: To search for block devices
  • p: To search for a named pipe (FIFO)
  • s: To search for a socket

Search multiple directories for a file httpd.conf

$ find /home /etc /var -name httpd.conf -type f 

Find all directories in root(/) with the name django.

$ find / -type d -name django

3. Find files with specific permissions

This command will find files with mode 644 and prints them out.

$ find /home -type f -perm 0644 -print

4. Locate files based on size

To find files with size bigger than 10 Mebibyte and sort them by size:

find . -size +10M -type f -print0 | xargs -0 ls -Ssh | sort -z

Manipulating files

Find files and directories with a given permission and changing the permissions

## Find files with 755 mode and change to 777
find /home -type f -perm 0755 -print -exec chmod 777 {} ;

## For Directories
find /home -type d -perm 0755 -print -exec chmod 777 {} ;

Find and delete files based on search criteria.

find /etc -type f -name "*.pdf" -exec rm -f {} ;
find ./path/ -name '*.txt' -exec rm '{}' ;

Find files based on the time it was modified

find /etc/selinux -name "*.conf" -mtime 5

The above finds files with names that end with the characters conf in the specified directory and have been modified in the previous 5 days.

Find and delete files compressed files in /data directory older than 7 days.

find /data/*.gz -mtime +7 -exec rm -rf {} ;

Find and delete files and directories

find /var -name "*.backup" -delete
find / -type f -name *.txt -exec rm {} ;
find / -type d -name flask -exec rm -rf {} ; ## directory

Find and delete empty directories in the current working parent directory.

find . -type d -empty -exec rmdir {} ;

Find files by ownership

Find all files owned by tech user

find /home -user tech

Find pdf files owned by user tech in the /var directory

find /var -user tech -iname "*.pdf"

Find all files owned by accounts group

find /home -group accounts

Finding files based on their sizes

The find command can search and find files when you specify the sizes you desire. To find all 70MB files in /var directory, do

find /var -size 70M

Since you can match files with their sizes, you can also delete them as follows

find /var -size +70M -exec rm -rf {} ;

Conclusion

There you go guys. Now you can be able to find your files with ease and achieve even more using the utility. Thank you for reading through and we hope it was informative.

Similar:

How to extract .xz files on Linux – CenOS / Ubuntu / Debian

How to use scp command to securely transfer files with examples

Lnav – Easy way to check Log files on Linux

How to empty (truncate) Log files in Linux

Learning:

Best Linux Books for Beginners & Experts

.td_uid_4_5d9099a4e7d3e_rand.td-a-rec-img{text-align:left}.td_uid_4_5d9099a4e7d3e_rand.td-a-rec-img img{margin:0 auto 0 0}

Related posts

FreeBSD Install Strace – A Process Tracer And Debugging Tool

SXI ADMIN

Improving Security for your Remote Desktop Connection

SXI ADMIN

A Network Analyst’s View of the Block Chain

SXI ADMIN

Bitcoin Faces Minor Price Retreat Amid Increasing Bull Exhaustion

SXI ADMIN

R3 Completes Blockchain Test With 11 Banks

SXI ADMIN

Back Above $5,500: Bitcoin Shrugs Off Fork with Price Rebound

SXI ADMIN

PBoC Official Calls for Wider Ban on Chinese Crypto Trading: Report

SXI ADMIN

Video: How ShapeShift CEO Erik Voorhees Makes Sense of Crypto Assets

SXI ADMIN

Huobi Will Now Take Your Bitcoins as Stock Trading Collateral

SXI ADMIN

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More