find command in Linux with Examples

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

find command in Linux with Examples

Syntax

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
 /home/vagrant/DevopsRoles/Devops/huupv.csv

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

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

//find all files not ending in ".csv"
[vagrant@DevopsRoles ~]$ find /home/vagrant -type f -not -name "*.csv"
 /home/vagrant/.bash_logout
 /home/vagrant/.bash_profile
 /home/vagrant/.bashrc
 /home/vagrant/.ssh/authorized_keys
 /home/vagrant/DevopsRoles/Devops/xxx
 /home/vagrant/Devops.zip
 /home/vagrant/.bash_history

//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
 /home/vagrant/DevopsRoles/Devops/a.java
 /home/vagrant/DevopsRoles/Devops/b.java
 /home/vagrant/home/vagrant/DevopsRoles/Devops/a.java
 /home/vagrant/home/vagrant/DevopsRoles/Devops/b.java
 [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 Devops.zip
 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 
 home/vagrant/DevopsRoles/Devops/a.java
 home/vagrant/DevopsRoles/Devops/b.java
 home/vagrant/home/vagrant/DevopsRoles/Devops/a.java
 home/vagrant/home/vagrant/DevopsRoles/Devops/b.java

Conclusion

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, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

1 thought on “find command in Linux with Examples

Leave a Reply

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

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