Understanding the Command Line

Understanding the Command Line

Long Live the Command Line!

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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

echo <string>

Print string to screen

$ echo hello

man <command>

Display manual page for command

$ man mkdir

⌃C

Get out of trouble

$ tail ⌃C

⌃A

Move to beginning of line

⌃E

Move to end of line

⌃U

Delete to beginning of line

Option-click

Move cursor to location clicked

Up & down arrow

Scroll through previous commands

clear or ⌃L

Clear screen

$ clear

exit or ⌃D

Exit terminal

$ exit

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

$ echo foo > foo.txt

>>

Append output to filename

$ echo bar >> foo.txt

cat <file>

Print contents of file to screen

$ cat hello.txt

diff <f1> <f2>

Diff files 1 & 2

$ diff foo.txt bar.txt

ls

List directory or file

$ ls hello.txt

ls -l

List long form

$ ls -l hello.txt

ls -rtl

Long by reverse modification time

$ ls -rtl

ls -a

List all (including hidden)

$ ls -a

touch <file>

Create an empty file

$ touch foo

mv <old> <new>

Rename (move) from old to new

$ mv foo bar

cp <old> <new>

Copy old to new

$ cp foo bar

rm <file>

Remove (delete) file

$ rm foo

rm -f <file>

Force-remove file

$ rm -f bar

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 result

  • Using the -h (”human-readable”) option to ls, 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

mkdir <name>

Make directory with name

$ mkdir foo

pwd

Print working directory

$ pwd

cd <dir>

Change to <dir>

$ cd foo/

cd ~/<dir>

cd relative to home

$ cd ~/foo/

cd

Change to home directory

$ cd

cd -

Change to previous directory

$ cd && pwd && cd -

.

The current directory

$ cp ~/foo.txt .

..

One directory up

$ cd ..

find

Find files & directories

$ find . -name foo.

cp -r <old> <new>

Copy recursively

$ cp -r ~/foo .

rmdir <dir>

Remove (empty) dir

$ rmdir foo/

rm -rf <dir>

Remove dir & contents

$ rm -rf foo/

grep -ri <string> <dir>

Grep recursively (case-insensitive)

$ grep -ri foo bar/

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

curl

Interact with URLs

$ curl -O example.com

which

Locate a program on the path

$ which curl

head <file>

Display first part of file

$ head foo

tail <file>

Display last part of file

$ tail bar

wc <file>

Count lines, words, bytes

$ wc foo

cmd1 | cmd2

Pipe cmd1 to cmd2

$ head foo | wc

less <file>

View file contents interactively

$ less foo

grep <string> <file>

Find string in file

$ grep foo bar.txt

grep -i <string> <file>

Find case-insensitively

$ grep -i foo bar.txt

ps

Show processes

$ ps aux

top

Show processes (sorted)

$ top

kill -15 <pid>

Kill a process

$ kill -9 24601

pkill -15 -f <name>

Kill matching processes

$ pkill -9 -f spring

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 file

  • To 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 important less 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.