Execute multiple commands with exec and find:
--------------------------------------------------------------------
Is it possible to execute multiple commands using exec on the Linux/UNIX find command output ?
The answer is "yes". Each -exec action is to be associated with a escaped semi-colon (\;)
e.g.
I had to find files named "1251936000.log" and then need to perform two actions on it:
- Count number of lines in the file.
- Do a "ls -l" listing of the file.
And the find command I wrote with exec:
$ find . -name 1251936000.log -exec wc -l {} \; -exec ls -l {} \;
Output:
6924 ./lv1/1251936000.log
-rw-r--r-- 1 root root 977264 Sep 4 00:17 ./lv1/1251936000.log
List empty directories using find in bash:
---------------------------------------------------------------
Linux command find gives an option called "empty" using which we can list empty regular files or empty directories.
e.g.
To list all the empty directories
$ find . -type d -empty
Output:
./bdb/prac
./sim/old/data
./prac/testd
Linux find command and logical operators :
---------------------------------------------------------------------
Some of the example to show how we can use logical operators with linux find command.
To find any file whose name ends with either 'sh' or 'pl'
$ find . -type f \( -iname "*.sh" -or -iname "*.pl" \)
To find .txt files that are writeable by "others"
$ find . -type f \( -iname "*.txt" -and -perm -o=w \)
To find .txt files but exclude the ones which are writeable by "others"
$ find . -type f \( -iname "*.txt" ! -perm -o=w \)
or
some find versions support "-not" as well
$ find . -type f \( -iname "*.txt" -not -perm -o=w \)
Remember: The parentheses must be escaped with a backslash, "\(" and "\)", to prevent them from being interpreted as special shell characters.