Essential Unix Commands and Functions

Commands

Function

Directory/Folder

A term refers to a folder used to organize different files and make locating them easier!

Bash

Bourne Again SHell

ls

lists the current working directory.

ls -F

tells ls to classify the output by adding a marker to file and directory names to indicate what they are:

  • / – a trailing / indicates that this is a directory
  • @ – indicates a link
  • * – indicates an executable

-a

-a stands for ‘show all’ (including hidden files); it forces ls to show us file and directory names that begin with ., such as .. (which, if we’re in /Users/nelle, refers to the /Users directory).

ls [path]

prints a listing of a specific file or directory

ls -s

display the size of files and directories alongside the names

ls -S

sort the files and directories by size

cd [path]

changes the current working directory

PWD

prints the user’s current working directory

Most commands take options that begin with a single –

Unix-based (macOS, Linux)

/

Windows

\

/

on its own is the root directory of the whole file system

absolute path

specifies a location from the root of the file system.

relative path

specifies a location starting from the current location.

. or cd .

on its own means ‘the current directory

..

means ‘the directory above the current one’.

mkdir [path]

creates a new directory.

cp [old] [new]

copies a file.

mv [old] [new]

moves (renames) a file or directory

rm [path]

removes (deletes) a file.

*

* matches zero or more characters in a filename, so *.txt matches all files ending in .txt

?

? matches any single character in a filename, so ?.txt matches a.txt but not any.txt

Ctrl-X, Control-X, ^X

use of the Control key may be described in many ways

Delete

The shell does not have a trash bin: once something is deleted, it’s really gone

Most files’ names are something.extension

The extension isn’t required, and doesn’t guarantee anything, but is normally used to indicate the type of data in the file

wc

counts lines, words, and characters in its inputs.

cat

displays the contents of its inputs.

sort

sorts its inputs.

head

displays the first 10 lines of its input.

tail

displays the last 10 lines of its input.

command > [file]

redirects a command’s output to a file (overwriting any existing content)

command >> [file]

appends a command’s output to a file.

[first] | [second]

is a pipeline: the output of the first command is used as the input to the second.

|

is a “pipe”. The | takes the standard output of the command on the left, and pipes it as standard input to the command on the right. You can think of this as “command to command” redirection. Example: cat volcanoes.txt | wc

Ctrl+D

log out of current session, similar to exit

Ctrl+C

halts the current command

Ctrl+W

erases one word in the current line

Ctrl+U

erases the whole line

Ctrl+R

type to bring up a recent command

whoami

who you are logged in as

date

show the current date and time

cd ..

.. is a special directory name meaning “the directory containing this one”, or more succinctly, the parent of the current directory; moves one directory above it

cd –

It brings you back after using cd .. (takes to just previous opened directory).

~

The shell interprets a tilde (~) character at the start of a path to mean “the current user’s home directory”. For example, if Nelle’s home directory is /Users/nelle, then ~/data is equivalent to /Users/nelle/data

cd

shortcut to go back to the user’s home directory.

Desciption

General syntax of a shell command