find command in Linux with Examples

find command in Linux means Search for files in a directory hierarchy.

find command in Linux with Examples


find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point…] [expression]

On the man page, the describes it.

  • find– search for files in a directory hierarchy.
  • man find– More details information about find command.

find command in Linux with Examples

$ find /home/huupv -name devopsroles.txt
// Full command
 [vagrant@DevopsRoles ~]$ find /home/vagrant/ -name huupv.csv -type f -print

// Search DevopsRoles directory
[vagrant@DevopsRoles ~]$ find /home/vagrant/ -name DevopsRoles -type d

// Search Multiple Directory
[vagrant@DevopsRoles ~]$ sudo find /opt /tmp /home/vagrant -name huupv.csv -type f

//find all files not ending in ".csv"
[vagrant@DevopsRoles ~]$ find /home/vagrant -type f -not -name "*.csv"

//change csv files to mode 644
[vagrant@DevopsRoles ~]$ find /home/vagrant -name "*.csv" -type f -exec chmod 644 {} \;

//run ls command on files found with find command
[vagrant@DevopsRoles ~]$ find /home/vagrant -name "*.csv" -type f -exec ls -ld {} \;
 -rw-r--r--. 1 vagrant vagrant 0 Oct  1 06:45 /home/vagrant/DevopsRoles/Devops/huupv.csv

//How to find and copy file .csv to folder /tmp/
[vagrant@DevopsRoles ~]$ find /home/vagrant -type f -name "*.csv" -exec cp {} /tmp/ \;

How to find and delete

vagrant@DevopsRoles ~]$ find /home/vagrant/ -type f -name "huupv*" -exec rm {} \;

Find files by modification time

[vagrant@DevopsRoles ~]$ find /home/vagrant/ -mtime 1               # 24 hours
[vagrant@DevopsRoles ~]$ find /home/vagrant/ -mtime -7              # last 7 days

tar command with the find command

[vagrant@DevopsRoles ~]$ find /home/vagrant -type f -name "*.java" | xargs tar zcvf ./myfile.tar
 tar: Removing leading `/' from member names
 [vagrant@DevopsRoles ~]$ ll
 total 8
 drwxrwxr-x. 3 vagrant vagrant  20 Oct  1 06:51 DevopsRoles
 -rw-rw-r--. 1 vagrant vagrant 788 Oct  1 07:00
 drwxrwxr-x. 3 vagrant vagrant  21 Nov 11 15:22 home
 -rw-rw-r--. 1 vagrant vagrant 178 Nov 11 15:23 myfile.tar
 [vagrant@DevopsRoles ~]$ tar -xvf myfile.tar 


find command is a simple command in Linux. It is the most popular in use terminal Linux search for files in a directory hierarchy. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment moderation is enabled. Your comment may take some time to appear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.