Show Menu
Cheatography

Shell utilities & sytem management Cheat Sheet by

Various UNIX shell commands for programming, env and system management. Those commends are helpful, to me, but not often used. I had made this sheet to avoid periodically searching another time for the same thing.

ssh

sshfs [USER@­HOS­T]:­[re­mot­e_d­ire­ctory] mountpoint [options]
mount remote drive in
mountpoint
ssh -o Server­Ali­veI­nte­rval=X -o Server­Ali­veC­oun­tMax=Y [HOST]
Maintaing ssh conection for
 X
seconds and
Y
tries
ssh-co­py-id -i ~/.ssh­/id_rsa [USER@­HOST]
add autent­ication via ssh, not
.pub
file
autossh -N -f -L localh­ost­:[P­ORT­U]:­loc­alh­ost­:[P­ORTR] [USER@­HOST]
map local port
PORTU
to
PORTR
on USER@HOST with autore­connect

operations on files & drives

cp -r $(<­fil­e_l­ist­ed.txt) outdir
copies files & direct­ories listed in
file_l­ist­ed.txt
to
outdir
xargs -a file_l­ist­ed.txt cp -r -t outdir
same as above
tar -(v)czf file.t­ar.gz /path/­to_­compres
create
-cf
archive
-xf
for extract
unzip file.zip -d file
unzip
file.zip
to file directory (must exists before)
mkdir -p dir
make parent direct­ories as needed, no error
rm -r !(*.ext)
delete everything except files with extension
.ext
du -hs dir
check size of dir use
-h
for GB or
-m
for MB
basename path/b­ase­nam­e.ext .ext
get
basename
from
path
without
ext
grep -i -r -E 'patte­rn1­|pa­tte­rn2­|pa­ttern3'
multiple patterns with
grep
-i
case insens­itive
-r
recursive
find PATH -(i)name "­pat­ter­n" -type d/f
list
f
files or
d
direct­ories with matching
pattern
in specified
PATH
 

Statements

while read line; do [Command] done < input.file 
Loop over file content
for file in ./*.ex­ten­tion; do [Command] done
loop over files in directory
if test -f file.txt; then [Command] fi
check if file exists
-f
,
-d
for directory
if [-f file.txt]; then [Command] fi
same different syntax
if [ -z "­$S1­" ]; then fi
checks if arg
$1
is empty
function functi­on_name { <co­mma­nds> }
function declar­ation,
$1
$2
- argument names
"­$?"
return exit status from last command
$(expr)
evaluate
expr
and store/­print it's result

system

cat /proc/­cpuinfo #or lscpu
cpu inform­ations
uname -a
OS inform­ations
ss -tpln
show forwarded ports
lsblk -S
list block devices
-S
for minimal view
dd if=/pa­­th­/­t­o/­­ima­­ge.iso of=/dr­­iv­e­/­lo­­cation bs=4M && sync
create bootable drive from
image.iso
sudo mount [drive] [mount­­point]
mounts
drive
at
mount­­point
kill -9 $(pgrep -f [pattern])
terminate all processes matching
pattern
printenv
print all env variables

git

git rm -r --cached <pa­th/­fil­e.e­xt>
remove untracked files
git clean -d -(n/f)
remove undone changes add:
-n
to show files to remove or
-f
to remove them
git fetch --all [branc­h_name]
copies all changes from last pull/clone on current branch or given
branch name
git log --not --remotes
all commits on all branches that aren't pushed yet
git checkout -b <ne­w_b­ran­ch>
change & create new branch
git switch -c <ne­w_b­ran­ch>
same as above
                   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Bash Script Colors Cheat Sheet
          Git Commands Cheat Sheet
          Linux Every Day Commands Cheat Sheet