17 commands to master the Linux Command Line
Tags: cli, bash, basic, beginners
Published: 2024-05-02 14:04:31

# The Shell 
The shell is a program (Terminal) that takes commands from the user.
The default shell program on almost all the Linux distributions is the `Bourne Again shell` also known as `Bash`. 

You can find other shells available such as 
- Korn Shell `ksh`
- Z shell `zsh`
- TENEX C shell `tcsh`

For most distributions, the shell prompt should have this format: 

```bash
username@hostname:current_directory $ 
```
The `$` is there by default and doesn't need to be added. 

```bash
$ echo Hello from bash
$ date 
$ whoami
```
The `echo` command prints the string `Hello from bash` to the display.

The `date` will print the date and the `whoami` will print your username

## Print working directory
Linux like every other Unix-based system has its files organized in a hierarchical structure and acts as a blueprint.

```bash
/

|-- bin

|   |-- file1

|   |-- file2

|-- etc

|   |-- file3

|   `-- directory1

|       |-- file4

|       `-- file5

|-- home

|-- var
```
The root directory `/` contains a number of folders and file and allows you to store more folders and files. 

Each of the location of these folders and files are called `paths`. 

For example a folder named `home` that contains a folder named `projects` its path would look like this `/home/projects`

You can use the `pwd` command to  which directory you are in. 

## change directory
`cd` means change directory. It is used to navigate from one directory to another in the filesystem. 

There are two types of paths:
1. Absolute path, which means, this path starts from the root `/`. 
```bash
/home/pictures/vacations
```
2. Relative path, which means, this path starts from the directory you are in. For example let's say you are in `/home/pictures/vacations` and you  want to get inside `vacations` to a folder named `Italy` 
```bash
username@hostname:/home/pictures/vacations $ cd Italy
```
In other words, we're navigating from our current directory to the "Italy" folder.

`.` (current directory). This is the directory you are currently in.
`..` (parent directory). Takes you to the directory above your current.
`~` (home directory). This directory defaults to your “home directory”. Such as /home/pictures.
`-` (previous directory). This will take you to the previous directory you were just at.

## list directories
You want to know what folders and files are available in a certain directory, you will need this command `ls`.

You can also specify the path you want to list the directories of 

```bash 
$ ls /home/pictures/vacations
```
Note that files starting with `.` will not be visible to us using the `ls` command but we can pass the `-a` flag to see them

```bash 
$ ls -a 
```
Another helpful ls flag is `-l` for long format, which displays the list of files in a long format. beginning on the left: file size, last modification time, owner name, owner group, number of links, file permissions, and file/directory name.

you can combine both flags to have the same combined results `ls -la`

Here are some useful flags: 
```bash
$ ls -r
$ ls -t
```
- `ls -r` Lists the current directory's content in reverse order.
- `ls -t` Lists and sorts the current directory's content by modification time starting by the newest.

## touch command
The `touch` command is used to create empty files.
```bash
$ touch my_new_file
```
The touch command will also modify the modification and access time to the current system time.

- -a: Updates only the access time.
- -c: Updates only the modification time 
- -t: Allows setting a specific date and time for the timestamps.


You can use `file name_of_file` to get a description of the file's content. 

The `cat` command allows you to display the content of a file `$ cat file_name`
You can also combine multiple files and show you the output `$ cat file_name1 file_name2`

## Less
The `less` command opens the file in the terminal
you can press `q` to quit.

- `g` moves to the beginning of the file 
- `G` moves to the end of the file
- `-N` displays line numbers
- `/search` is used to search for a specific text inside the file

## Viewing all the previous commands
```bash
$ history
```
This command is useful to see all the commands you previously used. You can also use this shortcut `!!` instead.

```bash 
$ !!
```

You can hit the up arrow to go back to the previous command instead of typing it again

if you are not sure about a certain command or remember a part of a command you can type `CTRL + R` and start typing a part of the command you want and a match will become visible to you.

You can also clear the terminal by writing clear
```bash 
$ clear
```
or you can type `CTRL + l` 

## Copy, move, make and remove files and directories.

By giving the name of the file and the path to the location you wish to copy it to, you can copy a single file. 

```bash
$ cd name_of_file /home/myFiles/newDocs
```
You can copy several files and folders and use wildcards. A wildcard is a character that can increase the flexibility of your search by substituting it for a pattern-based choice. Wildcards are additionally versatile as they can be used in any command.

- * it's used to represent all single characters or any string.
- ? used to represent one character
- [] used to represent any character within the brackets

```bash 
$ cp *.txt /home/Documents/videos
``` 
this will copy all the files with the `.txt` extension in the current directory to the videos directory.

Using the -r flag when running a command can be helpful since it copies files and directories within a directory recursively.

```bash
$ cp -r vacation/ /home/Documents/videos
```
If you copy a file to a directory that has the same filename, the file will be overwritten with whatever you're copying over. You can use the `-i` flag to avoid that.
The `-i` flag will prompt you to confirm if you wanna overwrite a file before you overwrite it.
```bash
cp -i myfile /home/Documents/
```

### moving files and directories
The `mv` command allows you to move a file to a different directory by taking the file's name and the location to where you want it moved.
This also works for directories.

```bash
$ mv file1 /home/Documents
$ mv myfolder /home/documents
```

You can also move multiple files by providing the names of the files and the path to where you want them moved. 
```bash
$ file1 file2 /home/Documents
```
You can also use the `-i` flag to prompt a confirmation before overwriting anything. 

we can also create a backup of a directory using the `-b` flag
```bash
$ mv -b myfolder myfolder_backup
```
You can also use the `mv` command to rename files like so
```bash
$ mv myfile new_file
```
### making a new directory and removing a directory
You can use the `mkdir` command to create a directory.
```bash 
$ mkdir folder folder1 
```
you can also use the `-p` flag to create subdirectories at the same time 
```bash
$ mkdir -p folder/folder1/folder2
```
You can remove files using the `rm` command 
```bash
$ rm myfile
```
Using rm doesn't mean the removed files will be in the trash. Once this command is used the files will be completely gone.

You can use the `-f` flag to force remove the files even if they're write-protected ( you can't rename, move, or delete its parent directory.)

You can also use the `-i` flag to prompt for confirmation.

You can't use the `rm` on its own to remove a directory, you need to use the `-r` flag. Using this flag will allow the removal of all the files and any subdirectories it has.
```bash
$ rm -r myfolder
```
You can remove a directory by using `rmdir` command
```bash
$ rmdir folder1
```

## Find a specific file
You can use the `find` command if you're trying to find a specific file. You need to specify the directory you'll be searching and what you're searching for. It also looks inside subdirectories.

```bash
$ find /home -name coffee.jpg
```
You can also specify the type of file you are trying to find.
```bash
$ find /home/Documents -type d -name videos
```
## Manual
If you need some information about a specific command you can use the `man` or `whatis` command. 
```bash 
$ man ls
$ whatis cp
```
## Alias
You can give an alias to repetitive commands instead of typing them especially if they're long ones.
To create an alias:
```bash
alias la= 'las -la`
```
Now you can type `la` instead of `ls -la` command and you will have the same output.

If you want to remove the alias you can use
```bash
$ unalias la
```
## Check flags
If you wanna know what flags are available for a certain command you can use `help`
```bash
$ ls --help
```

Feel free to leave a question in the comment section!