Linuxopsys
Linuxopsys

@linuxopsys

20 Tweets 1 reads Feb 16, 2023
15 useful Linux find command practical examples πŸ§΅β†“
{ 1 } Find empty files and delete them:
This will delete all the empty files in the /home/linuxopsys directory.
$ find /home/linuxopsys/ -empty -delete
{ 2 } Find big files taking too much space so you can delete them blindly:
This will delete files which are greater than 47000k
$ find /home/linuxopsys/ -type f -size +45000k -exec ls -l {} \; | awk '{ print $9 }' | xargs rm -i >/dev/null
{ 3 } Find a specific file in the current working directory by name:
$ find . -name "log.txt"
This will look in the current directory for all files with the name log.txt.
{ 4 } Search for specific file extensions:
$ find /home/linuxopsys/ -name "*.sh"
This will find all bash script files in the /home/linuxopsys/ directory.
{ 5 } Find files by name ignoring case:
$ find /home/linuxopsys -iname "log.txt"
{ 6 } Search for directories only:
$ find /home/linuxopsys/ -name "lnux" -type d
This will search for all directories with the name linux.
{ 8 } Search for a file in multiple directories
$ find /home/linuxopsys linux/ -name "linux.txt" -type f
{ 8 } Find multiple files with different extensions from all directories:
$ $ find . -type f ( -name ".py" -o -iname ".png" -o -iname ".jpg" -o -name ".php" -o -iname "*.txt" )
Explanation:
β€’ -type f - only search for files (not directories)
β€’ \( and \)` - are needed for the -type f to apply to all arguments
β€’ -o - logical OR operator
β€’ -iname - like -name, but the match is case insensitive
{ 9 } Find files containing certain text
To look for all the files containing the word linux”, use;
$ find / -type f -exec grep -il "linux" {} \;
-i - tells grep to be case insensitive
-l - tells grep to list only files that contains the specified search string.
{ 10 } Find all empty directories:
$ find /home/linuxopsys -type d -empty
{ 11 } Find all hidden files
$ find /home/linuxopsys/ -type f -name ".*"
{ 12 } Find files of a specif user:
$ find /home/linuxopsys -user linuxopsys -name "linux.txt"
{ 13 } Find files of a specif group
$ find /home/linuxopsys -group linuxopsys -name "linux.txt"
{ 14 } Find files of a readable or writable or executable by a specific user
This will find all the files readable by the user linuxopsys
$ find /home/linuxopsys -type f -readable -user linuxopsys
This will find all the files writable by the user linuxopsys
$ find /home/linuxopsys -type f -writable -user linuxopsys
This will find all the files executable by the user linuxopsys
$ find /home/linuxopsys -type f -executable -user linuxopsys
{ 15 } Negating find matches
The -not option can be used to negate find matches, here is an example of finding all the files not executable by the user linuxopsys:
$ find /home/linuxopsys -type f -not -executable -user linuxopsys
That it for today's thread! Thank you for reading!
If you enjoyed this thread and found it useful, please follow us
(@linuxopsys) for more Linux, sysadmin, and devops content!.
Also feel free to add more useful practical examples.

Loading suggestions...