DEV Community

Cover image for Bash Terminal Guide
DrSimple
DrSimple

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

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
DrSimple

@kwadoskii I'm delighted you found it useful.

Collapse
 
drsimplegraffiti profile image
DrSimple

@michaelcurrin thanks for the observation. I will update

Collapse
 
drsimplegraffiti profile image
DrSimple

@bobbyiliev thanks for the e-book

Collapse
 
drsimplegraffiti profile image
DrSimple

@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