Basic Linux Commands

BuddhiK
15 min readJan 14, 2025

What is Linux ?

Linux is an open-source operating system that serves as the backbone for many different devices, from personal computers and servers to smartphones and embedded systems. It functions similarly to other operating systems like Windows and macOS, providing the essential interface between hardware and software so that applications can run efficiently.

Linux Architecture

image 01

— — — — — — — — — — — — — — — — — — — — — — — — -
| User Applications |
— — — — — — — — — — — — — — — — — — — — — — — — -
| System Libraries |
— — — — — — — — — — — — — — — — — — — — — — — — -
| Shell |
— — — — — — — — — — — — — — — — — — — — — — — — -
| Linux Kernel |
| — Process Management — File Systems |
| — Memory Management — Networking |
| — Device Drivers — System Calls |
— — — — — — — — — — — — — — — — — — — — — — — — -
| Hardware |
— — — — — — — — — — — — — — — — — — — — — — — — -

Hardware Layer

This is the physical layer, which includes:

  • CPU (Central Processing Unit)
  • Memory (RAM)
  • Storage devices
  • Peripherals (keyboards, monitors, printers, etc.)

The Linux operating system interacts with these hardware components via the kernel.

Kernel Layer

The kernel is the core of the Linux operating system. It acts as a bridge between hardware and software. The kernel has several responsibilities:

  • Hardware Management: Communicates with and controls hardware devices.
  • Process Management: Manages processes by allocating CPU time and memory.
  • Memory Management: Controls the use of RAM and manages virtual memory.
  • Device Drivers: Allows the kernel to interact with specific hardware components.
  • File System Management: Manages how data is stored and retrieved.
  • Networking: Handles data exchange between devices and networks.

The kernel operates in kernel space, which is isolated from the user and application processes for security and stability.

Shell Layer

The shell is an interface between the user and the kernel. It allows users to execute commands and interact with the operating system. There are two types of shells:

  • Command-Line Shells: Examples include Bash, Zsh, and Fish, where users input text-based commands.
  • Graphical Shells: Used in desktop environments like GNOME or KDE, offering a graphical user interface (GUI).

User Space

This is where applications and user processes run. It is divided into two main categories:

1. System Libraries

  • Provide reusable functions and tools that applications can use to perform specific tasks (e.g., accessing files, communicating with the network).
  • The most common library in Linux is the GNU C Library (glibc).

2. User Applications

  • Programs or software that users interact with, such as web browsers, text editors, and media players.

Applications communicate with the kernel indirectly via system calls provided by system libraries.

General Knowlege Before We starts

What is the Command Line here ?

When we talk about the command line, we are actually referring to the Shell. The shell is a program that receives commands typed on the keyboard and passes them to the operating system (OS) for execution. One popular shell program is Bash, which stands for “Bourne Again Shell.” It is an enhanced version of the original Unix Shell program, which was written by Steve Bourne.

What Terminal Emulators ?

In Linux, terminal emulators are software applications that provide a command-line interface (CLI) for interacting with the operating system. They emulate the functionalities of traditional hardware terminals and allow users to execute shell commands, run scripts, and manage the system. Terminal emulators are essential for system administration, development, and various technical tasks in Linux.

Common terminal emulators in Linux : GNOME, Konsole, xterm,xterm

First Steps

Launch the terminal emulator ! Once you open it up you will see some thing like this.

[user1@linuxbox ~ ]$

This is what we call a shell prompt. Now the terminal imulator is ready for take input.

If it is prompt with # sign rather than $ that means you have higher “supper user” privilages.

[user1@linuxbox~ ]#

In here

User1

  • Represents the username of the person logged into the system.
  • In this case, User1 is the current user.

@

  • Separates the username (User1) from the hostname (linuxbox).
  • It indicates the format: username@hostname.

linuxbox

  • Refers to the hostname or name of the computer.
  • In this case, linuxbox is the name given to the system by the user or administrator.

~

  • Represents the current working directory.
  • The ~ symbol is shorthand for the user's home directory (e.g., /home/me).
  • If the user navigates to a different directory, this part of the prompt will display the full path (e.g., /usr/bin).

$

  • Indicates the type of user currently operating in the shell:
  • A dollar sign ($) means the user is a regular user with limited permissions.
  • If the user were a root user (administrator), the prompt would display a hash symbol (#) instead.

Navigation

Understand the filesystem tree

The Linux filesystem is organized as a hierarchical tree structure, with the root directory (/) at the base. All files and directories stem from this root directory.

The first directory in the filesystem is called the root directory.The root directory contains files and subdirectories and so on.

note : Unlike the windows ,which has a seperate filesystem tree for each storage device ,Unix systems like Linux always have a single filesystem tree,regarrdless of how many drives or storage divices are attached to the computer.

File Hyrachy in windows

image 02

File Sytem in Linux

image 03
image 04

Current working directory — pwd

pwd refers to the directory in the file system where the user is currently operating. It is the location where commands will be executed, files will be searched, or new files and directories will be created by default.

image 05

Listing the contents in the directory -ls

ls is used to list the contents of a directory. When you use the ls command without any options, it displays a list of files and directories present in the current working directory.Using cd helps you move around the file system quickly and efficiently, making it easier to find and manage files and directories.

image 06

Change the current working directory — cd

cd is used to navigate through the file system by changing the current working directory to a specified path.

  • Absoulute path : A file or directory from the root directory (/). It starts from the root directory and includes all the directories in between.
image 07
  • Relative path : A file or directory location relative to the current working directory. It does not start from the root but uses the current directory as the reference point.
image 08

What are those dots really doing ? ..

cd .. is used to move up one level in the directory hierarchy, taking you to the parent directory of the current working directory.

In here

  • cd — stands for Change Directory.
  • .. refers to the parent directory, which is one level above the current directory.

Example :

image 09

Most commonly used commands you should know

1. Create folder — mkdir

mkdir <space> <Folder Name>

Checking available files and folders in current location

image 10

Creating the folder named “Prabha” and then checking the location just to verify whether the folder has been created.

image 11

2. Create text file

2.1 touch command :

Will create empty text file.The touch command is used only to create an empty file; it doesn't open the file for editing. After running the touch filename.txt command, you don't need to exit because it does not leave you in an interactive editing mode. The command finishes immediately after execution.

touch <textfilename.txt>

  • To edit the textfile you have created you can use cat, nano or vim commands to edit the content of the text file and you can save and exit via givving relevant commands accordingly.

example: edit the created text file using touch command I have used nano<textfilename.txt>. Then I have edit my text file.So to save I pressed Ctrl+O to save and Ctrl+X to close and exit the file.

image 12
image 13

2.2 echo command

The echo command in Linux is used to display a line of text or a string in the terminal.

  • 2.2.1 Display a Message or Text — echo “text you want to display”
  • Display Messge or a Text

echo “<Text you needed to display in the terminal>”

image 14

2.2.2 Write Text to a File — echo “text you want to save to a text file” > textfilename.txt

  • Write Text to a File — echo “This is line 1” > echofile.txt
image 15
image 16

2.2.3 Append to a file — echo “text you want to append to a text file” >> textfilename.txt

  • Append to a File — echo “This is line 2” >> echofile.txt
image 17
image 18

Echo command is far more than that and will explain the usage in upcoming topic.

2.3. cat command

Following cat command will allow you to write text to the givven text file.

cat > filename.txt

Then you can start writing text in to the file.

image 19

After you finished you can simply press Ctrl + D to exit.

View and ammend the text file as below.

image 20

3. Copy files or directories

3.1 Copy a file to another directory:

cp <copeing file name> <path where you need to locate the file copy>

cp document.txt /home/user/documents/

image 21

3.2 Copy a directory and its contents:

cp -r <From Directory> <to Directory>

image 22

3.3 Copy and rename a file:

cp <file name> <path/<new name for rename>

Extra:

Important Options

  1. -i: Interactive mode; prompts before overwriting.
  2. -r: Recursive copy for directories.
  3. -a: Archive mode; preserves file attributes and directory structure.
  4. -u: Copy only when the source file is newer than the destination file or if the file doesn’t exist at the destination.
  5. -v: Verbose mode; shows detailed output during the copy process.

4. Move or rename files or directories

4.1 Move a File to Another Directory:

mv <file1.txt>< path to destination>

image 23

4.2 Move Multiple Files :

mv <file1.txt><file2.txt> <path to destination>

image 24

4.3: Options for moving

Prompt Before Overwriting: Use the -i (interactive) flag to prompt before overwriting:

mv -i file1.txt /path/to/destination/

image 25

Force Overwrite Without Prompt: Use the -f (force) flag to overwrite without prompting:

mv -f file1.txt /path/to/destination/

image 26

Verbose Output: Use the -v (verbose) flag to display detailed output:

mv -v file1.txt /path/to/destination/

image 27

5. Remove files or directories

rm <filename>

image 28

Before you remove a file or directory just good to know below options.

  • Forceful deletion: Be cautious when using rm -f or rm -rf as it bypasses prompts and can delete important files or directories.
  • Recursive deletion: The -r flag is required to remove directories and their contents.
  • Safety tip: Always double-check the command before pressing Enter, especially when using wildcards like *.

Examples:

image 29
image 30

6. Change file permissions

Understanding the file permssion

rwxr-xr —

Permissions are represented as:

  • r: Read
  • w: Write
  • x: Execute
  • - : No permission
  • First character: File type (- for files, d for directories).
  • Next three characters: Permissions for the owner (user).
  • Next three characters: Permissions for the group.
  • Last three characters: Permissions for others (everyone else).

6.1 View permision

ls -l <file name>

image 31

6.2 Add Permission — chmod u+x <filename>

image 32

6.3 Add Excactlly Permission — chmod o=r <filename>

image 33

6.4 Remove Permission -chmod g-w <filename>

image 34

6.5 Give All Permissions to Everyone: chmod a+rwx <filename>

image 35

6.6 Changing Permissions Recursively

chmod -R 755 <path to directory>

6.7 Using Numeric Mode

In numeric mode, permissions are represented as a three-digit number (octal):

  • Read = 4
  • Write = 2
  • Execute = 1

Add these values to calculate permissions:

  • rwx = 4 + 2 + 1 = 7
  • rw- = 4 + 2 + 0 = 6
  • r-- = 4 + 0 + 0 = 4

Format: chmod [owner][group][others] <filename>

example :

a) Set permission to everyone -chmod 777 <filename>

b) Set Owner to Read/Write, Group to Read, Others to No Access —

chmod 640 <filename>

7. Change file ownership — chown

7.1 Verfying ownership

ls -l <filename>

image 36

7.2 Change the Owner of a File

chown newuser file.txt

7.3 Change Both Owner and Group:

chown newuser:newgroup file.txt

7.4 Change Only the Group:

Use a colon without specifying a user:

To change only group ownership without using chown, you can use the chgrp command:chgrp newgroup file.txt

chown :newgroup file.txt

7.5 Options for chown

  • Change Ownership Verbosely: Use the -v option to display changes:

chown -v alice file.txt

  • Preserve Permissions While Changing Ownership: The -c option reports changes without verbose output for every file:

chown -c alice:developers file.txt

  • Suppress Errors: Use the --silent or -f option to suppress error messages:

chown -f alice file.txt

7.6 Permissions Required

  • You must have root privileges or be the file owner to use chown. Use sudo if necessary

sudo chown alice file.txt

Practical examples

Example 01: Set Owner to bob and Group to staff -

chown bob:staff report.txt

Example 02: Set Owner for All Files in a Directory —

chown -R bob /home/bob/documents

Example 03:Set Group for a File

chown :engineers script.sh

8. Add new Users and Remove Users

sudo adduser <newuser>

sudo deluser <username>

image 37

8. Display disk space usage

df [options] [file_or_directory]

8.1 Display Disk Usage for All Filesystems -df

image 38

8.2 Display in Human-Readable Format: Use the -h option to show sizes in human-readable units (KB, MB, GB):

image 39

8.3 Check Disk Usage for a Specific Directory:

image 40

8.4 Display Information in Inodes: Use the -i option to show inode usage instead of disk space:

image 41

8.5 Include Filesystems of Specific Types: Use the -t option to filter by filesystem type (e.g., ext4):

image 42

8.6 Exclude Filesystems of Specific Types: Use the -x option to exclude certain filesystem types (e.g., tmpfs):

image 43

8.7 Show Filesystem Type: df -T

image 44

8.8 Command for Specific File/Folder Usage -du

8.8.1 Basic disk usage

image 45

8.8.2 Basic disk usage in Human readable format

image 46

8.8.3 Summerise total disk usage

image 47

9. Display information about active processes

9.1 ps Command

9.1.1 List all processes — ps -e

image 48

9.1.2 Detailed process information — ps -ef

image 49

9.2 top Command

image 50

9.3 htop Command

image 51

10. Terminate a process

10.1 Using kill Command

steps :

  1. Find the PID of the Process: Use ps, top, or pgrep to identify the process ID (PID)
  2. send the kill command
image 52
image 53

10.1.1 Kill by User

image 54

10.1.2 Kill by Port

image 55
image 56

10.2 Using pkill Command

image 57

10.3 Using killall Command

image 58

Example 01 : Kill a process by name- pkill -9 apache2

Example 02 : Force kill a process using its PID — kill -9 9876

Example 03 : Kill all instances of a program- killall firefox

Example 04 :Kill processes owned by a specific user — pkill -u john

11. Display network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

image 59

11.1 The netstat command provides comprehensive network-related information.net stat command

Step 01: Install netstat if not available with sudo install netstat

Step 02: netstat command

  • Display Active Network Connections — netstat
  • Show Routing Tables- netstat -r
  • Display Interface Statistics- netstat -i
  • Show Masquerade Connections- netstat -M
  • Display Multicast Group Memberships — netstat -g

11.2 Using ss Command

  • Display Active Network Connections — ss
  • Show Listening Ports -ss -l
  • View All Sockets (TCP and UDP)- ss -tua
  • Display Process Information- ss -p

11.3 Using ip Command

  • Display Network Interfaces — ip link show
  • Show IP Addresses- ip addr show
  • Display Routing Table- ip route show
  • View Multicast Addresses- ip maddr show

11.4 Using the ifconfig Command

  • provides information about network interfaces.
  • If ifconfig is unavailable, install net-tools as shown above.

11.5 nmcli Command

  1. Display Network Status — nmcli general status
  2. Show Active Connections- nmcli connection show

12. Test network connectivity

  • ping — ping <hostname_or_IP>
  • traceroute — traceroute <hostname_or_IP>
  • curl- curl <URL>
  • wget- wget <URL>
  • telnet- telnet <hostname_or_IP> <port>
  • naslookup- nslookup <hostname>
  • ss- ss -tua

13. Securely connect to another machine over the network

First you have to ensure whether ssh installed in both local and remote machine.

image 60

Real-World Examples:

  1. Access a remote server: ssh admin@server.example.com to log into the server securely.
  2. Copy files to a remote machine: scp report.pdf

http://user@192.168.1.5/home/user/docs/

  1. Create an SSH key for password-less login: Use ssh-keygen to generate a key, then ssh-copy-id to add it to the remote server.
  2. Transfer large directories: Use rsync for efficient and secure directory synchronization.

14. Manage system services

image 61

15. View system logs

image 62

16. Monitor system processes

image 63

Resources : “The Linux Command Line” by Williams E.Shotts Jr, ChatGPT

Special thanks to my VU lecturer, Jimmy Mathew, for providing insightful resources and guidence, and to HelloMonday career coach, Katie Peterson, for her encouragement throughout the Digital Jobs Program.

--

--

No responses yet