You are on page 1of 24

This

explains some of the most useful Unix


zine

command line tools in 1 page each

t.to sis n

asHieiseEoaseFIsqf
ns

Julia

Even

ifyou've used the command before I might have

a new trick or two for To


Table of contents

BSDIGNU 4 bash tricks IO 11 misc commands 17

grep

5 disk usage 12 headftail 18

find G tar 13 less 19

Xargs

7 ps 14 kill 20

AWK 8 top 15 cat 21

Sed 9 sortfunig 16 Isof 22

If

Iggy 4

FE CTUhyphae

For almost all these tools there are at least

2 versions

The BSD version on BSDs Mac OS

The GNU version on Linux

All of the examples in this zine were tested on Linux

Some things I like sed i are different on Mac

Be careful when writing cross platform scripts

You can install the GNU versions on Mac with

brew install coreutils


g rep 5

grep

lets you search use if youwant


like to rafffrhseiff.Y.es

files for text fingeregeeps archf

work otherwise you


aka need to use lt
a directory

grepbananas too.tt

invertmatch find onlyprintthe

theurfurairfesgormeepcootmmmasnd

linearguments
Hairlines
match
that don't Thea'T.hn 9nPoatrtthoet

whole line

searchbinaries

case insensitive onlyshowthe

Showcontextforyour
search
f f it eafmasgfedthefites QaTireEtifisnaIeIfiatsatea

ofignoringit.fr

713 grep A 3 foo


don'ttreat the match grep alternatives
I I
string Ares

willshow3lines of
contextafter a match
hyp
aka eg grep F o
f Emfack Efag ErmmTipgrep
betterforsearchingcode.o

find 6
find searches a name I name

directory for files


caseinsensitive type TYPE

thefilename eg f regularfile 1 symlink


tin

t name.tt d directors moret

f 1
foirectaorcq whichfiles acutifhntondeo

files pthNUM
pathfipath f only

searchthe fullpath descend NUMlevels

herearemyfavouri
findarguments path home1 1 go whensearchinga directory

size O print 0 locate

findemptyfiles printnullseparatedfilenames Thelocate command

Usefulto findfilesyou Usewithxargs.org searches a database of

createdbyaccident tem

om
ebadsaII
foeTete

can getoutofdate
action run COMMAND on action delete allfilesfound Sudo updatedb

every filefound updatesthedatabase


xargs

Xargs

takes whitespace this is usefulwhen you how to replace foo

separated strings from want to run the same with bar in all txt

stdinandconvertsthem command on a list of

into commandlinearguments files


files

echo lhome Hmp f 0delete Cxargs rm f find name txt I

I xargs IS
will run
to combinelxargscat
Xargs sed i sHoo1barIg
search leargs grep

Is 1 home Hmp replace Kargs sed

how

to tint every if there are spaces in moreusefulxargsoptions

Python file in yourGitrepo Yourfilenames mydaytxt

xargs willthink it's 2 files makes xargs run

it is filesIgrep post mis and das.tt meant

xargspep8
f fix it likethis f capitalto
forseepaacrhatenpprof.es

is the max number

find printo I ofparallelprocesses

args 0
COMMAND MaxPro's

xargs will start


AWK 8

awk is a
tiny basic awk program extract a column

programming language structure of text with awk

for manipulating

columns of data BEGINE 3 awk F print 53

f condition action
CONDITION action column
ro
separator
p g
fingerprintthe
f
to do 2 things

EEOftonlyknowhowT END f 9 column

4 with awk but it's doactionon


linesmatching O
jfjfttisdoiswifffoawofwt.at

A stilluseful.tt
CONDITION

awk programexampie.TK exampe


sum the numbers in print line over

SO
MANY unix commands every

print
columns of the 3rd column 80 characters

text 1ps Iso EEE

so

the
being able to get
column you want
f i est
l END
1
f
33 i
prints i I EIIIEIIII.IE
tcondition
there's an implicit
I

with awk is GREAT attheendprint

thesumo print as the action


Sed a
incantationsI

Sed
is most often change a file in place some more sed

used for

replacing with amp


text in a file

sedsq.atdog gfi H q
e nngggseeqiiss print mine

can be a Yn confuses me IT suppressesoutputso

everytime onlywhatyouprintwith.pl

rexpression getsprinted

Sed Sd Sed stcatltdog.lt Sed G


lI

deletesthline use
canbeanycharacter
as regexdelimeter
a lgoodforlongerrorli
doublespace a file

sed fit wayeasiferthansed.TTadog


y

deletelinesmatchinghat dog after lines


append

containing
sed n5 sed nscatTgp
JP

i7panda

print lines 5 30 Onlyprintchangedlines insert panda on line 17


bash tricks

to

Ctrl r magical braces expands to the last

convert file jpgprig command run

searchYourhistory sudo

expands to

convert filejpg file png commands that start

I use this constantly with a space don't go

to rerun commands 1 5 expands to 12345 in yourhistory good if

for i in CI too there's a password

more keyboardshortcuts

loops 1

for i in gives theoutputof a Ctrl a beginning of line

Png command
end of line

doconuerts

done
i pg
touch file date I ftrite
Ctrl11 clear the screen

ga
Es create a file named lots more emacs

Tfe fPu if file 2018 05 25 shortcuts too 0

bash

more tricks 11

Cd Ctrl 12
bg

changes to the suspends SIG 15TP starts suspended program

ni9am
were

directoryyou

last in

fashellcheckT

pushd popd let you keep

bringsbackgroundedsuspended shellscriptlinter helps


a stack

program to theforeground spotcommonmistakes

C fc type

process substitution
fix command
tellsyou if something is

i.ij iii iii i


tarea.ie esm utaeiItiies eenrtahneihasaI m no

mi e

eg the edited EthnTime


tehresniornon posted

diff us us a

Itdifferentypest

disk usage 12

tells
du Jf
tells you howmuchfree spaceeach
partition has 4T for human readablesizes

you howmuch disk
space files 1directoriestakeup Filesystem Size Used Avail Use Mounted on


EB summary total
f
size of all files in
roevsoas Iso o 2 so sooo i

a directory
Ude 483M 4 Ok 483M 1 Idev


D humanreadable
sizes
tmpts 99M 1.4M 97M 2 lean

dev sda4 167615709.96953

of i ncdu iostat
see what's using diskspace statistics about

instead of diskfree
navigatewith arrow keys get
disk reads1

reporthow manyinodes writes

are used free on each

partition
f h.sa.BE
3.2 GiB E
music
31pictures Iostat 5
intervaitoreportat


5 7 MiB E 31 text Device KB reaorskBerrtnis

OIj ninseuqtIftygyga
raihneonfign.gs

2.0 MiBE J file pdf Sda 2190.21 652.87



Sdb 6.00 0.00

tar 13

The tar file format Usuallywhen you use X is for extract

combines
manyfiles the tar command you

into one file run some incantation intothecurrentdirectory

uTiIEttirc.ttffounpackatarsz.use thaw

star filesaren't Itar xzf file tar gzI c is for ereate

7aPieyssedwbgtihpemsneek.es

w at
Er makes a new tarsier

tar gzor.tgz.lk

t is for list tar can compress 1 putting it together

decompress

lists the contents of listcontents of a tar.bz

a tar archive 2 gzip format Cgz tar tivfuerfiolsee.tar.bz2

pzforma c.bzz

t.fi j.bz create a tar gz

ey

J xz format l iz
tar czf file targz dirt

whichtar file to create


more seethemanpage filesto91
go

or unpack inte

PS

14

ps w wchan
f

ps

showswhichprocesses 1 is forwide ps command


awwww youcan choosewhichcolumns
toshowwith ps Cps eo

are running willshow all the


r achi

I usually run ps like this HEYL ggwemnnaimsewghanhewh.ch

ps aux _a kernelfunction if theprocess

x is sleeping
umeansinclude tongewthair is for environment ps auxe

usernamecolumn process willshowthe environmentVars tryit

lps.ee too ps eo userpidwchan cmd

process state a f pshas 3 differentsets

Here's is for forest of command line arguments a9i

whattheletters in psauxf
ps's STATEcolumn means will showyou an ASCII art 1 UNIX l 1 dash

If I
Process tree

Biorinnaisngeep

2 zombie
f Pstree can display
3.3
youcan
4
write
Yes
monstrosities like

a processtree too f
l multithreaded ps f

intheforeground
F fullformat

forestCBSD unix

top


15

top load average memory



liveupdating summary of
a 3 numbers that roughly 4numbers

the topusers of your
reflect demand for your

system's
resources go.ns.taheosystem.mn.in the spiteIIhiidng
onetopteInaHpsreuYe


total is notfree used

meytop if itis higherthanthe
1 memory
total free usedtgched

ofCPUsyouhave that's
let's

explainsomenumbers intop oftenbad filesystem


CPU RES htop


what7T
35oro
ooo thiscolumn is the resident
setsize aka howmuch
a prettier moreinteractive
version oftop

RAMyour process is

hiscolumn is givenas of

a singlecore If youhave
f using
iexam
24111mmol
3EM
20

4 cores this can go up SHR is how much of the cached
5 3
yaw jsed 5

to yoo RES issharedwithother menHmmmmmmmm mm y o

q processes swp4 mm 2156


sort

16

unig

sort sorts sort n sort h human sort

numeric sort

its input sort n'order sort h order

sort order i'sortn'order 156 45K

sort names.tt
n
iz
15000
ii
i
iz
48 f h
it
som
45K
2000
i son
156

the default sort is it 48 i 96 2000

6020 6020 usefulexample


alphabetical 96 ii 15000

I du sh I sort h

unig

removes duplicates sort uniq e


Unie C

a noticethere Pipesomething to counteachline it saw

b
Ar are still 2 sort unig'andyou'll Recipe getthetop 10most

b
b a'sr.um.ci getadedupiicateoiist commoniinesinatiie

a a onlyuniquifies oflines mmTort


udoestheSsortfoo.tx t I

c adjacent
matchinglines
sbamething
a 1 sort u
a
b
lunig
1 sort n
c
j yE

c b p l tail n lo
t

misc commands

17

rt wrap watch pv

adds rerun a commandevery µ pipeviewer givesyou stats


history ctrl on data goingthrough apipe

support to REPLS that 2 seconds

don't

alreadyhave them

http EIE I

fi
t i
eisll a
tin's oar

1 s nadu diff
I

add a timestamp in front figure out whatisusing diff 2 files Run with

of everyinput line all your disk space U 8 for context

COMM column sell x clip

I
fiance effmmsoonrted files

from system

format input opaggt.e


qq.p.gg

into lpbc.org pbpasteonMa


head tail 18

head tail n NUM

showsyou the first tail showsthe last 10lines n NUM eitherhead or tail

10 lines of a file changethe shown

f gmmftaiwmeo.ee

if youpipe a program'soutput head n NUM Sb tah'd

tohead the program will printanynew linesaddedto tail n Num

stopafter printing 10 lines theend of FILE Superuseful last 1first

itgetssent516PIPE for logfiles9 NUMlines

C NUM tail retry tail follow _name

showthe first Nast NUM keeptrying to open µ Usually tail f will follow

bytes of the file file if it's inaccesible a file descriptor

uenanef
a fouow name

head

ftp
will keepfollowingthesame

filename
eg if the file

will showthe first stopwhen process PD descriptor is rotated

1024bytes stopsrunning with f


less

19

less is a
pager many Vim shortcuts less r

work in less displaysbash escape

that means it lets you

uoirejipenootinefiet.IT files
search codes as colours

n1NnextIprevmatch tryis colorless r

man uses j 1K down1up a line with r without r


ugh
your pager

usually less to display


m markreturn to line a txt a txt
GIG beginninglend of file ESCEomEsc

man

pages t
Igginvim
a
tft
red
92 Col31mAtxt.gz
Esccom

9 F t

quit press F to keepreading


runs a commandwhen
less starts
VTwercase from the file as it's less F followupdates

II

updated clike tail f less to istgrtofata.ie

eoitfiieinya.rs or

press Ctrl1C to stop less 20 start 20

arrowkeys HomelEnd readingupdates into file

Up Dn work in less less Hoo search forfoo


Pg Pg

right

Kill 20

kill doesn't
just kill whichsignal kill sends kill 1

programs name nvm lists all signals

kill SIGTERM 15 1 Hop 2 INT 3 Quit 4 ILL

f
5Trap 6 ABRT 7 Bus 8 EPE

t hiiii.int
kill Hop
ua
SIGHUP 1
ii i i ii iiiii
Matus 18CONT 19stop zorstp

2ITTIN 221100 23

kill stop SIGstop Iq urozyxcpu.z


sxeszzovtn.ir
m z proe 28winch

kill SIGNAL PID 29POLL30Pwr 3 sis

nameornumber

killall SIGNAL NAME

signalsallprocessescalledNAME
pgrep
prints MDs ofmatching
pkill
same as pgrep butsignals

forexample runningprograms plDsfound ex

firefox

useful

akgils.la
f pgrepfiremantoghesasnfiqffff.sn
pkiu.ffirefox µ

eosaroitetsosresatosoisienated

Tines h.at e nmmandEEiIiisaenPfih'esmgeaythaO

teforesignalling

cat friends 21

cat concatenates files youcan use cat as an cat n

EXTREMELYBASIC text

editor prints out thefile with

pslriffstcomnfehfs.to line numbers

orun cat sfiie.t


myfiie 1 Once upon a midnight

typethecontents don'tmake

cat txt mistakes 2 Over many a quaint

prints all.ttfilesput press ctrl d to finish 3 While I nodded nearly

together

2 cat tee howto redirect to

a file owned by root

cats a gzipped file tee file txt will write sudoecho hi x txt

its stdin to both

Ashatfalyripifsthaaf fines

Cd but easier
f stout and file.tt
stdout
Hthswii open as your
user not as root so itfails

gzip stdin txt


tee a echo hi lsooo tee a x txt

to remember willopen x txt as root


a txt

Is of

22

Isof what Is of tells you p PID

foreachopenfile

stands for distepenfiles list the files PID has 1

tepid open
et're't.EE45ocEiItoy
Ti s

r filedescriptor CFDcolumn
somer

listjust the openfiles

Icante Iffileername
socketaddress
in Isomeldir

finddeleted files netstat

listopennetworksockets

sockets are files9 1 soft grepdeleted anotherwayto list open

ex sockets on Linuxis

p name.nl
don'tresolve
filesfrom
roeote
iii
o netsta tunap
7
f

i Gogo hostnamesIports
s
tunaplease.r
also Pni lproclapio Ifdlefd

i Tcp 7 On Mac netstathas

i s TCP LISTEN processthatopened the file


different

more useful tools

make screen diff U

jog tmux ripe

nohup date image magick

disown entr fish

cutlpaste seq ranger


chronic

sponge join

xxd parallel

hexdump GNU parallel

objdump pigzipixz

strings sort parallel

love this

find more awesome zines at

jvns.ca zines

Ac

By NC SA computerwizard industries 2018

You might also like