Note: In these, and all other examples in the cheatsheets, the $ prefix before a command indicates the use of a linux command line input. It is not part of the command - it is there to show you which lines are input, and which are output (lines without a $ prefix)
Where are you on this file system? Find out with pwd:
$ pwd
/var/www/html
Useful just after landing a shell or if you get lost.
The cd command changes directory:
$ pwd
/home/mac
$ cd /opt
$ pwd
/opt
Go to your home directory:
$ cd ~
Go back to the previous directory:
$ cd -
Go to the root of the filesystem:
$ cd /
List files in the current working directory:
$ ls
List all files, including hidden ones (preceded by a ., e.g. .env):
$ ls -a
Display extra information about files, including timestamps and file ownership:
$ ls -l
Combine the two:
$ ls -la
The su command switches to another user on the machine. You must know their password.
This command will attempt to switch to the jane user:
$ su jane
If no username is provided, su will attempt to login as root:
$ su
Using the - flag simulates a real login - you will often see su - in writeups as shorthand for su root:
$ su -
However this command is effectively the same as the one above, apart from a few small differences like the clearing of environment variables.
grep is an incredibly useful command that allows searching the contents of files and the outputs of terminal commands.
Grepping a File
$ grep [SEARCH_TERM] /path/to/file
or, using pipe (|):
$ cat /path/to/file | grep [SEARCH_TERM]
Grepping Terminal Output
You can use the pipe operator to run grep on the output of any command. A common use case is searching the output of a directory.
$ ls -la | grep [SEARCH_TERM]
awk can be used for manipulating structured text and extracting specific fields. Think of it as a way of selecting a specific column from a load of structured data.
For example, a list of employee names:
$ cat employees
John Doe
Jane Doe
awk could be used to extract the first names of these employees:
$ cat employees | awk '{print $1}'
Where $x represents the xth column (1-indexed).
You can specify a different ‘field separator’ with the -F flag:
$ cat employees
John:Doe
Jane:Doe
$ cat employees | awk -F ':' '{print $1}'
John
Jane
#cheat-sheet #unix #fundamentals