DEV Community

Cover image for Bash Terminal Guide
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on • Edited on

Bash Terminal Guide

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
bash

See this post as cheat sheet to using the terminal.
Enter fullscreen mode Exit fullscreen mode

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  
Enter fullscreen mode Exit fullscreen mode

πŸ₯¦ Go to the start of a line

ctrl + a
Enter fullscreen mode Exit fullscreen mode

πŸ₯¦ Go to the end of a line

ctrl + e
Enter fullscreen mode Exit fullscreen mode

πŸ₯¦ Go back one word

alt + b
Enter fullscreen mode Exit fullscreen mode

πŸ₯¦ Go front one word

alt + f
Enter fullscreen mode Exit fullscreen mode

πŸ₯¦ Delete word backward

alt + backspace
Enter fullscreen mode Exit fullscreen mode

πŸ₯¦ Word forward

alt + d
Enter fullscreen mode Exit fullscreen mode

Working with dates

πŸ“† Get present date and time

date
Enter fullscreen mode Exit fullscreen mode

πŸ“† Get calendar (NA)

cal
Enter fullscreen mode Exit fullscreen mode

πŸ“† Get current user

whoami
Enter fullscreen mode Exit fullscreen mode

Manual section

πŸ“š Get manual (NA)

man
Enter fullscreen mode Exit fullscreen mode

πŸ“š Get manual help

man --help
Enter fullscreen mode Exit fullscreen mode

Directories

πŸ“‚ Make a new directory

mkdir test
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ print working directories

pwd
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ List storage (folder or file content)

ls
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Shows files within directory

ls -a
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Shows all contents (read, write, execute)

ls -al
Enter fullscreen mode Exit fullscreen mode

Navigating the terminal

πŸ“‚ Change directory

cd <folder name>
e.g cd Desktop/
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Go back one folder up

cd ..
Enter fullscreen mode Exit fullscreen mode

Go back two folders

cd ../..
Enter fullscreen mode Exit fullscreen mode

Go to the previous folder

cd -
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Go to the root folder

cd ~
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Go back to the previous folder

cd -
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Remove folder

rm -rf testfolder
Enter fullscreen mode Exit fullscreen mode

🎯 Steps to copy a folder into another

mkdir folder1
mkdir folder2
mv folder1 folder 2
Enter fullscreen mode Exit fullscreen mode

Explanation: The above command means move folder1 into folder2

πŸ“‚ Move folder back to its former root

mv folder2/folder1 .
Enter fullscreen mode Exit fullscreen mode

Explanation: The above command means moving folder inside folder2 (i.e folder1) into the current directory .

πŸ“‚ Make subdirectory

mkdir -p folder1/folder2/folder3
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Explanation: The above command means rename folder1 into newfolder


Working with files

πŸ“” Create a file

touch index.ts
Enter fullscreen mode Exit fullscreen mode

πŸ“” Steps to move files into folder

mkdir testfolder
touch index.html
mv index.html testfolder
ls testfolder (to see the contents inside testfolder)
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ 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
Enter fullscreen mode Exit fullscreen mode

πŸ“” Create multiple files at once

touch index.html main.js style.css
Enter fullscreen mode Exit fullscreen mode

Writing into files

πŸ“” write

mkdir test
cd test
echo "hello world" > index.html
Enter fullscreen mode Exit fullscreen mode

πŸ“” To read the file (see content inside)

mkdir test
cd test
echo "hello world" > index.html
cat index.html
Enter fullscreen mode Exit fullscreen mode

πŸ“” Append to files

mkdir test
cd test
echo "jeeeeez you are so cool" >> index.html
cat > index.html
Enter fullscreen mode Exit fullscreen mode

Explanation: one > overrides while double >> appends.


Line breaks

 echo "heloo" \n "hi there" \n "this is me" >> index.html

Enter fullscreen mode Exit fullscreen mode

🎯: \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
Enter fullscreen mode Exit fullscreen mode

πŸ”– See the first 10 lines

head index.html
press spacebar to move first 10 lines
Enter fullscreen mode Exit fullscreen mode

πŸ”– See the last 10 lines

tail index.html
press spacebar to move first 10 lines
Enter fullscreen mode Exit fullscreen mode

Copying files

To leave original intact

cp index.html index2.html
cat index2.html
Enter fullscreen mode Exit fullscreen mode

Explanation: This copies all internal content from index.html into index2.html

πŸ–±οΈ Copy everything into a folder

mkdir newfolder
cp *.html folder3
ls newfolder
Enter fullscreen mode Exit fullscreen mode

πŸ–±οΈ Copy folder

cp -r newfolder newfolder2
ls .
Enter fullscreen mode Exit fullscreen mode

Find Commands

πŸ”Ž find all files and folder inside a directory

find .
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž Specific search query

find . -name index.txt
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž Find by type

find . -type f -name index.txt  //the f means file
Enter fullscreen mode Exit fullscreen mode
find . -type d -name index.txt  //the d means directory
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž Ignore case sensitive in search

find . -type d -iname index.txt 
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž Find all file type

find . -type d -iname "*.json"
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž Find all empty file

find . -type f -empty
Enter fullscreen mode Exit fullscreen mode

πŸ”ŽπŸ—‘οΈ Find and Delete

find . -type f -name index.js -delete
Enter fullscreen mode Exit fullscreen mode

grep

This is a global regular expression print that is used to locate text in files.

grep -r "hello" newfolder
Enter fullscreen mode Exit fullscreen mode
grep -rn "hello" newfolder // n is the line number
Enter fullscreen mode Exit fullscreen mode

Explanation: This means find the word hello in the directory called newfolder

grep -rni "put" .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🎯 To re-execute any from the history.

!676
Enter fullscreen mode Exit fullscreen mode

🎯 Get the previous command

!!
Enter fullscreen mode Exit fullscreen mode

🎯 Search as you type

ctrl + r
Enter fullscreen mode Exit fullscreen mode

Let's take a short pause before diving into vim's features.
break


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
Image description

Copy the URL
Image description

πŸ–₯️ 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>
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Then open the existing file with vim

vim Footer.test.tsx
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Result:
Image description

πŸ’‘ Open a new file in vim

vim test.js
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ To see the line mode inside the file

:set nu or set number
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ To enable syntax highlighting

:syntax on
Enter fullscreen mode Exit fullscreen mode

The command above opens vim in command mode.

🚫 Quit out of command mode

:q!
Enter fullscreen mode Exit fullscreen mode

Navigating around vim

πŸ§˜πŸ½β€β™‚οΈ To move words by words
h ⬅️ - go left
j ⬇️ - go down
k ⬆️ - go up
l ➑️ - go right


πŸ”– Move forward

w
Enter fullscreen mode Exit fullscreen mode

πŸ”– Move backward

w
Enter fullscreen mode Exit fullscreen mode

πŸ”– Go to the beginning of the line

0
Enter fullscreen mode Exit fullscreen mode

πŸ”– Go to the end of the line

$
Enter fullscreen mode Exit fullscreen mode

πŸ”– Go to the beginning of the line after white spacing

^
Enter fullscreen mode Exit fullscreen mode

πŸ”– Press g twice to go to the top of the code

gg
Enter fullscreen mode Exit fullscreen mode

πŸ”– Press G twice to go to the end of the code

G
Enter fullscreen mode Exit fullscreen mode

πŸ”– Go down the line by line

shift + ]
Enter fullscreen mode Exit fullscreen mode

πŸ”– Go up line by line

shift + [
Enter fullscreen mode Exit fullscreen mode

πŸ”– Go to a specific line number e.g line 1 press1 and gg

1gg 
Enter fullscreen mode Exit fullscreen mode

Insert Mode (Vim)

i
Enter fullscreen mode Exit fullscreen mode

Press ESC to switch to command mode.

πŸ“Open vim and jump to the actual line number

vim +20 index.html
Enter fullscreen mode Exit fullscreen mode

πŸ“ To undo

u
Enter fullscreen mode Exit fullscreen mode

πŸ“ To redo

ctrl + r
Enter fullscreen mode Exit fullscreen mode

πŸ“ To insert after the current line

o
Enter fullscreen mode Exit fullscreen mode

πŸ“ Insert before the current line

shift + O (i.e Capital O)
Enter fullscreen mode Exit fullscreen mode

Saving

πŸ’Ύ Save changes after inserting

:w then :q!
Enter fullscreen mode Exit fullscreen mode

Explanation: w means write and q quits from insert mode

πŸ’Ύ To save and quit with one command

:wq
Enter fullscreen mode Exit fullscreen mode

i.e write and quit


Delete, cut, and paste

πŸ—‘οΈ Delete entire line

dd
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Delete 4 lines

4dd
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Delete word

dw
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Delete everything

dG
Enter fullscreen mode Exit fullscreen mode

βœ‚οΈ Cut and paste (Vim)

dd to cut
j to go down
p to paste
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Delete to the beginning of the word

db ⬅️
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Delete to the end of the word

➑️
Enter fullscreen mode Exit fullscreen mode

πŸ’Ύ Saving and quitting Vim

:q!
Enter fullscreen mode Exit fullscreen mode

The above command means close file and abandons changes

:wq
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Search and replace

πŸ”Ž To search, you must be in the command mode

vim test.js
esc
/ <type words to search here>
enter
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž Navigate to the next search result

n
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž Return to the previous outcome.

shift + n or N
Enter fullscreen mode Exit fullscreen mode

Vim config

cd ~
vim .vimrc
press i
:setnumber
:syntax on
press esc
:wq
Enter fullscreen mode Exit fullscreen mode

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

Amigos Code
Joe Collins

Top comments (8)

Collapse
 
bobbyiliev profile image
Bobby Iliev

This is a really great post! Well done for putting this all together!

For anyone who wants to learn more about Bash scripting, I could suggest this open-source eBook here:

GitHub logo bobbyiliev / introduction-to-bash-scripting

Free Introduction to Bash Scripting eBook

πŸ’‘ Introduction to Bash Scripting

This is an open-source introduction to Bash scripting guide/ebook that will help you learn the basics of Bash scripting and start writing awesome Bash scripts that will help you automate your daily SysOps, DevOps, and Dev tasks. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Bash scripts to combine different Linux commands and automate boring and repetitive daily tasks, so that you can focus on more productive and fun things.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Bash scripting.

πŸš€ Download

To download a copy of the ebook use one of the following links:

πŸ“˜ Chapters

The first 13 chapters would be purely focused on getting some solid Bash scripting foundations then the rest of the…

Collapse
 
michaelcurrin profile image
Michael Currin

Some fixes for you.

  • mv folder1 folder 2 take out the space
  • heloo - typo
  • you have move back and forward both as w
  • using arrow keys is far more convenient in the shell for moving across words or jumping to start or end. Try ctrl or alt and left arrow
Collapse
 
kwadoskii profile image
Austin Ofor

Thanks learnt some new commands
The vim editor is on another level.

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

@kwadoskii I'm delighted you found it useful.

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

@bobbyiliev thanks for the e-book

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

@michaelcurrin thanks for the observation. I will update

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

@aoayoola thanks for the contribution

Collapse
 
aoayoola profile image
Abayomi Ayoola • Edited

Using wildcard to remove files, you could just use

rm *.css instead

For pagination, you could also use the more command. It output the content of the file page by page

more index.html