Published on

UNIX Commands Cheatsheet

Another day, another cheatsheet?

Well, not trying to bombard the web with yet another cheatsheet, but I thought that this would be a good opportunity to refresh myself on UNIX and LINUX commands. I do not have a background in Computer Science, so I am just picking up these topics along the way to acquire a more well-rounded background knowledge base.

I'm not going to get into the details of Unix (see wiki here)... but I mostly need to have basic knowledge of the commands to navigate the terminal in Mac OS, most developers do.

The statement below kind of sums it up:

Mac OS X is a Unix OS and its command line is 99.9% the same as any Linux distribution. Bash is your default shell and you can compile all of the same programs and utilities.

Below is the lists of useful commands but keep in mind that I run MacOs with zsh so there might be some subtle differences in the command or command flags.

Basic System Commands

CommandDescriptionExample Usage
manhelp docs, manual on the commandman echo
whoamithe current userwhoami
caldisplay current month calendarcal
datedisplay current datedate
historyshows the history list of commands usedhistory
history> -N, a flag to show last N commands (zsh)history -10

When in man mode, pressing q will quit and close out that mode and pressing spacebar will advance to next section.

Folders and File Commands

CommandDescriptionExample Usage
pwdpresent working directory, where you current arepwd
mkdirmake a folder with given folder namemkdir myFolder
mkdirmake multiple folders with that begin with same namesmkdir folder1 folder2
mkdirsimplify making multiple folders that begin with same namesmkdir folder3
mkdir> -p, flag to make parent folder with child folder insidemkdir -p parentFolder/childFolder
rmdirremove the given folderrmdir myFolder
rmdirremove multiple folders that begin with the same namesrmdir folder*
touchcreate a file with given file nametouch myFile.txt
rmremove the given filerm myFile.txt
lslist the folders and files at present working directoryls
ls> -l, flag to show long formatls -l
ls> -a, flag to show all files, including hiddenls -a
ls> -t, flag to show time, descendingls -t
ls> -r, flag to show in reverse orderls -r
cpcopy a file or folder into a target foldercp myFile.txt myFolder
cp> -r, flag for recursive, takes all the files in a foldercp -r myFolder1 myFolder2
mvmove or cut and paste, move folder into target foldermv myFolder1 targetFolder
mvmove multiple files into target foldermv {file1.txt,file2.txt} targetFolder
mvrenames a file name to another file namemv oldFileName.txt newFileName.txt

CAT Commands

Concatenate (CAT) command is used frequently to read data from a file and gives the content as output.

CommandDescriptionExample Usage
catoutput content of filecat myFile.txt
cat> -n, flag to show line numberscat -n myFile.txt
catcreate file and allow insertion of data in linecat > myFile
catcopy file content into another file (overwrite into file)cat fileContent > newFile
catcopy by merging of two filescat file1 file2 > file1n2
catcopy file content into another file (append into file)cat fileContent >> newFile

GREP Commands

Global search for regular expression and printout, global-regular-expression-print (grep), searches a file for a particular pattern of characters, and displays all lines that contain that pattern.

GeeksForGeeks has a collection of great resources with examples on the grep command as well as other Unix commands.

CommandDescriptionExample Usage
grepsearches the file given a search termgrep "searchTerm" fileName
grep> -i, flag to ignore case sensitivegrep -i "searchTerm" fileName
grep> -n, flag to also return the line number of foundgrep -n "searchTerm" fileName
grep> -l, flag to return fileName if search foundgrep -l "searchTerm" fileName
grep> -w, flag that matches exact wordgrep -w "searchTerm" fileName
grep> -v, flag that ignores the search term, returns inversegrep -w "searchTerm" fileName
grep> -c, flag to display the count of search matchesgrep -c "searchTerm" fileName
grepchaining flags to search ignore case w/ line numbergrep -in "searchTerm" fileName
egrepsearches a file given multiple search termsegrep "firstTerm|secondTerm" fileName

Other Commands of Interest

CommandDescriptionExample Usage
>redirects standard outputgrep "searchTerm" fileName > newFile
>>redirects and appends standard outputgrep "searchTerm" fileName >> newFile
<redirects standard inputgrep "searchTerm" < fileName
|pipe symbol, chain two or more commandsls -l | grep -i folderName
findfind in current directory and all subdirectoryfind . | grep fileName
diffdisplay differences in two filesdiff fileOne fileTwo
diff> -y, flags to show all lines compared side by sidediff -y fileOne fileTwo