In this article, we will understand a Unix-based command line, the Bash!
What are Shell and Kernel?
In any computing system, say we consider the Operating System as a "Pistachio".
The core of the Operating System is called a Kernel. The Kernel has complete control over everything in the system and it facilitates interactions between hardware and software components.
And, the outermost layer of the Operating System is known as the Shell. It acts as an Interface between the user and the kernel. It allows a user to give commands to the kernel and receive responses from it.
A Shell can be a Graphical User Interface (GUI) such as the File Explorer or a Command Line Interface (CLI) such as Bash. The most common shell available on all Linux and Debian-based systems is BASH, aka Bourne Again Shell.
Why Would You Use CLI over GUI?
The GUI was developed within the operating system as soon as the mouse became a new input device to operate the computer.
We should admit that GUI is visually attractive and easily understood. But, for some vital tasks, CLI is way more powerful.
Here, we would like to pick some points why you would use CLI over GUI.
Less Resource It is not a secret that the text-based program needs very few resources from your computer. This means that with CLI you can do similar tasks with minimum resources.
High Precision You can use a specific command to target specific destinations with ease. As long as you don’t type the wrong command, it will work like a charm. Once you learn the basics, writing syntax is not as hard as you might think.
Repetitive Tasks Friendly GUI has developed well over the years. But, the operating system may not give you all the menus and buttons to perform all tasks. One of the reasons is safety. This leaves you overwhelmed if you have to do repetitive tasks. For example, when you have to handle hundreds of files within a folder, CLI enables you to use a single command to automate the repetition easily.
Powerful Most operating systems today prevent you from messing up the system’s core process. Windows has system protection and MacOS has SIP (System Integrity Protection). You won’t be able to perform certain tasks that are system-protected. However, with CLI, you will have full control over your system.
While the GUI may seem appealing, CLI is light, powerful and straightforward.
Source: https://www.hostinger.in/tutorials/what-is-cli
Learn enough command line to be dangerous
To understand the command syntax in Bash, let’s learn from these examples:
Basics
Command | Description | Example |
| Print string to screen |
|
| Display manual page for command |
|
| Get out of trouble |
|
| Move to beginning of line | |
| Move to end of line | |
| Delete to beginning of line | |
Option-click | Move cursor to location clicked | |
Up & down arrow | Scroll through previous commands | |
| Clear screen |
|
| Exit terminal |
|
Note:
On git bash you can use <command> —help instead of the man <command>
The Alt key acts as an option key on Windows keyboards
Manipulating Files
Command | Description | Example |
| Redirect output to filename |
|
| Append output to filename |
|
| Print contents of file to screen |
|
| Diff files 1 & 2 |
|
| List directory or file |
|
| List long form |
|
| Long by reverse modification time |
|
| List all (including hidden) |
|
| Create an empty file |
|
| Rename (move) from old to new |
|
| Copy old to new |
|
| Remove (delete) file |
|
| Force-remove file |
|
Note:
The cat command can take multiple arguments, to combine several text files into a single file
cat file1 file2 file3 > newfile
If you want to add one or more files to an existing document, use>>
When there is no diff between two files, diff simply outputs nothing
By the way,
-rtl
is the commonly used compact form, but you can also pass the options individually, like this:$ ls -r -t -l
. In addition, their order is irrelevant, so typing ls-trl
gives the same resultUsing the
-h
(”human-readable”) option tols
, lists the long form of the file with a human-readable byte count (in kilobytes)Suppose you wanted to list the files and directories using human-readable byte counts, all, by reverse time-sorted long-form:
ls -rtlh
Directories
Command | Description | Example |
| Make directory with name |
|
| Print working directory |
|
| Change to <dir> |
|
| cd relative to home |
|
| Change to home directory |
|
| Change to previous directory |
|
| The current directory |
|
| One directory up |
|
| Find files & directories |
|
| Copy recursively |
|
| Remove (empty) dir |
|
| Remove dir & contents |
|
| Grep recursively (case-insensitive) |
|
Note:
The tilde (~) is a Linux "shortcut" to denote a user's home directory. As a result, the two paths shown are identical:
/Users/sabhi/foo/projects
is the same as~/foo/projects
To make the directory foo and, within it, the directory bar (i.e., ~/foo/bar) with a single command:
mkdir -p ~/foo/bar
. Also,mkdir
accepts multiple path arguments:mkdir -p -- a/foo b/bar a/baz
If you want to copy only the files, be explicit using the star operator, as in:
$ cp ../text_files/* .
The command
rm -rf /
is unbelievably dangerous, and you should never type it into a terminal window, not even as a joke.
Inspecting Files
Command | Description | Example |
| Interact with URLs |
|
| Locate a program on the path |
|
| Display first part of file |
|
| Display last part of file |
|
| Count lines, words, bytes |
|
| Pipe cmd1 to cmd2 |
|
| View file contents interactively |
|
| Find string in file |
|
| Find case-insensitively |
|
| Show processes |
|
| Show processes (sorted) |
|
| Kill a process |
|
| Kill matching processes |
|
Note:
Downloading a file: We can download a file from the Internet using the powerful curl utility:
curl -OL <https://cdn.learnenough.com/sonnets.txt>
By default, the
head
command shows the first 10 lines of the file. Similarly,tail
shows the last 10 lines of the fileTo look at the first n lines (say 25) of the file:
head -n 25 <filename>
Using the
less
Command: Less is an awesome Linux command utility for viewing text files. The most importantless
commands:Command
Description
Example
Up & down arrow
Move up or down one line
Spacebar
Move forward one page
⌃F
Move forward one page
⌃B
Move back one page
G
Move to end of file
IG
Move to beginning of file
/<string>
Search file for string
$ /rose
n
Move to next search result
N
Move to previous search result
q
Quit less
An Extra Bit: Repeating previous commands:
So far, we’ve used the up-arrow key to retrieve (and possibly edit) previous commands, but this isn’t the only possibility.
An even quicker way to find and immediately run a previous command involves using the exclamation point !
which in the context of software development is usually pronounced “bang”.
To run the previous command exactly as written, we can use !!
“bang bang”:
$ cat hello.txt
Hello world ..!
$ !!
cat hello.txt
Hello world ..!
A closely related usage is “bang” followed by some number of characters, which runs the last command that starts with those characters.
For example, to run the last curl command, we could type this: $ !curl
This would save us the trouble of typing out the options, the URL, etc. Depending on our history of commands, the even terser !cu or !c would work as well.
A second and incredibly powerful technique is reverse-i-search
.
CTRL+R activates reverse-i-search
, which provides a nice search area, giving you an easier way to navigate through your history.
$ ⌃R
(reverse-i-search)`': curl
Check this article for more info: Bash bang commands: A must-know trick for the Linux command line | Enable Sysadmin (redhat.com)
Congratulations
!🎉
You’ve officially learned enough command line to be dangerous.
I recommend following the Learn Enough™, as it represents the shortest path to technical proficiency and software development skills.
References: Learn Enough Command Line to Be Dangerous by Michael Hartl is an introduction to the Unix command line for complete beginners.