Linux Commands for beginners
Published: 2021-02-18 10:08:37

## Opening a terminal
Most Linux systems use the same default keyboard shortcut to start it: Ctrl-Alt-T

## Moving Around
1. `pwd`: pwd is an abbreviation of ‘print working directory’. All it does is print out the shell’s current working directory.
2. `cd`: ‘change directory’
3. `ls`: lists directory contents of files and directories
4. Relative and absolute paths
Relative Paths: The place you end up at depends on your current working directory. 
Absolute Paths: No matter what your current working directory is, they’ll have the same effect.
* `cd`: when you run cd on its own to go straight to your home directory. 
* `cd /`: to switch to the root directory. 
* Any path that starts with a forward slash is an absolute path. You can think of it as saying "switch to the root directory, then follow the route from there."
```
cd /etc  //goes to root directory first and then to etc
```
*  There’s one other handy shortcut that works like an absolute path. As you’ve seen, using “/” at the start of your path means “starting from the root directory”. Using the tilde character (”~”) at the start of your path similarly means “starting from my home directory”. 
```
cd ~
cd ~/Desktop
```

## Creating folders and files
1. `mkdir` is short for ‘make directory’.
```
mkdir dir1 dir2 dir3 // create all the folders in one directory.
mkdir -p dir4/dir5/dir6 //create in nested structure 
```

2. Creating new files
(a) Create a File with Touch Command 
```
touch test.txt
```
This creates a new empty file named test.txt

(b) Create a New File With the Redirect Operator 
A redirection operator is a name for a character that changes the destination where the results are displayed.

*Right angle bracket >*

This symbol tells the system to output results into whatever you specify next. The target is usually a filename. You can use this symbol by itself to create a new file:
```
test2.txt
```
This creates a new empty file.

(c) Create File with echo Command
```
echo ‘Random sample text’ > test4.txt
```

## Editing files
Linux file system allows us to operate various operations on files like create, edit, rename, remove. We can edit files by different Linux editors like vim, nano, Emacs, Gedit, Gvim, and more.
We will cover nano & vim for this article. 
*Difference between Nano and Vim* 
Nano is simple, vim is powerful.
If you only want to simply edit some textfiles, nano will be enough. 
### 1. VIM
The Vi editor has various modes like normal mode, insert mode, command mode, line mode, and more. 
*How to switch a mode in Vi editor?*
* Press the `ESC` key for normal mode.
* Press `i` Key for insert mode.
* Press `:q!` keys to exit from the editor without saving a file.
* Press `:wq!` Keys to save the updated file and exit from the editor.
* Press `:w` test.txt to save the file as test.txt

### Edit test.txt
```
vi test.txt
```
This command will open the file with the Vi editor in normal mode. To switch it to insert mode press `ESC` key followed by `i` key. Place the cursor on your desired position and enter some text. To save the file and exit from the editor, press the 'ESC' key, followed by `:wq!`. 

### 2. NANO
It is a built-in editor for Linux distributions.
In nano, no primary command is used to operate on the file. All the basic operations are displayed at the bottom of the editor. We can trigger them with a CTRL key, for example, to save the file press CTRL+O keys, to exit from the editor press CTRL+X key.

To edit a file with the nano editor, open the file from the directory where it is stored with the following command:
```
nano test.txt  
```
This will open the test.txt file with nano editor. To edit the file, move the cursor and enter some text and press the CTRL+O keys to save the file.
Press CTRL+X keys to exit from the editor.