Table of Contents
Introduction
The find
command in Linux is one of the most powerful tools for system administrators and developers. It allows users to search for files and directories based on a variety of criteria such as name, type, size, permissions, and modification time. Whether you’re troubleshooting, managing files, or automating tasks, mastering the find
command can significantly boost your productivity.
In this article, we’ll explore the ins and outs of the find
command, starting with the basics and moving to advanced use cases. By the end, you’ll have a strong grasp of how to wield this versatile tool effectively.
How to Use the find Command in Linux
Basic Syntax of the find
Command
The general syntax of the find
command is as follows:
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
External Resources
Conclusion
The find
command in Linux is an indispensable tool for locating files and directories based on a variety of criteria. From simple name searches to advanced permission-based queries, find
offers unmatched flexibility. Whether you’re a system administrator or a casual user, mastering this command can streamline your workflow and improve efficiency.
Start experimenting with the examples provided in this guide to become proficient in using the find
command. For more details, refer to the official Linux documentation linked above. Thank you for reading the DevopsRoles page!
1 thought on “find command in Linux with Examples”