-
Notifications
You must be signed in to change notification settings - Fork 14
/
general_bash_tricks.cheat
141 lines (112 loc) · 6.08 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
% General Bash Tricks
$ file: fdfind . "$(pwd)" -Ltf | sort -u
$ json-file-or-url: fdfind . "$(pwd)" -Ltf | sort -u | grep \.json
$ mimeapps-list: locate mimeapps.list | head -n1 | xargs -I {} grep -v '\[' {} | grep --color=no . | awk -F '=' '{print $2}' | sed 's/;//g' | 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'
# remove duplicates from or in file (creates a /tmp/tmp.txt to test on)
echo -e 'dup1\ndup2\ndup2 \ndup1' | tee /tmp/tmp.txt
echo "sort, remove dupes and reorder"
sort -u <file>
echo "remove dupes"
awk '!a[$0]++' <file>
echo "remove dupes"
perl -ne 'print if ! $a{$_}++' <file>
echo "remove dupes even if there are %s behind the string"
perl -lne 's/\s*$//; print if ! $a{$_}++' <file>
echo "It deletes duplicate, consecutive lines from a file (emulates \"uniq\")."
sed '$!N; /^\(.*\)\n\1$/!P; D' <file>
echo "remove dupes inline (replaces inside the file)"
perl -i -ne 'print if ! $a{$_}++' /tmp/tmp.txt
echo -e 'dup\ndup\ndup \ndup' > /tmp/tmp.txt # leaving a file after to test on.
# redirect stderr to stdout
thiscommanddoesnotexist 2>&1 | grep --color command
# open a file within cli
xdg-open <file>
# change xdg-open default program for a file
file_xdg=$(file --mime-type -b <file>)
xdg-mime query default $file_xdg
xdg-mime default <mimeapps-list> $file_xdg
xdg-mime query default $file_xdg
# add a json inline with gron
(gron <file>; echo 'json.<inject_path> = "<inject_value>";') | gron -u
# skip the first lines-to-skip lines from a file and get the rest (n=amount of lines)
tail -n +$((<n-lines-to-skip> + 1)) <file>
# cat a file and stop at the lines from the bottom (n=amount of lines)
head -n -<n-lines-from-bottom-to-stop-printing> <file>
# sort on keys, sort various columns in a csv file by numeric and dictionary order, columns 5 and after as dictionary order
echo -e '2,3,a,9,C\n2,2,b,20,F\n2,2,c,19,Gb,hj\n2,2,c,19,Gb,hi\n2,2,c,19,Ga\n2,2,b,22,Ga\n1,10,b,22,Ga' > tmp_list_that_does_not_exists.csv
echo -e "original: \n$(cat tmp_list_that_does_not_exists.csv)";
echo "sorted: "; sort --field-separator=, --key=1,1n -k2,2n -k3,3d -k4,4n -k5d tmp_list_that_does_not_exists.csv
rm tmp_list_that_does_not_exists.csv
# echo, cat, and remove a file with inode (example)
echo 'Hello general_bash_tricks' > '/tmp/sometempfilethatdoesnotexist`pwd`'; inode=$(ls -li /tmp | grep 'pwd' | awk '{print $1}')
echo $inode | xargs -I inodenumber find /tmp/ -maxdepth 1 -inum inodenumber -exec echo {} \; -exec cat {} \; -exec echo "" \; -exec rm {} \;
# *carefully* remove a file with inode and iname
find . -maxdepth 1 -inum $(ls -li $path | grep '<partitional_file_name>' | awk '{print $1}') -exec -exec rm {} \; # by inum
find . -maxdepth 1 -iname '*<partitional_file_name>*' -exec echo {} \; # by iname
# execute commands with sed
echo 'ls -la\nps aux' | sed 's/.*/&/e'
# replace by hex chars with sed (09=\t, 0a=\n, check `man ascii`)
echo 'test\ttest' | sed 's/\x09/\x0a/g'
# replace an port (all integers) inside old scans
sed 's/:[[:digit:]]\+\//:<new_port>\//g' <file> # place an -i for in-file operations
# replace newlines with sed
echo -n 'test\ntest' | sed ':a;N;$!ba;s/\n/\t/g'
# print all files in the current workdirectory that are printable
find . -type f -exec grep -Iq . {} \; -print | xargs cat
# replace spaces in filename with `_` lowercase
for file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done