codea.live
Search…
Bash - The Basics
Get started with bash and learn maximize your efficiency with Linux!

In This Guide...

After this guide you will:
Prerequisites: none.

What is Bash?

Bash is a shell and command language that allows you to efficiently perform tasks. It comes with Linux, and MacOS. You can also install it on Windows.
Let's say for example, you wanted to create a folder and put 100,000 text files in it, with a unique name. This is something you can do with bash.
1
$ mkdir text
2
$ touch text/{1..100000}.txt
Copied!
Now let's count how many files were created in this folder.
1
$ ls text | wc -l
2
100000
Copied!
We successfully created 100,000 text files!
This was just a demo, so don't worry about memorizing these commands. The basics are most important, and will be covered first.

How the Terminal Works

What is the terminal?

The terminal, or command line interface (CLI), is a program that accepts text input to execute functions.

What is a command?

A command is text in a command line that executes a program. Commands are simply programs.
echo “Hello World” is equal to /usr/bin/echo “Hello World”

Command Anatomy - Prefix

Command Anatomy - Command

What is the PATH?

The PATH stores where commands are located - more specifically, all directories where they are found. This makes it easy for bash to find commands to be found by name, as it knows where to look.
1
$ echo $PATH
2
/usr/bin/node/bin:/home/adam/.local/bin:/home/adam/bin:/usr/bin/node/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Copied!
You may get different output, with different operating systems. Ubuntu 20.04 was used for this demo.

man - The Command Manual

The manual lets you find all you need to know about a most commands.

Syntax

1
$ man <command_name>
Copied!
"man is the system's manual pager. Each page argument given to man is normally the name of a program, utility or function. The manual page associated with each of these arguments is then found and displayed. A section, if provided, will direct man to look only in that section of the manual. The default action is to search in all of the avail‐ able sections following a pre-defined order (see DEFAULTS), and to show only the first page found, even if page exists in several sections."
— Manual page on the man command.

What if the manual does not cover it?

The help command is available for some default bash commands. For example, cd does not have a manual page, and details info can be found with it.
1
$ help <command_name>
Copied!

Navigation - Show Files

List Files

The ls command is used to list files in a directory.
1
2
file.txt hello.earth testing.123
Copied!
When opening the terminal, the default user directory is denoted by the~symbol. In this case ~ refers to /home/adam. The root user, has /root as the default directory.

More Readable File List

The list command has arguments to make showing directory files easier to read. In this case, -lh refers to list, human readable. Type man ls to see more arguments.
1
[email protected]:~/test$ ls -lh
2
total 8.0K
3
drwxrwxr-x 2 adam adam 4.0K Apr 12 18:49 files
4
-rw-rw-r-- 1 adam adam 12 Apr 12 18:31 file.txt
5
-rw-rw-r-- 1 adam adam 0 Apr 12 18:24 hello.earth
6
-rw-rw-r-- 1 adam adam 0 Apr 12 18:24 testing.123
Copied!

File Metadata

The file command is used to get the metadata of a file.
1
$ file file.txt
2
file.txt: Bourne-Again shell script, ASCII text executable
Copied!

File Extensions Don't Matter!

In Linux, file extensions are not used by bash to determine the file type. Instead, the data inside the file is used to determine the file type.
file.txt is read as a shell script in Linux, and would be read as a text file on Windows.

Navigation - Move Around

pwd - Print Working Directory

The pwd command is used to output where the command is being executed in.
2
/home/adam
Copied!

cd - Change Directory

The cd command is used to change directory. In this case, we move from ~ to ~/test.
1
$ cd test
2
$ pwd
3
/home/adam/test
Copied!
Some directories, like /root, require superuser permissions to go there. You can type sudo cd /root to execute a command as the superuser.

File Management - CRUD

CRUD stands for Create, Read, Update, Delete, and defines the basic operations for file management.

Create - Files and Folders

1
$ touch <file_name> # create an empty file
2
$ mkdir <file_name> # create an empty directory
Copied!

Read - with Less

Less is a terminal-based pager in Linux that is used to view text, in pages.
1
$ less <file_name>
Copied!

Update - with Nano

While there are many ways to update files, using Nano, a terminal-based text editor, can be quicker for more specific updates.

Syntax

Here is how to open a file with nano:
1
$ nano <file_name>
Copied!

Nano Editor Preview

There are many keybinds with nano. Your editor may look something like this:
1
GNU nano 4.8 file2.txt
2
3
// type something here, then press
4
'Ctrl + X', then 'Enter'
5
// on Windows to save
6
7
8
9
10
[ New File ]
11
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos
12
^X Exit ^R Read File ^\ Replace ^U Paste Text^T To Spell ^_ Go To Line
Copied!
For more keybinds, you can check out this nano cheatsheet.

Delete - Files and Folders

Syntax

1
$ rm <file_name> # delete file(s)
2
$ rm -rf <file_name> # 'recursive force' - delete folder(s) and contents
Copied!
rm -rf can be a dangerous command. Once a file is deleted, it cannot be recovered easily.

Safer Deletion

With the interactive flag you can more carefully delete files and folders, with a confirmation prompt.
1
$ rm -i file.txt
2
rm: remove regular file 'file.txt'?
3
$ # type 'y' or 'n'
Copied!
The trash command can be made to make deletion safer. It's actually an exercise at the end of this section.

File Management - Copy and Move

cp - Copy

The cp command is used to copy files, or directories, to another location. This duplicates the files/folders.

Syntax

1
$ cp file1.txt files
2
$ ls files
3
file.txt
Copied!
Again, when copying folders, the -r (recursive) flag needs to be used.

mv - Move

The mv command is used to move files or folders, to another location. This will remove the file/folder from the old location, and add it to the new location.

Syntax

1
$ mv <old_path> <new_path>
Copied!

Rename with the Move Command

The move command can also be used to rename files/folders.
1
$ mv file1.txt file2.txt
Copied!
You should now be ready for this challenge. Doing is the most effective way of learning, and will help reinforce your knowledge and understanding.
This content is from an upcoming Raspberry Pi hosting course.
Last modified 4mo ago