find

This article lists some of the more useful ways of utilizing the find command.

These commands will work on most Linux distributions, and have been tested on recent versions of Fedora.

To find a file by its name, starting in the current directory and recursing through its children, use:

find . -name '<string>'

Search Inside Files

To search inside files, descending into subdirectories and listing ONLY the file names:

find . -print0 | xargs -0  grep '<search_phrase>' -sl

The above command will print out the names of the files that contain the <search_phrase>, which could be a regex.

Note that the above command works even with “funny” filenames that contain spaces or newlines. The “print0” and -0 option to xargs do the trick – you can see the xargs man page for an explanation of why this works!

Also, xargs is a faster option to the “exec” option of the find command, which is present in many examples on the Internet – though exec is more “portable” across Linux distributions, old and new.

To make things easier for me, I have created a function for the above command, by adding the following line in the /etc/bash.bashrc file at the bottom:

function f() { find . -print0 | xargs -0 grep "$1" -sl; }

Try it out – your search term can even contain regular expressions! Here’s an example usage:

f SOAP

will search for the word “SOAP” in all files starting recursively from the current directory.

Finding Files Based on Permissions

To find files that match a certain permission, use the perm option:

find . -perm -644

will match all files that have, at a minimum, the rw permission set for user AND r permission for group AND r permission set for others.

find . -perm 644

will match all files that exactly have the rw permission set for user AND r permission for group AND r permission set for others.

find . -perm /644

will match all files that have the either the r or w permission set for user OR r permission for group OR r permission set for others.

To find files writable by the current user, use:

find . -writable

Other tricks

On similar lines to the find+grep combination above, you can use xargs to perform other tasks, such file deletion or text replacement. For example, to delete the files found by find, use:

find . -name '<string>' -print0 | xargs -0 rm

To replace contents of multiple files at once, you can use find along with sed. The following command will search for files containing the number 12121 and replace it with 99999:

find . -print0 | xargs -0 grep '12121' -sl | xargs sed -i 's/12121/99999/g'

The sed command above searches for the 1st string and replaces it with 2nd string. The g option is for global replacement, or else only the 1st occurence will get replaced. The -i option will make changes in the same file, in place.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值