basic-commands1
basic-commands1
echo $SHELL
Command breakdown:
The echo command is used to print on the terminal.
The $SHELL is a special variable that holds the name of the
current shell.
In my setup, the output is /bin/bash. This means that I am using
the bash shell.
# output
Introduction to Bash Shell and System Commands
Bash is very powerful as it can simplify certain operations that are hard to
accomplish efficiently with a GUI (or Graphical User Interface). Remember that most
servers do not have a GUI, and it is best to learn to use the powers of a command
line interface (CLI).
Terminal vs Shell
The terms "terminal" and "shell" are often used interchangeably, but they refer to
different parts of the command-line interface.
The terminal is the interface you use to interact with the shell. The shell is the
command interpreter that processes and executes your commands. You'll learn more
about shells in Part 6 of the handbook.
What is a prompt?
When a shell is used interactively, it displays a $ when it is waiting for a command
from the user. This is called the shell prompt.
[username@host ~]$
If the shell is running as root (you'll learn more about the root user later on), the
prompt is changed to #.
Command Structure
Command Structure
When you are in the terminal, you can speed up your tasks by using shortcuts.
Here are some of the most common terminal shortcuts:
Operation Shortcut
You can get the username you are logged in with by using
the whoami command. This command is useful when you are switching
between different users and want to confirm the current user.
Just after the $ sign, type whoami and press enter.
whoami
This is the output I got.
zaira@zaira-ThinkPad:~$ whoami
zaira
uname -a
# output
Linux zaira 6.5.0-21-generic #21~22.04.1-Ubuntu SMP
PREEMPT_DYNAMIC Fri Feb 9 13:32:52 UTC 2 x86_64 x86_64 x86_64
GNU/Linux
In the output above,
Linux: Indicates the operating system.
zaira: Represents the hostname of the machine.
6.5.0-21-generic #21~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC
Fri Feb 9 13:32:52 UTC 2: Provides information about the
kernel version, build date, and some additional details.
x86_64 x86_64 x86_64: Indicates the architecture of the system.
GNU/Linux: Represents the operating system type.
Find details of the CPU architecture using the
lscpu Command
The lscpu command in Linux is used to display information about the CPU
architecture. When you run lscpu in the terminal, it provides details such as:
The architecture of the CPU (for example, x86_64)
CPU op-mode(s) (for example, 32-bit, 64-bit)
Byte Order (for example, Little Endian)
CPU(s) (number of CPUs), and so on
Let's try it out:
lscpu
# output
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 48 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 12
On-line CPU(s) list: 0-11
Vendor ID: AuthenticAMD
Model name: AMD Ryzen 5 5500U with Radeon Graphics
Thread(s) per core: 2
Core(s) per socket: 6
Socket(s): 1
Checking your file system
tree -d -L 1
You can check your file system using the tree -d -L 1 command. You can
modify the -L flag to change the depth of the tree.
tree -d -L 1
# output
.
├── bin -> usr/bin
├── boot
├── cdrom
├── data
├── dev
├── etc
├── home
├── lib -> usr/lib
├── lib32 -> usr/lib32
├── lib64 -> usr/lib64
├── libx32 -> usr/libx32
├── lost+found
25 directories
This list is not exhaustive and different distributions and systems may be
configured differently.
Navigating the Linux File-system
pwd
# output
/home/zaira/scripts/python/free-mem.py
Changing directories using the
cd command:
The command to change directories is cd and it stands for "change directory".
You can use the cd command to navigate to a different directory.
You can use a relative path or an absolute path.
For example, if you want to navigate the below file structure (following the red lines):
and you are standing at "home", the command would be like this:
cd home/bob/documents/work/project
Some other commonly used cd shortcuts are:
Command Description
When working with files and directories, you might want to copy,
move, remove, and create new files and directories.
3 directories, 0 files
Creating new files using the touch command
The touch command creates an empty file. You can use it like this:
# creates empty file "file.txt" in the current folder
touch file.txt
The file names can be chained together if you want to create multiple files in a single
command.
# creates empty files "file1.txt", "file2.txt", and "file3.txt" in the current folder
Command Description
The mv command is used to move files and folders from one directory to
the other.
Syntax to move files:mv source_file destination_directory
Example: Move a file named file1.txt to a directory named backup:
mv file1.txt backup/
To move a directory and its contents:
mv dir1/ backup/
Renaming files and folders in Linux is also done with the mv command.
Syntax to rename files:mv old_name new_name
Example: Rename a file from file1.txt to file2.txt:
mv file1.txt file2.txt
Rename a directory from dir1 to dir2:
mv dir1 dir2
Locating Files and Folders Using
the find Command
The find command lets you efficiently search for files, folders, and
character and block devices.
Below is the basic syntax of the find command:
find /path/ -type f -name file-to-search
Where,
/path is the path where the file is expected to be found. This is the
starting point for searching files. The path can also be/or . which
represents the root and current directory, respectively.
• represents the file descriptors. They can be any of the below:
f – Regular file such as text files, images, and hidden files.
d – Directory. These are the folders under consideration.
l – Symbolic link. Symbolic links point to files and are similar to
shortcuts.
c – Character devices. Files that are used to access character devices
are called character device files. Drivers communicate with character
devices by sending and receiving single characters (bytes, octets).
Examples include keyboards, sound cards, and the mouse.
b – Block devices. Files that are used to access block devices are
called block device files. Drivers communicate with block devices by
sending and receiving entire blocks of data. Examples include USB and
CD-ROM
How to search files by name or extension
Suppose we need to find files that contain "style" in their name. We'll use this
command:
find . -type f -name "style*"
#output
./style.css
./styles.css
Now let's say we want to find files with a particular extension like .html. We'll modify
the command like this:
find . -type f -name "*.html"
# output
./services.html
./blob.html
./index.html