Bash Terminal Guide
Tags: bash, tutorial, programming, webdev
Published: 2021-11-06 02:12:10

Good day, wonderful friends; today we'll be talking about the terminal, the dreaded black thing. Although the GUI is appealing to use, we must embrace the terminal as software engineers. We'll focus on Bash, which is also known as Bourne Again Shell. Let's take a swing at the <mark>Bash</mark>
![bash](https://media.giphy.com/media/HY0fZB9iNGEo5kSjiY/giphy.gif)

```html
See this post as cheat sheet to using the terminal.
```
[click](https://git-scm.com/) to download Git Bash.

---

##<abbr title="Table of Content">TOC</abbr>
🎯 [Basic commands](#basic-c)
🎯 [Working with dates](#wwid)
🎯 [Directories](#direct)
🎯 [Navigating the terminal](#terminal)
🎯 [Working with files](#files)
🎯 [Pagination](#pagine)
🎯 [Copying files](#copy)
🎯 [Find commands](#find)
🎯 [grep](#gre)
🎯 [More basic commands](#more)

## Part2: 
🥦 [Vim : Introduction](#intro)
🥦 [Navigating around vim](#navigate)
🥦 [Insert Mode](#insert)
🥦 [Saving](#save)
🥦 [Delete, cut and paste](#delete)
🥦 [Saving and quitting](#quit)
🥦 [Saving file and still remaining in insert mode](#savefile)
🥦 [Search and replace](#replace)
🥦 [Vim config](#config)

♣️ [Conclusion](#conclude)
♣️ [Reference](#ref)

---

## Basic command<a name="basic-c"></a>
🥦Clear screen
```Javascript
Ctrl + l  
```

🥦 Go to the start of a line
```Javascript
ctrl + a
```


🥦 Go to the end of a line
```JavaScript
ctrl + e
```

🥦 Go back one word
```JavaScript
alt + b
```

🥦 Go front one word
```JavaScript
alt + f
```

🥦 Delete word backward
```JavaScript
alt + backspace
```

🥦 Word forward
```JavaScript
alt + d
```

---
## Working with dates <a name='wwid'></a>

📆 Get present date and time
```JavaScript
date
```

📆 Get calendar (NA)
```JavaScript
cal
```

📆 Get current user
```JavaScript
whoami
```

---

## Manual section

📚 Get manual (NA)
```JavaScript
man
```

📚 Get manual help
```JavaScript
man --help
```
---

## Directories <a name='direct'></a>
📂 Make a new directory 
```JavaScript
mkdir test
```

📂 print working directories
```JavaScript
pwd
```

📂 List storage (folder or file content)
```JavaScript
ls
```

📂 Shows files within directory
```JavaScript
ls -a
```

📂 Shows all contents (read, write, execute)
```JavaScript
ls -al
```

---

## Navigating the terminal <a name='terminal'></a>
📂 Change directory
```JavaScript
cd <folder name>
e.g cd Desktop/
```

📂 Go back one folder up
```JavaScript
cd ..
```

Go back two folders
```JavaScript
cd ../..
```

Go to the previous folder
```JavaScript
cd -
```

📂 Go to the root folder
```JavaScript
cd ~
```

📂 Go back to the previous folder
```Javascript
cd -
```

📂 Remove folder
```JavaScript
rm -rf testfolder
```

---

🎯 Steps to copy a folder into another
```JavaScript
mkdir folder1
mkdir folder2
mv folder1 folder 2
```
<mark>Explanation:</mark> The above command means move folder1 into folder2

📂 Move folder back to its former root
```JavaScript
mv folder2/folder1 .
```
<mark>Explanation:</mark> The above command means moving folder inside folder2 (i.e folder1) into the current directory `.`

📂 Make subdirectory
```JavaScript
mkdir -p folder1/folder2/folder3
```
<mark>Explanation:</mark> The above command means folder2 is created in folder 1 and folder 3 is created in folder2


🖋️ Renaming with the `mv` command
```Javascript
mv folder1 newfolder
```
<mark>Explanation:</mark> The above command means rename folder1 into newfolder


---

## Working with files <a name='files'></a>
📔 Create a file
```Javascript
touch index.ts
```

📔 Steps to move files into folder 
```Javascript
mkdir testfolder
touch index.html
mv index.html testfolder
ls testfolder (to see the contents inside testfolder)
```

🗑️ Remove all files (dangerous)
```Javascript
mkdir testfolder
cd testfolder
touch style-1.css
touch style-2.css
rm style-*.css

if you ls, the folder should be empty
```
📔 Create multiple files at once
```Javascript
touch index.html main.js style.css
```

---

## Writing into files
📔 write
```Javascript
mkdir test
cd test
echo "hello world" > index.html
```

📔 To read the file (see content inside)
```Javascript
mkdir test
cd test
echo "hello world" > index.html
cat index.html
```

📔 Append to files
```Javascript
mkdir test
cd test
echo "jeeeeez you are so cool" >> index.html
cat > index.html
```

<mark>Explanation:</mark>  one <mark> > </mark> overrides while double <mark>>></mark> appends.

---

## Line breaks
```Javascript
 echo "heloo" \n "hi there" \n "this is me" >> index.html

```
🎯: \n adds a new line to the line statement


---

## Pagination <a name='pagine'></a>
This section is for paginating output using the less, Head, and Tail
 
```Javascript
less index.html
press spacebar to move to the next sets of lines
```

🔖 See the first 10 lines
```Javascript
head index.html
press spacebar to move first 10 lines
```

🔖 See the last 10 lines
```Javascript
tail index.html
press spacebar to move first 10 lines
```

---
##Copying files <a name='copy'></a>
To leave original intact
```Javascript
cp index.html index2.html
cat index2.html
```

<mark>Explanation:</mark> This copies all internal content from index.html into index2.html

🖱️ Copy everything into a folder
```Javascript
mkdir newfolder
cp *.html folder3
ls newfolder
```

🖱️ Copy folder 
```Javascript
cp -r newfolder newfolder2
ls .
```

---

## Find Commands <a name='find'></a>

🔎 find all files and folder inside a directory
```Javascript
find .
```

🔎 Specific search query
```Javascript
find . -name index.txt
```

🔎 Find by type
```Javascript
find . -type f -name index.txt  //the f means file
```

```Javascript
find . -type d -name index.txt  //the d means directory
```

🔎 Ignore case sensitive in search

```Javascript
find . -type d -iname index.txt 
```

🔎 Find all file type 
```Javascript
find . -type d -iname "*.json"
```

🔎 Find all empty file
```Javascript
find . -type f -empty
```

🔎🗑️ Find and Delete
```Javascript
find . -type f -name index.js -delete
```

----

## grep <a name='gre'></a>
This is a global regular expression print that is used to locate text in files.
```Javascript
grep -r "hello" newfolder
```

```Javascript
grep -rn "hello" newfolder // n is the line number
```

<mark>Explanation:</mark> This means find the word hello in the directory called newfolder

```JavaScript
grep -rni "put" .
```

<mark>Explanation:</mark>  means find the word put in recursive mode, show the line number and make it case insensitive. The dot here means current directory

---

## More basic commands <a name='more'></a>
```JavaScript
history  //This is a list of all the commands we've used since
```

🎯 To re-execute any from the history.
```JavaScript
!676
```

🎯 Get the previous command
```JavaScript
!!
```

🎯 Search as you type
```JavaScript
ctrl + r
```

---

Let's take a short pause before diving into vim's features.
![break](https://media.giphy.com/media/RiWZUGcZPEKdQgrQ96/giphy.gif)

---

Now that you've returned, let's get started with this question. What exactly is vim❓

<a name='intro'></a>
Vim, which stands for Vi Improved, is a popular open source text editor. It enables us to write quickly in our terminal. Other editors, such as **nano**, are available, but vim will be my primary focus.

Let us see what this tool is capable of. I will start by copying a raw file from GitHub using <mark>curl</mark>. The steps are giving below.
<kbd>click raw</kbd>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vxdlpo7cb7scb2e736r.PNG)

<kbd>Copy the URL</kbd>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k7sg07vwzt9r48etufyi.PNG)

🖥️ I will use <mark>curl</mark> to pull data from GitHub
```Javascript
Go to GitHub
click on raw files e.g test
Copy the link
Open terminal, here i am using <mark>Bash</mark>
run: curl -O <paste the URL here>
```


💡 Then open the existing file with vim
```Javascript
vim Footer.test.tsx
```

💡 Result: 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wa3t86ioeo7u70zg10rm.PNG)

💡 Open a new file in vim
```Javascript
vim test.js
```

💡 To see the line mode inside the file
```Javascript
:set nu or set number
```

💡 To enable syntax highlighting
```Javascript
:syntax on
```
> The command above opens vim in command mode.


🚫 Quit out of command mode
```Javascript
:q!
```

## Navigating around vim <a name='navigate'></a>
🧘🏽‍♂️ To move words by words
h ⬅️ - go left
j ⬇️ - go down
k ⬆️ - go up
l ➡️ - go right

---

🔖 Move forward
```Javascript
w
```

🔖 Move backward
```Javascript
w
```

🔖 Go to the beginning of the line
```Javascript
0
```

🔖 Go to the end of the line
```Javascript
$
```

🔖 Go to the beginning of the line after white spacing
```Javascript
^
```

🔖 Press g twice to go to the top of the code
```Javascript
gg
```

🔖 Press G twice to go to the end of the code
```Javascript
G
```

🔖 Go down the line by line
```Javascript
shift + ]
```

🔖 Go up line by line
```Javascript
shift + [
```
🔖 Go to a specific line number e.g line 1 press1 and gg
```Javascript
1gg 
```

---

## Insert Mode (Vim) <a name='insert'></a>
```Javascript
i
```
> Press ESC to switch to command mode.

📍Open vim and jump to the actual line number

```Javascript
vim +20 index.html
```

📍 To undo
```Javascript
u
```

📍 To redo
```Javascript
ctrl + r
```

📍 To insert after the current line
```Javascript
o
```

📍 Insert before the current line
```Javascript
shift + O (i.e Capital O)
```

## Saving <a name='save'></a>
💾 Save changes after inserting
```Javascript
:w then :q!
```

<mark>Explanation:</mark> w means write and q quits from insert mode


💾 To save and quit with one command
```Javascript
:wq
```
i.e write and quit


--- 
<a name='delete'></a>

## Delete, cut, and paste
🗑️ Delete entire line
```Javascript
dd
```

🗑️ Delete 4 lines
```Javascript
4dd
```

🗑️ Delete word
```Javascript
dw
```

🗑️ Delete everything
```Javascript
dG
```

---
✂️ Cut and paste (Vim)
```Javascript
dd to cut
j to go down
p to paste
```

🗑️ Delete to the beginning of the word
```Javascript
db ⬅️
```

🗑️ Delete to the end of the word
```Javascript
➡️
```

## 💾 Saving and quitting Vim <a name='quit'></a>
```Javascript
:q!
```

The above command means close file and abandons changes


```Javascript
:wq
```
<mark>Explanation:</mark> The above command means save file and close the file
Write a command and press the <kbd>Tab</kbd> key or double-tap

## 💾Save the file and still remain in insert mode <a name='savefile'></a>
```Javascript
esc
:w
write a few more codes
:w
:q
```

---

## Search and replace <a name='replace'></a>
🔎 To search, you must be in the command mode

```Javascript
vim test.js
esc
/ <type words to search here>
enter
```

🔎 Navigate to the next search result
```Javascript
n
```

🔎 Return to the previous outcome.
```Javascript
shift + n or N
```

---

## Vim config <a name='config'></a>
```Javascript
cd ~
vim .vimrc
press i
:setnumber
:syntax on
press esc
:wq
```
Consider this to be our vim config file, similar to eslint configuration file

## Conclusion <a name='conclude'></a>
This is a step-by-step guide to using the terminal. Remember, the goal isn't to memorize the entire command, but rather to use this post as a guide, and as you practice them in small chunks, they'll become second nature. Please feel free to comment on any commands that have been omitted or incorrectly stated. Also, explain how to use commands like 'man' and 'cal' that aren't available in the git bash shell. Warm regards

## Reference <a name='ref'></a>
[Amigos Code](https://www.youtube.com/watch?v=TPRSJbtfK4M&t=7150s)
[Joe Collins](https://www.youtube.com/watch?v=oxuRxtrO2Ag&t=2276s)