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

Command Description Example Usage
man help docs, manual on the command man echo
whoami the current user whoami
cal display current month calendar cal
date display current date date
history shows the history list of commands used history
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

Command Description Example Usage
pwd present working directory, where you current are pwd
mkdir make a folder with given folder name mkdir myFolder
mkdir make multiple folders with that begin with same names mkdir folder1 folder2
mkdir simplify making multiple folders that begin with same names mkdir folder3
mkdir > -p, flag to make parent folder with child folder inside mkdir -p parentFolder/childFolder
rmdir remove the given folder rmdir myFolder
rmdir remove multiple folders that begin with the same names rmdir folder*
touch create a file with given file name touch myFile.txt
rm remove the given file rm myFile.txt
ls list the folders and files at present working directory ls
ls > -l, flag to show long format ls -l
ls > -a, flag to show all files, including hidden ls -a
ls > -t, flag to show time, descending ls -t
ls > -r, flag to show in reverse order ls -r
cp copy a file or folder into a target folder cp myFile.txt myFolder
cp > -r, flag for recursive, takes all the files in a folder cp -r myFolder1 myFolder2
mv move or cut and paste, move folder into target folder mv myFolder1 targetFolder
mv move multiple files into target folder mv {file1.txt,file2.txt} targetFolder
mv renames a file name to another file name mv oldFileName.txt newFileName.txt

CAT Commands

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

Command Description Example Usage
cat output content of file cat myFile.txt
cat > -n, flag to show line numbers cat -n myFile.txt
cat create file and allow insertion of data in line cat > myFile
cat copy file content into another file (overwrite into file) cat fileContent > newFile
cat copy by merging of two files cat file1 file2 > file1n2
cat copy 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.

Command Description Example Usage
grep searches the file given a search term grep "searchTerm" fileName
grep > -i, flag to ignore case sensitive grep -i "searchTerm" fileName
grep > -n, flag to also return the line number of found grep -n "searchTerm" fileName
grep > -l, flag to return fileName if search found grep -l "searchTerm" fileName
grep > -w, flag that matches exact word grep -w "searchTerm" fileName
grep > -v, flag that ignores the search term, returns inverse grep -w "searchTerm" fileName
grep > -c, flag to display the count of search matches grep -c "searchTerm" fileName
grep chaining flags to search ignore case w/ line number grep -in "searchTerm" fileName
egrep searches a file given multiple search terms egrep "firstTerm|secondTerm" fileName

Other Commands of Interest

Command Description Example Usage
> redirects standard output grep "searchTerm" fileName > newFile
>> redirects and appends standard output grep "searchTerm" fileName >> newFile
< redirects standard input grep "searchTerm" < fileName
| pipe symbol, chain two or more commands ls -l | grep -i folderName
find find in current directory and all subdirectory find . | grep fileName
diff display differences in two files diff fileOne fileTwo
diff > -y, flags to show all lines compared side by side diff -y fileOne fileTwo