command line cheat sheet for newbies
Tags: newbie, cheatsheet, beginners
Published: 2023-09-12 20:21:15

A command-line interface (CLI) is a way to interact with a computer's operating system by typing commands into a terminal.

## Structure
```bash
❯❯❯ $ command ${options} ${arguments}

# e.g.
❯❯❯ $ ls -la /document/photo 
```
- command
  - The action to be executed.
- options
  - These are optional options (or flags) that modify the command's behaviour.
- arguments
  - These are the required or optional arguments that use to specify the target. The format of arguments depend on the each command.

## Shortcut

|Shortcut|Action|
|---|---|
|Ctrl + a|Move the insertion point to the beginning of the line|
|Ctrl + e|Move the insertion point to the end of the line|
|Ctrl + b|Move the insertion point backwards |
|Ctrl + f|Move the insertion point forward |
|Ctrl + h|Delete one character behind the cursor|
|Ctrl + d|Delete a character before the cursor|
|Ctrl + u|Delete the entire line|

## Directory management

### pwd 
- check the current directory

```bash
~/Desktop ❯❯❯ pwd
/Users/user/Desktop
```

### mkdir 
- make new directory

```bash
~/Desktop ❯❯❯ mkdir myDir
```

### cd
- change directory
```bash
# change directory to myDir
~/Desktop ❯❯❯ cd myDir
# change directory one level up in the hierarchy
~/Desktop ❯❯❯ cd ..
# change directory to home
~/Desktop ❯❯❯ cd
```


## File operations

###ls 
- list files and directories
- `-a`:	list all files including hidden file starting with '.'
- `-l`: list with long format (permissions)
- `-t`: sort by time and date
- `-d`: show only the directories

```bash
~/Desktop ❯❯❯ ls -lat
total 2992
drwxr-xr-x+ 54 user  staff    1728  6 Sep 16:30 ..
drwx------@  9 user  staff     288  6 Sep 16:28 .
drwxr-xr-x@ 2 user  staff      64  6 Sep 16:28 newDir
-rw-r--r--@ 1 user  staff       0  6 Sep 16:25 file1.txt
-rwx------@ 1 user  staff  321110  6 Sep 14:04 file2.txt
-rwx------@ 1 user  staff  363448  5 Sep 08:46 file3.txt
-rwx------@ 1 user  staff  835488 26 Aug 11:21 file4.txt
```

### cp 
- copy files
- `-r`: copy directories recursively
- `-p`: preserve the attributes (mode,ownership and timestamps) 

```bash
~/Desktop ❯❯❯ cp -r originalDir copyDir
```

### rm 
- remove files and directories
- `-r`: delete directories recursively
- `-f`: Ignore nonexistent files, and never prompt before removing
- `-i`: Prompt before removal
- `-d`: delete empty directories

```bash
~/Desktop ❯❯❯ rm -d originalDir
```

### mv 
- rename or move files and directories to another location
- Adding a -v flag is to print source/destination directory

```bash
~/Desktop ❯❯❯ mv -v original.txt new.txt
original.txt -> new.txt
```

## other

### man 
- view manual pages for Unix commands
- press `q` to exit
```bash
~/Desktop ❯❯❯ man ls
LS(1)                       General Commands Manual                      LS(1)
 
NAME
     ls – list directory contents
 
SYNOPSIS
     ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when]
        [-D format] [file ...]
 
DESCRIPTION
     For each operand that names a file of a type other than directory, ls
.
.
.
```

### clear 
- clear screen
- shortcut is `Ctrl + k`

### history 
- show history of previous commands
```bash
  300  ls -lat
  301  pwd
  302  cd
  303  man ls
```

### ★　extra tip!　★
If you want to make sure a prompt always appears before deleting files you can create alias by changing configuration file. 
- `.bashrc`: Bash shell
- `.zshrc`: Zsh shell

```bash
~ ❯❯❯ echo 'alias "rm=rm -i"' >> ~/.zshrc  
```