shell¶
variable¶
$variable_name
- no space around
=
- enclose variable with space with
"
echo "$variable"
make as variable output¶
var=$(git status)
swallow output¶
string¶
concat¶
allmsg="${allmsg}\n${msg1}\n${msg2}\n${msg3}\n"
slice¶
for str[1]
for str[0:4]
split¶
for str.split(',')[0]
replace¶
sed¶
echo xxx | sed "s/<find>/<replace>/<position>"
sed -i "s/<find>/<replace>/<position>" input.txt
for <position>
- g
= all
- 1
= 1
- 2g
= 2:
https://stackoverflow.com/a/44898816/15493213
use \bword\b
for whole word
tr¶
tr 'find' 'replace'
paste¶
paste -sd'abc'
replace every \n
with a
b
c
in a round robin manner
array¶
arr=((1 2 3))
get index¶
echo ${arr[2]}
replace¶
echo ${arr[@]/<find>/<replace>}
awk¶
awk '{print $1, ":", ($2>=50 && $3>=50 && $4>=50) ? "Pass" : "Fail"}'
awk '{print "2 " $2 " 3 " $3}' file.txt
awk '{avg=($2+$3+$4)/3; print $0, ":", (avg>=80)?"A":(avg>=60)?"B":(avg>=50)?"C":"FAIL"}'
- substring index
awk 'BEGIN{print index("abcdes","bc")}'
→ 2echo "My name is Deepak" | awk '{print index($0,"Deepak")}'
→ 12
find¶
Find all files under the current directory recursively
List all the files named haha
List all the files > 50MB
List all the files < 50MB
Delete all the files matching the pattern *pat*
scp¶
remote to local
local to remote
https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/
processes¶
ps aux
top
htop
- proc
free -h
for total memory usage
output to file¶
- write to file
echo haha > file.txt
- append to file
echo haha >> file.txt
curl¶
curl + python to extract json
e.g.
curl -s "http://ws.audioscrobbler.com/2.0/?method=user.getrec
enttracks&user=<user>&api_key=<api key>&format=json" | python3 -c "import json,sys;result=json.load(sys.stdin);print(result['recenttracks']['track'][0]['name'])"
disk¶
- usage and available space of all partitions
df -h
- size of all top level non-hidden directories or files
du -sh *
du -sh * | sort -h
- sorted
du -sh * | sort -rh
- reversly sorted
du -sh -I fileprefix* *
- exclude
fileprefix*
- exclude
- size of all top level directories or files
du -ahd1 . | sort -h
df -i
df -ah
sort¶
sort -n
for numerial sortsort -r
for reversesort -k 2
to sort with 2nd column
alias¶
- double quote -> evaluate at creation time
- single quote -> evaluate at runtime
tar¶
- compress
- compress
b08901001_pa1
tob08901001_pa1.tgz
tar -zcvf b08901001_pa1.tgz b08901001_pa1
- compress
- extract
- extract
sample.tgz
tar zxvf sample.tgz
- extract
sample.tgz
into an existing directorysample
tar zxvf sample.tgz -C sample
- extract
zip¶
- zip
haha
tohaha.zip
zip -r haha.zip haha
find files¶
history¶
history
to show history!5
to execute 5th commands in historyctrl+r
to search history command
commands¶
memory¶
free -h
exec time¶
time command
meta¶
- autocomplete from history
ctrl+R
- cd to previous directory
cd -
softlink & hardlink¶
- softlink
ln -s ogfile softlink
- create a pointer to ogfile
- deleting ogfile will make softlink unsuable
- like Windows shortcut
- hardlink
ln ogfile hardlink
- create a pointer to the data ogfile points to
- deleting ogfile won't affect hardlink
python¶
python -c <script>
- pass things in with input()
echo 2 | python -c "print(int(input())+2)"
→ 4
calculate¶
bc
bc -l
use some libraryecho 2+2 | bc
→ 4