Find, Grep, SED and AWK commands
- Recursively '-r' search for the text string '- Server Name', backslash is an escape character for '-', exclude directories proc and sys
grep -r --exclude-dir={proc,sys} '\- Server Name'
- Grep recursive (-r) in php files and find 'fping' -- './' means start in the current directory.
grep -r --include='*.php' 'fping' ./
- Find all 'png' files, recursive.
find . -type f -name '*.png'
- Shows the packages installed without date or time etc. Remove everything before the last ':' in a line, also remove any extra spaces.
cat /var/log/yum.log | grep -E 'Installed|Updated' | sed 's/.*://g;s/ //g'
- Find httpd processes, remove the grep process that is searching for 'httpd'
ps -ef | grep -v " $$ .*grep " | grep httpd
- Fast way to find all files that contain a matching string.
find . -type f -print0 | xargs -0 grep -Fil "librenms"
- Find files that match file extension '*.sh' and delete them.
find . -type f -name '*.sh' -delete
- Find files that contain a text string inside the file and only output file name.
find . -type f -print0 | xargs -0 grep -l "port_bits"
- Find files (-f), match (grep) filenames that contain 'switch', pipe to xargs to remove (rm) the matching files.
find ./ -type f | grep switch | xargs rm
- Same as above, but only echo 'rm' files, does not remove the files, this is a good test before you permanently remove files
find ./ -type f | grep switch | xargs echo rm
- Find duplicate files using checksum values.
find ./ -type f -print0 | xargs -0 -n1 md5sum | sort -k 1,32 | uniq -w 32 -d --all-repeated=separate | sed -e 's/^[0-9a-f]*\ *//;'
- Remove file by inode number rather than file name, helpful if the filename contains special characters like {\|})( etc. Use 'ls -li' to find inode number.
find . -inum 4294 -delete
- Return strings that contain '.x86_64' and('\|') 'noarch', do NOT return whole line. grep -o means return strings that contain the first string (x86_64) and('\|') the second string (noarch).
yum history info brasero | grep -o '[^ ]*.x86_64[^ ]*\|[^ ]*noarch[^ ]*'
- cat --show text output of a file, -n show line numbers, | to grep and find lines that match 'discovery.php', | to less for easy navigation. cat definition: concatenate, or string things together in a chain or series.
cat -n /var/log/cron | grep discovery.php | less
- list directory, -l long list format, -a all files, -h human readable file size. | awk (awk is a text parser/filter), 'print' print the output of column '$3' then "\t" tab, then column '$9'
ls -lah | awk '{print $3 " " $9}' > temp2
##ls -lah output:
drwxr-xr-x 10 apache apache 153 Sep 28 20:23 docs
##output piping ls -lah through awk '{print $3 " " $9}'
apache site