Find command in Linux is more powerful than you think  - Devops productivity hacks

Find command in Linux is more powerful than you think - Devops productivity hacks

I have been a fan of the find command in Linux. Many people don't know how to use the find command to its full potential.

Trust me, this command makes my life easy every single day.

Here are some use cases where the find command will come in handy.

1. Find Files by Name

find /path -name  <filename>        

Locate files with a specific name.

2. Find Directories by Name

find /path -type d -name <dirname>        

Search for directories with a specific name.

3. Find Large Files

find /path -type f -size +100M        

Identify files larger than 100MB.

4. Execute Commands on Found Files

find /path -exec command {} \;        

Run a command on each found file.

5. Find Recently Modified Files

find /path -mtime -1        

Find files modified in the last 24 hours.

6. Find Empty Files and Directories

find /path -empty        

Locate empty files and directories.

7. Search by File Permissions

find /path -perm 644        

Find files with specific permissions, like 644.

8. Find Files by Owner

find /path -user username        

It searches for files owned by a specific user.

9. Find Files Excluding Certain Directories

find /path -path /exclude_path -prune -o -name filename        

It excludes certain directories from your search.

10. Combine Multiple Conditions

find /path \( -name '*.txt' -o -name '*.log' \) -a -mtime -7        

Combine conditions to find .txt or .log files modified in the last week.

11. Find Files by Type

find /path -type f -name "*.jpg"        

Locate all files of a specific type, like .jpg images.

12. Find Files Based on Access Time

find /path -atime +30        

Find files that were last accessed more than 30 days ago.

13. Find Files with Specific Depth

find /path -maxdepth 2 -name "*.conf"        

Search for files with a specific depth, useful for limiting the search scope.

14. Find Files Based on Size and Delete

find /path -size +1G -exec rm -i {} \;        
bash        

Copy code

find /path -maxdepth 2 -name "*.conf"

Search for files with a specific depth, useful for limiting the search scope.

14. Find Files Based on Size and Delete

find /path -size +1G -exec rm -i {} \;        

Find and interactively delete files larger than 1GB.

15. Find and Print File Details

find /path -type f -exec ls -lh {} \;        

List files with detailed information, such as permissions and sizes.


Thanks for sharing

回复

要查看或添加评论,请登录

Akhilesh Mishra的更多文章

社区洞察

其他会员也浏览了