Shell Cheatsheet
Shortcut
ctrl+c
- halts current commandctrl+z
- stops current commandfg
- resume stopped command in foregroundbg
- resume stopped command in backgroundctrl+a
- begin of linectrl+e
- end of linectrl+l
- clear screenctrl+w
- erases one word in current linectrl+u
- erases whole linectrl+r
- reverse lookup of previous commands!!
- repeat last commandctrl+d
- log out of current sessionecho {a,b}.jpg
-expands toecho a.jpg b.jpg
echo {1..3}.jpg
-expands toecho 1.jpg 2.jpg 3.jpg
Execution
nohup jupyter lab > /dev/null 2>&1 &
- redirect every output to nohup.txt
Compression
tar zcvf file.tar.gz dir
- tar everything in dir and compress with gziptar zxvf file.tar.gz dir
- extract everything in file.tar.gz to dir with gziptar Jcvf file.tar.xz dir
- tar everything in dir and compress with xztar Jxvf file.tar.xz dir
- extract everything in file.tar.xz to dir with xzgzip file
- compress file and rename to file.gzgzip -d file.gz
- decompress file.gztar cf file.tar files
- tar files into file.tartar xf file.tar
- untar into current directorytar tf file.tar
- show contents of archive- tar flags:
c
- create archivet
- table of contentsx
- extractf
- specifies filenamez
- use zip/gzipj
- bzip2 compressionk
- do not overwriteT
- files from filew
- ask for confirmationv
- verbose
Searching
grep pattern files
- search for pattern in filesgrep -r pattern dir
- search recursively for pattern in dircommand | grep pattern
- search for for pattern in in the output of commandfind . -name "*.js"
- find all file whose extension is js in current directory
loop
- create 10 files
for ((i=0; i<10; i++)); do touch test_$i.txt done
xargs
find . -name "*.pyc"|xargs rm
- remove (rm) all file matching the pattern (filename *.pyc)find . -name "pattern" | xargs rm -rf
- remove all file/dir matching the patternfind . -name "*.c" -print0 | xargs -0 rm -rf
- correctly deal with filenames containing spacefind . -name '*.sh' | xargs grep "SBATCH"
- find all occurence of “SBATCH” in files that match the patternecho a b c | xargs cmd
- executecmd 1; cmd 2; cmd 3
echo a b c | xargs mkdir
- create three dir a b c- Basics (c.f. link)
xargs
- when you type xargs without any argument, it will prompt you to enter the input through stdinecho a b c | xargs -d\n
- specify delimiter using -d optionecho a b c | xargs -n 2
- display only 2 items per line in the xargs outputecho a b c | xargs -p -n 2
- prompt before executionxargs --show-limits
- display system limits on xargs
Process Management
ps
- display currently active processesps aux
- ps with a lot of detailps uxf
- ps with tree view (only your process)kill pid
- kill process with pid ‘pid’killall proc
- kill all processes named procbg
- lists stopped/background jobs, resume stopped job in the backgroundfg
- bring most recent job to foregroundfg n
- brings job n to foreground
File
ls
- directory listingls -al
- formatted listing with hidden filescd dir
- change directory to dircd
- change to homepwd
- show current directoryreadlink -f params.json
- show path of the filemkdir dir
- create direcotry dirrm file
- delete filerm -rf dir
- remove directory dirln -s file link
- create symbolic link ‘link’ to filetouch file
- create or update filesed -i 's/word1/word2/g' input.file
- replace word1 with word2 in input filecat > file
- place standard input into fileless file
- output the contents of the filehead file
- output first 10 lines of filetail file
- output last 10 lines of filetail -f file
- output contents of file as it growschmod octal file
- change permission of file- 4 - read (r)
- 2 - write (w)
- 1 - execute (x)
- order: owner/group/world
chmod 777 files
- rwx for everyonechmod 755 files
- rw for owner, rx for group/world
ssh
ssh user@host
- connet to host as userssh -p port user@host
- connect using port pssh -D port user@host
- connect and use bind port
Network
ping host
- ping host ‘host’whois domain
- get whois for domaindig domain
- get DNS for domaindig -x host
- reverse lookup hostwget file
- download filewget -c file
- continue stopped downloadwget -r url
- recursively download files from urlwget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=FILEID" -O FILENAME && rm -rf /tmp/cookies.txt
download gdrive files
System Info
date
- show current date/timecal
- show this month’s calendaruptime
- show uptimew
- display who is onlinewhoami
- who are you logged in asuname -a
- show kernel configcat /proc/cpuinfo
- cpu infotop
- display Linux processes and cpu/mem usageman command
- show manual for commanddf
- show disk usagedu
- show directory space usagedu -h -d 1
- show direct subdirectory space usagewhereis app
- show possible locations of appwhich app
- show which app will be run by default