Search/Navigation

Multiple directories

mkdr -p ./name/{dir1,dir2}
mkdir dir1 dir2
rm -rf dir1/ dir2/

Lists directories with size : du . -h

Search refining: Lists files which are:

  • writable

  • executable

  • not readable

  • owned by user 'bandit7'

  • owned by group 'bandit6'

  • of size 33mb

find . -type f -writable -executable ! -readable -user bandit7 -group bandit6 -size 33c 2>/dev/null

Output line count | -n: number, -r : reverse

find . -name *root.txt -type f -exec wc -l {} \;  | sort -nr

#Search for a keyword in all files in specified directory
find /home -name .bashrc -exec grep keyword_to_search {} \;

Print all matching lines (without the filename or the file path) in all files under the current directory that start with "access.log" that contain the string "500"

find . -name "access.log*" | xargs grep -h 500

Find no: of lines in access.log with the word "GET" | -r :recursive | -i :Ignore test cases

grep -ri "GET" access.log | wc -l

Prints 1-100 : echo {1..100}

Delete all files in directory : find . -delete

List directories recursively without directory name

find . -type f -printf "%f\n"

Sort | List unique entries | Grep out | -r:recursive; -l:match

cat data.txt | sort | uniq -u | grep -v text | grep -rl word

Print the difference: diff passwords.new passwords.old

Remove line wrapping | Cleaner output: ./any | less -S

cut command

  • Can only accept a single character as a field delimiter

  • -f for the field number; -d for the field delimiter

  • uniq -c : Prefix output with no: of occurences

cut -d ":" -f 1 /etc/passwd 
root 
daemon 
bin
----------------------------------------------
cat access.log | cut -d " " -f 1 | sort -u 
201.21.152.44 
208.115.113.91 
208.54.80.244 
208.68.234.99 
70.194.129.34 
72.133.47.242 
88.112.192.2 
98.238.13.253 
99.127.177.95 
-------------------------------------------
cat access.log | cut -d " " -f 1 | sort | uniq -c | sort -urn 
   1038 208.68.234.99 
     59 208.115.113.91 
     22 208.54.80.244 
     21 99.127.177.95 
      8 70.194.129.34 
      1 201.21.152.44 

awk command

  • -F : Field separator; print :Outputs the result text.

echo "hello::there::friend" | awk -F "::" '{print $1, $3}' 
hello friend 

tail command

  • -f (follow) continuously updates the output as the target file grows.

  • -n X, outputs the last “X” number of lines, instead of the default value of 10.

watch command

  • run a designated command at regular intervals. Default: runs every two seconds

  • -n X option to have it run every “X” number of seconds.

  • -w - list logged-in users

watch -n 5 w 

Last updated