0% found this document useful (0 votes)
20 views

basic-commands1

basic-commands1

Uploaded by

abdikanipd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

basic-commands1

basic-commands1

Uploaded by

abdikanipd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Introduction to Bash Shell and System Commands

The Linux command line is provided by a program


called the shell. Over the years, the shell program
has evolved to cater to various options

Different users can be configured to use different shells. But, most


users prefer to stick with the current default shell. The default
shell for many Linux distros is the GNU Bourne-Again Shell ( bash).
Bash is succeeded by the Bourne shell (sh).
To find out your current shell, open your terminal and enter the
following command:

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

A command is a program that performs a specific operation.


Once you have access to the shell, you can enter any
command after the $ sign and see the output on the terminal.
Generally, Linux commands follow this syntax:

command [options] [arguments]


Here is the breakdown of the above syntax:
 command: This is the name of the command you want to
execute. ls (list), cp (copy), and rm (remove) are common
Linux commands.
Command Structure

 [options]: Options, or flags, often preceded by a


hyphen (-) or double hyphen (--), modify the behavior
of the command.
 They can change how the command operates. For
example,
 ls -a uses the -a option to display hidden files in the
current directory.
 [arguments]: Arguments are the inputs for the
commands that require one. These could be filenames,
user names, or other data that the command will act
upon
Command Structure

Command Structure

Manual pages are a great and quick way to access the


documentation. I highly recommend going through man pages for
the commands that you use the most.
Bash Commands and Keyboard Shortcuts

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

Look for the previous command Up Arrow

Jump to the beginning of the previous word Ctrl+LeftArrow

Clear characters from the cursor to the end of the


command line Ctrl+K

Complete commands, file names, and options Pressing Tab

Jumps to the beginning of the command line Ctrl+A

Displays the list of previous commands history


Identifying Yourself:
Commands uname and whoami

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

Understanding Your Linux System


Discovering Your OS and Specs
Print system information using the uname Command
You can get detailed system information from the uname command.
When you provide the -a option, it prints all the system information.
Identifying Yourself:
Commands uname and whoami

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

Absolute path vs relative path


 The absolute path is the full path from the root directory to the file or directory.
 It always starts with a /. For example, /home/john/documents.
 The relative path, on the other hand, is the path from the current directory to the
destination file or directory. It does not start with a /. For
example, documents/work/project.
 Locating your current directory using the pwd command
 It is easy to lose your way in the Linux file system, especially if you are new to the
command line. You can locate your current directory using the pwd command.
 Here is an example:

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

cd .. Go back one directory

cd ../.. Go back two directories

cd or cd ~ Go to the home directory

cd - Go to the previous path


Managing Files and Directories

When working with files and directories, you might want to copy,
move, remove, and create new files and directories.

You can differentiate between a file and folder by looking at the


first letter in the output of ls -l. A'-' represents a file and
a 'd' represents a folder.
Creating new directories using the mkdir command

You can create an empty directory using the mkdir command.


# creates an empty directory named "foo" in the current folder
mkdir foo
You can also create directories recursively using the -p option.
mkdir -p tools/index/helper-scripts
# output of tree
.
└── tools
└── index
└── helper-scripts

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

touch file1.txt file2.txt file3.txt


Removing files and directories using
the rm and rmdir command
You can use the rm command to remove both files and non-empty directories.
Note that you should use the -f flag with caution
as you won't be asked before deleting a file.
Also, be careful when running rm commands in the root folder as it might result in deleting important system files.

Command Description

rm file.txt Removes the file file.txt

rm -r directory Removes the directory directory and its contents

rm -f file.txt Removes the file file.txt without prompting for confirmation

rmdir directory Removes an empty directory


Copying files using
the cp command
 To copy files in Linux, use the cp command.
 Syntax to copy files:cp source_file destination_of_file
 This command copies a file named file1.txt to a new file
location /home/adam/logs.
 cp file1.txt /home/adam/logs
 The cp command also creates a copy of one file with the provided
name.
 This command copies a file named file1.txt to another file
named file2.txt in the same folder.
 cp file1.txt file2.txt
Moving and renaming files and
folders
using the mv command

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

How to search hidden files


A dot at the beginning of the filename represents hidden files. They are normally
hidden but can be viewed with ls -a in the current directory.
We can modify the find command as shown below to search for hidden files:
find . -type f -name ".*"

You might also like