unix search text string in file Finding all files containing a text string in linux
backup !!! find /opt/commvault/ -name backup.out -mtime 2 -exec grep -il "databasename" {} \; find /home/anujs/Documents -type f -exec grep -il "/tickets" {} \; find /home/oracle -type f -exec grep -il "192.168.0.17" {} \; find /u01/app/oracle/diag/rdbms/irac/irac1/trace -type f -exec grep -il "undo_tablespace" {} \; if you know the file type find . -type f -name "*.ext" -exec grep -il "hello" {} \ find /u01/app/oracle/diag/rdbms/irac/irac1/trace -type f -name "*.log" -exec grep -il "undo_tablespace" {} \; find . -type f -exec grep -il "hdp" {} \; rm -rf `ls -l | grep 'Apr 2' | tr -s ' ' | cut -d ' ' -f9` find /u01/app/oracle/diag/tnslsnr/***/listener/alert -ctime +5 -exec rm -f {} \;
via grid
15 * * * * find /u01/app/grid/diag/asm/*/*/alert -name 'log_*.xml' -mtime +1 -exec rm -f {} \; &> /dev/null 20 * * * * find /u01/app/oracle/diag/tnslsnr/*/listener/alert -name 'log_*.xml' -mtime +4 -exec rm -f {} \; &> /dev/null05 * * * * find /u01/app/*/grid/rdbms/audit -name '*.aud' -ctime +7 -exec rm -f {} \; &> /dev/null25 * * * * find /u01/app/grid/diag/crs/*/crs/trace -name 'log_*.xml' -mtime +1 -exec rm -f {} \; &> /dev/null
20 * * * * find /u01/app/oracle/diag/tnslsnr/*/listener/trace -name 'listener*.log' -mtime +4 -exec rm -f {} \; &> /dev/nullvia oracle
05 * * * * find /u01/app/oracle/admin/*/adump -name '*.aud' -ctime +7 -exec rm -f {} \; &> /dev/null
35 * * * * find /u01/app/oracle/diag/rdbms/*/*/trace \( -name '*.trc' -o -name '*.trm' \) -type f -mtime +2 -exec rm -f {} \; &> /dev/null 25 * * * * find /u01/app/oracle/diag/rdbms/*/*/ -name 'log_*.xml' -mtime +1 -exec rm -f {} \; &> /dev/null
05 * * * * /u01/app/oracle/diag/tnslsnr/*/listener/alert -name '*.xml' -ctime +7 -exec rm -f {} \; &> /dev/null
05 * * * * find /u01/app/oracle/diag/tnslsnr/*/listener/alert -name '*.xml' -ctime +7 -exec rm -f {} \; &> /dev/null
find /u01/app/oracle/admin/*/adump -name '*.aud' -ctime +7 -exec rm -f {} \; &> /dev/null du -max /anuj | sort -hr | head -30 find /anuj -type f -size +100M -exec du -ch '{}' + | sort -hr | head -50 find /u01/app/oracle/diag/rdbms/*/*/trace -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk '{ print $NF ": " $5 }' | sort -hrk 2,2
/bin/rm: Argument list too long
find $HOME -maxdepth 1 -type f -name "*.jpg" -exec rm "{}" \;ls | head -n 1000 | xargs rm
15 * * * * find /u01/app/oracle/admin/*/adump -name '*.aud' -ctime +1 -exec rm -f {} \; # Purge Trace files 30 * * * * find /u01/app/oracle/diag/rdbms/*/*/trace \( -name '*.trc' -o -name '*.trm' \) -type f -ctime +1 -exec rm -f {} \; &> /dev/null 20 * * * * find /u01/app/oracle/diag/rdbms/*/*/ -name 'log_*.xml' -ctime +1 -exec rm -f {} \; &> /dev/null
find /u01/app/oracle/diag/rdbms/*/*/trace \( -name '*.trc' -o -name '*.trm' \) -type f -mtime +7 -exec rm -f {} \; &> /dev/null find /u01/app/oracle/diag/rdbms/anuj/anuj/trace -type f -mtime +15 -exec rm -f {} \;
use below already has .gz suffix -- unchanged find /u01/app/oracle/diag/rdbms/irac/irac1/trace/ -mtime +1 -type f ! -name '*.gz' -print0 -exec gzip -9 '{}' \; find /u01/app/oracle/diag/rdbms/*/*/trace \( -name '*.trc' -o -name '*.trm' \) -mtime +1 -type f ! -name '*.gz' -print0 -exec gzip -9 '{}' \;
find . -xdev -type f ! -name "*.trc" -newermt 2024-10-10 ! -newermt 2024-10-16 -ls | egrep -v 'May' find . -xdev -type f ! -name "*.trc" -newermt 2024-10-10 ! -newermt 2024-10-16 -ls -exec gzip {} \;
find . -xdev -type f -size +100M -print | xargs ls -lh | sort -k5,5 -h -r | head find . -xdev -type f -ls|sort -nr -k 7,7
$find . -xdev -type f -size +10G -exec du -hs {} \; $find . -xdev -type f -size +1M -exec du -hs {} \;
20,000 kilobytes (20 megabytes) in the user’s home directory (~) find ~ -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk '{ print $NF ": " $5 }' | sort -hrk 2,2zgrep # Grep text patterns from files within compressed file (equivalent to grep -Z). # More information: <https://manned.org/zgrep>. # Grep a pattern in a compressed file (case-sensitive): zgrep pattern path/to/compressed/file # Grep a pattern in a compressed file (case-insensitive): zgrep -i pattern path/to/compressed/file # Output count of lines containing matched pattern in a compressed file: zgrep -c pattern path/to/compressed/file # Display the lines which don’t have the pattern present (Invert the search function): zgrep -v pattern path/to/compressed/file # Grep a compressed file for multiple patterns: zgrep -e "pattern_1" -e "pattern_2" path/to/compressed/file # Use extended regular expressions (supporting `?`, `+`, `{}`, `()` and `|`): zgrep -E regular_expression path/to/file # Print 3 lines of [C]ontext around, [B]efore, or [A]fter each match: zgrep -C|B|A 3 pattern path/to/compressed/file $
==
find /home/oracle -name 'TKT0' -print -exec zip /dumps/TKT0_`hostname`.zip {} \;
find /ux/log/alert -type f -exec grep -r "ORA-01555" {} \;
<txt>ORA-01555 caused by SQL statement below (SQL ID: 0kv0t7aj8yu67, Query Duration=16206 sec, SCN:
<txt>ORA-01555 caused by SQL statement below (SQL ID: 8ru41zauaub1n, Query Duration=14790 sec, SCN:
<txt>ORA-01555 caused by SQL statement below (SQL ID: 20fr6cgpnzgcy, Query Duration=15270 sec, SCN:
w
find . -type f -name "opr_dia0*" -print -exec zip /home/oracle/odsprdc_dia.zip {} \;
===
to delete files
rm -rf `find -maxdepth 1 -type f -exec ls -l {} + | grep '11:54' | awk '{ print $9 }'`
to delete Dir
rm -rf `find -type d -exec ls -l {} + | grep '11:54' | awk '{ print $9 }'`
==================
Find all files in the current directory:
find . -type f
Find all files with a specific extension in the current directory:
find . -name "*.txt"
Find all files modified within the last 24 hours:
find . -mtime -1
Find all files larger than 100MB:
find . -size +100M
Find all files with a specific name and execute a command on them:
find . -name "example.txt" -exec cat {} \;
Find all files with a specific permission:
find . -perm 777
Find all files that are empty:
find . -empty
Find all files that are owned by a specific user:
find / -user myusername
Find all files that are in a specific group:
find / -group mygroupname
Find all files that are older than a certain date:
find / -type f -newermt "2022-01-01"
Find all directories in the current directory:
find . -type d
Find all files with a specific name and delete them:
find . -name "temp*" -delete
Find all files with a specific name and move them to a different directory:
find . -name "*.log" -exec mv {} /var/log/ \;
Find all files that are larger than a certain size and print their size:
find . -size +1G -exec ls -lh {} \;
Find all files that are older than a certain number of days and print their names: find . -mtime +30 -exec ls {} \;
Find all files that have been accessed within the last 7 days and print their names: find . -atime -7 -exec ls {} \;
Find all files that match a specific pattern and copy them to a different directory: find . -name "*.txt" -exec cp {} /backup/ \;
Find all files that are symbolic links and print their target:
find . -type l -exec ls -l {} \;
Find all files that have been modified within the last hour and print their names: find . -cmin -60 -exec ls {} \;
Find all files that have been modified within the last week and compress them: find . -mtime -7 -exec gzip {} \;
Find all files that have been modified within the last 30 days and have a specific string in their content:
find . -mtime -30 -exec grep -l "string" {} \;
Find all files that have been modified within the last 30 days and have a specific string in their content and delete them:
find . -mtime -30 -exec grep -l "string" {} \; -delete
Find all files that are larger than a certain size and have a specific string in their content and move them to a different directory:
find . -size +1G -exec grep -l "string" {} \; -exec mv {} /new_folder/ \;
Find all files that are smaller than a certain size and have a specific string in their content and print their name and size:
find . -size -100M -exec grep -l "string" {} \; -exec ls -lh {} \;
Find all files that have been modified within the last year and have a specific string in their content and compress them with tar:
find . -mtime -365 -exec grep -l "string" {} \; -exec tar -czf {}.tar.gz {} \;
Find all files that have been accessed within the last month and have a
specific string in their content and print their name and last access time:
find . -atime -30 -exec grep -l "string" {} \; -exec stat -c "%n last accessed on %x" {} \;
Find all files that have been modified within the last week and have a specific string in their content
and mail them to a specific email address:
find . -mtime -7 -exec grep -l "string" {} \; -exec mail -s "Files with specific string" email@example.com < {} \;
Find all files that have been modified within the last 30 days and have a specific string in their content
and run a specific command on them:
find . -mtime -30 -exec grep -l "string" {} \; -exec your_command {} \;
Find all files that have been modified within the last year and have a specific string in their content
and rename them:
find . -mtime -365 -exec grep -l "string" {} \; -exec mv {} {}_newname \;
Find all files that have a specific string in their content and have been modified within the last year,
and change their permission:
find . -mtime -365 -exec grep -l "string" {} \; -exec chmod 755 {} \;
Find all files that have been modified within the last year and have a specific string in their content
and also have a specific file extension and print their name and path:
find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; - exec stat -c "%n path: %h" {} \;
Find all files that have a specific string in their content and have been modified within the last year
and also have a specific file extension and change their owner:
find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; - exec chown new_owner {} \;
Find all files that have a specific string in their content and have been modified within the last year
and also have a specific file extension and change their group:
find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; - exec chgrp new_group {} \;
Find all files that have been modified within the last year and have a specific string in their content
and also have a specific file extension and print their name and last access time:
find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; - exec stat -c "%n last accessed on %x" {} \;
Find all files that have been modified within the last year and have a specific string in their content
and also have a specific file extension and move them to a different directory:
find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; - exec mv {} /new_folder/ \;
Find all files that have been modified within the last year and have a specific string in their content
and also have a specific file extension and compress them with tar:
find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; - exec tar -czf {}.tar.gz {} \;
find . -type f -exec grep -il "ORA-XXX" {} \; | xargs tar czf 04025trc.tar.gz
Find all files that have been modified within the last year and have a specific string in their
content and also have a specific file extension and mail them to a specific email address:
find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; - exec mail -s "Files with specific string" email@example.com < {} \;
Find all files that have a specific string in their content and have been modified within the
last year and also have a specific file extension and rename them:
find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; - exec mv {} {}_newname \;
Find all files that have been modified within the last year and have a specific string in their content
and also have a specific file extension and change their permission:
find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; - exec chmod 755 {} \;
find . "*.trc" -exec ls -l {} +
for full path
find . "*.trc" -exec realpath {} \;
1 comment:
Post a Comment