forked from esp0xdeadbeef/cheat.sheets
-
Notifications
You must be signed in to change notification settings - Fork 5
/
general_bash_tricks.cheat
67 lines (57 loc) · 2.88 KB
/
general_bash_tricks.cheat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
% General Bash Tricks
$ file: fdfind . $(pwd) -Ltf | sort -u
# string editing
echo " # --- remove scema from webpage"
site='ftp://www.example.com'; echo ${site#*//}
echo "# remove (pre|suf)fix bash"
string="hello-world";prefix="hell";suffix="ld";echo "${string:${#prefix}:${#string}-${#prefix}-${#suffix}}"
# find reminders
echo "find only files"
find . ! -type d
echo "cat files using inode in dir (<dir>) with regex (<partitional_file_name>)"
ls -lai <dir> | grep <partitional_file_name> | awk '{print $1}' | xargs -I fn find <dir> -maxdepth 0 -type d -inum fn -exec cat {} 2>/dev/null \;
# xargs reminders
echo "reuse an variable from stdout multiple times"
echo 'test1\ntest2\ntest3' | xargs -r0 /bin/bash -c 'echo "$@"; echo "$@";' ''
echo 'test1\ntest2\ntest3' | xargs -I {} echo {} | while read stuff; do echo "$stuff"; echo "$stuff";done
echo "xargs use with stdin in the middle (last 2)"
echo 'test1\ntest2\ntest3' | xargs -I {} echo '{}'
echo 'test1\ntest2\ntest3' | xargs -I somerandomstring echo 'somerandomstring'
# grep reminders
echo " # --- adding a test file, with content:"
echo "some word\n#some word 2\n\n some word 3 with space\nsupport\nanother one." | tee /tmp/file_that_definitely_did_not_exist.txt
echo " # --- hide all matches (-v 'some word')"
grep -v 'some word' /tmp/file_*.txt
echo " # --- get match count (-c 'some word')"
grep -c 'some word' /tmp/file_*.txt
echo " # --- whole word search (-w 'some word')"
grep -w 'some word' /tmp/file_*.txt
echo " # --- display the line numbers (-n 'some')"
grep -n 'some' /tmp/file_*.txt
echo " # --- match counter limit m[num] (-m1 'some')"
grep -m1 'some' /tmp/file_*.txt
echo " # --- match pattern with before[num] (-B1 '2')"
grep -B1 '2' /tmp/file_*.txt
echo " # --- match pattern with after[num] (-A1 '2')"
grep -A1 '2' /tmp/file_*.txt
echo " # --- match pattern with before and after [num] (-C1 '2')"
grep -C1 '2' /tmp/file_*.txt
echo " # --- list filenames without the content. (-l 'support')"
grep -l 'support' /tmp/file_*.txt
echo " # --- grep for output of the regex ('.*2')"
grep -x '.*2' /tmp/file_*.txt
echo ' # --- grep match starting string ("^some") ("^[string]") '
grep "^some" /tmp/file_*.txt
echo " # --- grep match ending string ('\.$')"
grep '\.$' /tmp/file_*.txt
echo " # --- use a file for patterns (-f /tmp/zfile_that_pattern.txt)"
echo 'some word 3\nsupport' > /tmp/zfile_that_pattern.txt;grep -f /tmp/zfile_that_pattern.txt /tmp/file_*.txt ;rm /tmp/zfile_that_pattern.txt
echo " # --- multiple patterns -e 'some word 3' -e 'support'"
grep -e 'some word 3' -e 'support' /tmp/file_*.txt
echo " # --- get all comments and empty lines with extended grep (-E '^(#|$)')\n # ps. could be usefull with -v"
grep -E '^(#|$)' /tmp/file_*.txt
echo " # --- cleaning up"; rm /tmp/file_that_definitely_did_not_exist.txt
# clear screen without clear
echo "\ec"
# rot13 without external packages
cat <file> | tr 'A-Za-z' 'N-ZA-Mn-za-m'