find and -exec and other options
% find . -exec echo {} \;
The ';' is used by shell aos, so it is necessary to escape the character with a backslash or quotes.
% find `pwd` -type d -group staff -exec find {} -type l -print \;
The above command is used to list every symbolic link in every directory owned by a group staff under the current directory.
% find . -perm -20 -exec chmod g-w {} \;
or:
% find . -perm -20 -print | xargs chmod g-w
is used to search for all files with group-write permission under the current directory and to remove the permission.
If you accidentally created a file with a control character in it, and cannot delete it, then it can be done using find and -exec, with input from the command `ls -il` in the following way:
% find . -inum 31246 -exec rm {} ';'
In order to rename some files from {} to {}.orig, the following shell script comes in handy: (gnu find does it using a find . -group staff -exec mv {} {}.orig \; itself.)
$ find ... -print |
> while read file
> do mv "$file" "$file.orig"
> done
% find . -exec beauty {} \; -print
If you have a program called beauty that returns 0 if a file is beautiful, and non-zero otherwise, then this command prints the names of all beautiful files.
% find . -name "*.[ch]" -exec beauty {} \; -print
% find . -name \*.cc -exec grep -n "GetRaw(" {} \; -print
Finding many things with one command:
% find . \( -type d -a -exec chmod 771 {} \; \) -o \( -name "*.sh" -a -exec chmod 600 {} \; \) -o \( -name "*.txt" -a -exec chmod 644 {} \; \)
% find . -name "*.c" -print
multiple operators are ANDed by default.
% find . -type f -print | xargs ls -l
% find . -size 1234c -print
The c is used to specify the size in bytes.
% find . -size +100000c -size -320000 -print
When more than one operator is given, both must be true.
To find all directories with group write permission:
% find . -type d -perm -20 -print
The above command will match the following permissions: 777, 775, 666, 664, 660.
To find files that are set user ID root:
% find . -user root -perm -4000 -print
To find files that are set group ID staff:
% find . -group staff -perm -2000 -print
Normal find (not Gnu find) will not let this work:
% find . -type d -exec mkdir /usr/project/{} \;
Instead, you should do something like:
% find . -type d -print | sed 's@^@/usr/project/@' | xargs mkdir
% egrep '^[0-9].*SALE PRICE' `find . -type f -print`
0 Comments:
Post a Comment
<< Home