We developers use the terminal a lot and there are a lot of tricks that can ease our work. So itβs worth looking beyond the essentials of getting stuff done in the shell! The quickest way to find what you need is probably to use tldr
. But the frequently used hacks are best learned once to know about all the good stuff and work effectively. I recently found the great The Linux Command Handbook from Freecodecamp and went over it to extract the useful bites that I didnβt know.
So, here they are:
πFiles
πcp
-v
: verbose
πmkdir
-p
: create multiple nested folders
πrmdir
delete empty dir. Saver than (rm -rf
). But best to use trash
πls
-h
: human readable file sizes (M,Kβ¦)
πln
the default is a hard link.
Hard link: points to content linked at time of linkage (does copy)
Soft link: pointer to the original file. Breaks when original is moved.
-s original symlink
: soft link
πgzip
compress SINGLE file. By default the original file is removed.
-k
: keep original file
-d
: unzip or gunzip
-v
: verbose info on saved storage percentage
πtar
gather multiple files (uncompressed)
-cf archive.tar FILES...
: create archive
-czf archive.tar.gz
: tar and zip
-xf archive.tar.gz -C directory
: extract (potentially zipped) archive to specified directory.
-tf archive.tar
: show files without extracting
πxargs
take output and turn into arguments for other command.
This is especially useful for commands that donβt accept stdin
(e.g. touch
,rm
,ls
)
Example:
cat listFiles.txt | xargs rm
For find
use -print0 | xargs -0
to properly split the matching files
-p
: print conformation prompt
sh -c "command1 && command2 | command3"
: run multiple commands
-I _ command _ optional_extra_arguments
: put output into _
placeholder
πView
πcat
concatenate / print intput
-n
: see line number
cat file1 file2 >> file3
πtail
open the file at the end
-f
: (follow) watch for changes
-n 10
: print last 10 lines
πless
view file inside interactive UI
space
: navigate down one page
b
: navigate up one page
navigation similar to vim
v
: open in default system editor (vim)
F
: follow mode (see changes live)
:n
: open next file (for multiple input files)
:p
: open previous file
πnano
ctrl+s
: save
ctrl+x
: quit
for all the rest, use vim
π
πExtract
πtr
translate / delete character in a file:
tr find_character replace_character < filename
-d
: delete occurences of character
πcut
cut given character from file
-c 5-10
: cut character nbr 5 to 10 from each line
-d "," -f 2,6
: extract the second and sixth ,
delimited field from each line
πgrep
extract information (supports regex)
-n
: show line numbers
-C 2
: show 2 lines before and after match
-i
: ignore case
-v
: invert search (exclude all matches)
Example:
grep -n document.getElementById index.md
πfind
find files relative to directory
. -name '*.js'
: find js files relative to current directory
-type
: type can be f
(file), l
(link),d
(dir)
-mtime -1
: match all files modified within 1 day
find folder1 folder2
: can provide mutiple search directories
-or
: or condition
-delete
: deletes matches
-exec
: execute command on each result
πwc
-l
: just count lines
-w
: just count words
πuniq
output unique lines
-c
: count occurences
often used with sort (sort -u
also filters unique elements):
sort <input> | uniq
πdiff
compare two files
-y
: compare next to each other, line by line
-u
: show differences like in Git
-r
: (recursively)
πbasename
get last segment of provided path:
basename /Users/adria/test.txt
returns test.txt
πdirname
provides dirname of path (conjugate of basename)
πProcess
πkill
kill signals (alternative after /
) from softest:
-TERM / -15
: terminate
-HUP / -1
: (hang up) process before terminating
-KILL / -9
: abruptly terminate
πkillall
killall NAME
kill all processes matching NAME
πjobs
find all running jobs (including suspended ones)
πfg
ctrl+z
: suspend process
If only 1 process is suspended, its enough to run fg
, otherwise do:
fg 2
: bring job 2 to foreground (get id from jobs
)
πbg
run job in background (like appended &
to command). API is same as fg
πps
ax
: a
also shows processes of other users. x
shows processes not linked to terminal
axww
: to prevent cutting of command path
πtop
display real-time info on resource usage (CPU, memoryβ¦)
-o mem
: sort by memory usage
πnohup
let long-lived process run even after disconnect / logout
nohup <command>
πenv
run command with variable without setting it in the outer environment (current shell)
env USER=adria node app.js
-i
: clear variables from outer environment
-u
: clear specified variable from command environment
πPrivileges
πsu / sudo
su - <user>
: login to other userβs home directory
sudo -i
: start shell as root
sudo -u <user>
: run command as other user
πchown
change file ownership
chown -R <owner> <dir>
: recursively change permissions of all subdirectories
chown <owner>:<group>
: change group as weell
chgrp <group> <file>
: change group
πchmod
change read, write, execute permissions
-r
: apply recursively
Symbolic usage
a
stands for allu
stands for userg
stands for groupo
stands for others
chmod a+rw filename #everyone can now read and write
chmod og-r filename #other and group can't read any more
```
Numerical usage
-
1
if has execution permission -
2
if has write permission -
4
if has read permission This gives us 4 combinations: -
0
no permissions -
1
can execute -
2
can write -
3
can write, execute -
4
can read -
5
can read, execute -
6
can read, write -
7
can read, write and execute
πSingle vs double quotes
""
: resolved at definition time
''
: resolved at invocation time
An example makes it clearer:
alias lsstatic="ls $PWD"
alias lscurrent='ls $PWD'
lscurrent
depends on the current directory, whereas static doesnβt.
Sources: Freecodecamp: The Linux Command Handbook, Unix Tutorial