Linux Find command


>> find archive -type f \
>> ! -name '*.gz' ! -name \*.bz2 \
>> \( -exec gzip {} \; -o -exec bas \)
-------------------------------------------------
 \ - escape character & line continuty symbol.
 2nd line is AND operation
 3rd line is OR operation, with ()

* AND has high precedence than OR
(a) A ^ B + C ^ D => (A ^ B) or (C ^ D)
(b) A ^ (B + C) ^ D =>

>> find /dir -type f \
>> \( \
>>   \( -name '*_' -mtime +7  -exec rm -f {} \; \) -o \
>>   \( -name core -atime +30 -exec rm -f {} \; \) -o \
>>   \( -name '*.bak' ! -perm 444 -exec chmod 444 {} \; \) \
>> \)
-----------------------------------------------------------------
 if...then.....else like format
 stops evaluation after first match

==== Use of -ok & -ls ====
>> finx archive/ -type f \
>>  ! -name '*.gz' ! -name '*.bz2' \
>>  -size +10000c -ls -ok gzip {} \;
Find uncompressed files (whose name doesn't end with .gz or bz2) with more than
10,000 characters, show a long listing of the file information, then ask if you
want to compress the file with gzip.

==== invoke a shell ====
>> finx archive/ -type f \
>>  ! -name '*.gz' ! -name '*.bz2' \
>>  -size +10000c \
>>  \( -exec gzip {} \; -o -exec bash \; \)
If gzip failed, invoke a "bash shell

==== Use of {} in find - GNU fine only ====
>> cd /home/directoryTree
>> find . -type d  ! -name . \
>>  -exec mkdir /backup/directoryTree/{} \;
Duplicate the subdirectory structure from /home to /backup, just empty directories

Labels: cheatsheet, linux.