Bash Terminal Guide
Abayomi Ogunnusi
Posted on November 6, 2021
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 Bash
See this post as cheat sheet to using the terminal.
click to download Git Bash.
TOC
π― Basic commands
π― Working with dates
π― Directories
π― Navigating the terminal
π― Working with files
π― Pagination
π― Copying files
π― Find commands
π― grep
π― More basic commands
Part2:
π₯¦ Vim : Introduction
π₯¦ Navigating around vim
π₯¦ Insert Mode
π₯¦ Saving
π₯¦ Delete, cut and paste
π₯¦ Saving and quitting
π₯¦ Saving file and still remaining in insert mode
π₯¦ Search and replace
π₯¦ Vim config
β£οΈ Conclusion
β£οΈ Reference
Basic command
π₯¦Clear screen
Ctrl + l
π₯¦ Go to the start of a line
ctrl + a
π₯¦ Go to the end of a line
ctrl + e
π₯¦ Go back one word
alt + b
π₯¦ Go front one word
alt + f
π₯¦ Delete word backward
alt + backspace
π₯¦ Word forward
alt + d
Working with dates
π Get present date and time
date
π Get calendar (NA)
cal
π Get current user
whoami
Manual section
π Get manual (NA)
man
π Get manual help
man --help
Directories
π Make a new directory
mkdir test
π print working directories
pwd
π List storage (folder or file content)
ls
π Shows files within directory
ls -a
π Shows all contents (read, write, execute)
ls -al
Navigating the terminal
π Change directory
cd <folder name>
e.g cd Desktop/
π Go back one folder up
cd ..
Go back two folders
cd ../..
Go to the previous folder
cd -
π Go to the root folder
cd ~
π Go back to the previous folder
cd -
π Remove folder
rm -rf testfolder
π― Steps to copy a folder into another
mkdir folder1
mkdir folder2
mv folder1 folder 2
Explanation: The above command means move folder1 into folder2
π Move folder back to its former root
mv folder2/folder1 .
Explanation: The above command means moving folder inside folder2 (i.e folder1) into the current directory .
π Make subdirectory
mkdir -p folder1/folder2/folder3
Explanation: The above command means folder2 is created in folder 1 and folder 3 is created in folder2
ποΈ Renaming with the mv
command
mv folder1 newfolder
Explanation: The above command means rename folder1 into newfolder
Working with files
π Create a file
touch index.ts
π Steps to move files into folder
mkdir testfolder
touch index.html
mv index.html testfolder
ls testfolder (to see the contents inside testfolder)
ποΈ Remove all files (dangerous)
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
touch index.html main.js style.css
Writing into files
π write
mkdir test
cd test
echo "hello world" > index.html
π To read the file (see content inside)
mkdir test
cd test
echo "hello world" > index.html
cat index.html
π Append to files
mkdir test
cd test
echo "jeeeeez you are so cool" >> index.html
cat > index.html
Explanation: one > overrides while double >> appends.
Line breaks
echo "heloo" \n "hi there" \n "this is me" >> index.html
π―: \n adds a new line to the line statement
Pagination
This section is for paginating output using the less, Head, and Tail
less index.html
press spacebar to move to the next sets of lines
π See the first 10 lines
head index.html
press spacebar to move first 10 lines
π See the last 10 lines
tail index.html
press spacebar to move first 10 lines
Copying files
To leave original intact
cp index.html index2.html
cat index2.html
Explanation: This copies all internal content from index.html into index2.html
π±οΈ Copy everything into a folder
mkdir newfolder
cp *.html folder3
ls newfolder
π±οΈ Copy folder
cp -r newfolder newfolder2
ls .
Find Commands
π find all files and folder inside a directory
find .
π Specific search query
find . -name index.txt
π Find by type
find . -type f -name index.txt //the f means file
find . -type d -name index.txt //the d means directory
π Ignore case sensitive in search
find . -type d -iname index.txt
π Find all file type
find . -type d -iname "*.json"
π Find all empty file
find . -type f -empty
πποΈ Find and Delete
find . -type f -name index.js -delete
grep
This is a global regular expression print that is used to locate text in files.
grep -r "hello" newfolder
grep -rn "hello" newfolder // n is the line number
Explanation: This means find the word hello in the directory called newfolder
grep -rni "put" .
Explanation: 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
history //This is a list of all the commands we've used since
π― To re-execute any from the history.
!676
π― Get the previous command
!!
π― Search as you type
ctrl + r
Let's take a short pause before diving into vim's features.
Now that you've returned, let's get started with this question. What exactly is vimβ
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 curl. The steps are giving below.
click raw
π₯οΈ I will use curl to pull data from GitHub
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
vim Footer.test.tsx
π‘ Open a new file in vim
vim test.js
π‘ To see the line mode inside the file
:set nu or set number
π‘ To enable syntax highlighting
:syntax on
The command above opens vim in command mode.
π« Quit out of command mode
:q!
Navigating around vim
π§π½ββοΈ To move words by words
h β¬
οΈ - go left
j β¬οΈ - go down
k β¬οΈ - go up
l β‘οΈ - go right
π Move forward
w
π Move backward
w
π Go to the beginning of the line
0
π Go to the end of the line
$
π Go to the beginning of the line after white spacing
^
π Press g twice to go to the top of the code
gg
π Press G twice to go to the end of the code
G
π Go down the line by line
shift + ]
π Go up line by line
shift + [
π Go to a specific line number e.g line 1 press1 and gg
1gg
Insert Mode (Vim)
i
Press ESC to switch to command mode.
πOpen vim and jump to the actual line number
vim +20 index.html
π To undo
u
π To redo
ctrl + r
π To insert after the current line
o
π Insert before the current line
shift + O (i.e Capital O)
Saving
πΎ Save changes after inserting
:w then :q!
Explanation: w means write and q quits from insert mode
πΎ To save and quit with one command
:wq
i.e write and quit
Delete, cut, and paste
ποΈ Delete entire line
dd
ποΈ Delete 4 lines
4dd
ποΈ Delete word
dw
ποΈ Delete everything
dG
βοΈ Cut and paste (Vim)
dd to cut
j to go down
p to paste
ποΈ Delete to the beginning of the word
db β¬
οΈ
ποΈ Delete to the end of the word
β‘οΈ
πΎ Saving and quitting Vim
:q!
The above command means close file and abandons changes
:wq
Explanation: The above command means save file and close the file
Write a command and press the Tab key or double-tap
πΎSave the file and still remain in insert mode
esc
:w
write a few more codes
:w
:q
Search and replace
π To search, you must be in the command mode
vim test.js
esc
/ <type words to search here>
enter
π Navigate to the next search result
n
π Return to the previous outcome.
shift + n or N
Vim config
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
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
Posted on November 6, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.