linux commands cheatsheet
Tags: linux, productivity, tooling, beginners
Published: 2021-09-03 12:19:20

[Unix](https://en.wikipedia.org/wiki/Unix) was published in 1973. Since them there where many different operating systems build using the same standards as Unix. The most prominent are Linux and MacOS. More than 90% of cloud infrastructure and more than 80% of smartphones in the world are based on Linux.
If you want to see whats going on an EC2 instance or filter some log files you can always use the command line tools.

Using the command line tools for administrative work or lookups will save a lot of time and increase the productivity significantly compared to using only graphical interfaces.

As a webdeveloper we use the following commands on a daily basis. If you are new to this topic, I hope this will help you increase productivity.

## Orientation
| command | explanation|
| ----------- | ----------- |
| history | displays the history of the last used commands |
| pwd | print working directory  |
| man [command] | manual |
| echo | echoes an argument |


## Navigating in the terminal
When we keep our hands on the keyboard we are always faster, than when use the mouse or similar to navigate around. So we should make use of the following commands, when we are in a terminal.

`ctrl + a`: jump to the beginning of a line
`ctrl + e`: jump to the end of a line
`ctrl + w`: remove word
`ctrl + u`: remove whole line
`option + left arrow`: jump to the end of a word
`option + right arrow`: jump to the beginning of a word
`tab`: autocompletion
`arrow up`: show last used command
`cmd + k`: clear terminal screen


## Navigating in the file system
| command      | explanation |
| ----------- | ----------- |
| ls      | list - lists all files in the current directory     |
| ls -a   | lists _all_ but also shows the .hidden files |
| ls -la  | lists _all_ files in the _long_ file format   |
| cd / | navigate to the root directory |
| cd ~ | navigate to the home directory |
| cd .. | navigate one directory level up |
| cd - | navigate to the previous directory |

## Administrative tasks
| command      | explanation |
| ----------- | ----------- |
| sudo  |  superuser do    |
| mkdir [directory_name]  |  make directory    |
| rmdir |  removes a directory   |
| touch [file_name]    |  creates a file    |
| cp old_file_name new_file_name |  copy   |
| mv old_file_name new_file_name |  move   |
| rm [file_name] |  remove   |
| rm -r |  removes recursive, ie a Folder with all contents   |
| vi [file_name] |  opens file with vi  |
| nano [file_name] |  opens file with [nano](https://www.nano-editor.org/)   |
| code [file_name] |  opens file with vs code  |
| program_name | to start a program simply type the name, ie python   |
| ctrl + c | stop a program |

## Searching and filtering
| command      | explanation |
| ----------- | ----------- |
| head [file_name] |  show the first 10 lines of a file   |
| tail [file_name] |  show the las 10 lines of a file   |
| sort [file_name] | sorts the file alphabetically  |
| sort [unsorted_file_name] > [sorted_file_name] | sorts the content of the unsorted file and stores it in a new sorted file  |
| cat [options] [file_names] | reads files sequentially, writing them to standard output |
| grep [options] pattern [file_names]| globally search for regular expression and print |
| the pipe | The pipe send the output of one command as input of another command. |

As `grep`, `cat` and `|` are powerful tools I recommend having a look at [this awesome tutorial](https://www.guru99.com/linux-pipe-grep.html).

## Permissions
In Unix Systems there are three groups of ownership. 
- *USER*: the one who created the file
- *GROUP*: a group with assigned users
- *OTHER*: any other who has not created the file and does not belong to a group.

The UNIX system has 3 permissions defined for the 3 different owner groups:

*r* = *read* permission
*w* = *write* permission
*x* = *execute* permission

– = no permission
d = directory
@ = [extended attributes on macOS](https://en.wikipedia.org/wiki/Extended_file_attributes#macOS)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8ny2a7bomxfxq487dpz.png)
 

### Changing permissions
| command      | explanation |
| ----------- | ----------- |
| chmod | change mode |
| chown | change owner |

Detailed information for the permission system can be found [here](https://linuxcommand.org/lc3_lts0090.php).

This post is outlining the most important commands (for me). If you want to see all commands you might want to have a look at this [list](https://en.wikipedia.org/wiki/List_of_Unix_commands) or use the `man` in the terminal.