Linux Basic Commands

Site: Openpath
Course: Openpath
Book: Linux Basic Commands
Printed by:
Date: Tuesday, 25 November 2025, 1:17 PM

Description

Linux provides a set of powerful and commonly used commands to interact with the system efficiently. The cd command is used to change directories, while ls lists the contents of a directory. To clear the terminal screen, the clear command is used. The mkdir command helps create new folders, and pwd shows the current working directory. The rm command is used to remove files or directories, and touch is used to create empty files. To edit or view text files directly in the terminal, vi is a powerful editor. The / symbol represents the root directory, which is the top level of the Linux filesystem. To manage users, adduser is used to create a new user, and userdel is used to delete an existing one. The history command displays a list of previously executed commands, allowing users to track or reuse them easily. These commands form the foundation for working effectively in a Linux environment.

1. ls

ls

The ls command in Linux is used to list the contents of a directory (files and folders). It helps you see what's inside a folder.


Basic Syntax:

ls [options] [directory]

Common Examples:

  1. List files in current directory

ls

👉 Shows names of files and folders in the current location.


  1. List with details (-l for long format)

ls -l

👉 Shows detailed information like permissions, owner, size, and modified date.

Example output:

-rw-r--r-- 1 user user  2048 Jul 01 12:30 file.txt
drwxr-xr-x 2 user user  4096 Jul 01 12:15 folder

Include hidden files (-a for all)

ls -a

👉 Shows all files, including hidden files (those starting with . like .bashrc).


  1. Combine options (-la)

ls -la

👉 Shows all files including hidden ones, in detailed format.


  1. List files in a specific directory

ls /etc/sysconfig/network-scripts

👉 Lists contents inside the /etc/sysconfig/network-scripts folder.

Try with Linux Practice

2. pwd

pwd

pwd Command in Linux

pwd stands for Print Working Directory.
It shows the
full path of your current location in the filesystem.


🔹 Basic Syntax:

pwd

🔹 Example:

Let’s say you open a terminal and type:

pwd

Output:

/

👉 This means you're currently in the Documents folder, inside the ashwin user's home directory.


🔹 When to Use:

  • To know where you are in the system.

  • Useful while working with nested directories.

Try with Linux Practice

3. cd

cd Command in Linux

cd stands for Change Directory.
It is used to
navigate between folders in the Linux file system.


🔹 Basic Syntax:

cd [directory_path]
cd /etc/sysconfig

🔹 Common Examples:

  1. Go to a specific directory

cd /etc/sysconfig/network-scripts

👉 Moves you to the Documents folder.


  1. Go to your home directory

cd

or

cd ~

👉 Takes you to your user's home directory, like /home/ashwin.


  1. Go back to the previous directory

cd ..

👉 Takes you to the previous directory you were in.


  1. Navigate using relative path

cd /var/log

👉 Moves into /var/log from your current location.


🔹 Check Your Location:

Use pwd to confirm where you are after using cd.

Try with Linux Practice

4. / (Root Directory)

/ (Root Directory) in Linux

In Linux, / is called the root directory.
It is the
top-most directory in the Linux filesystem. All other files and folders are stored under this root.


🔹 Think of it like:

A tree 🌳 — / is the root and everything else (like /home, /etc, /bin, etc.) are branches.

🔹 Example:

Type this in terminal:

cd /
pwd

Output:

/

👉 This means you are now in the root directory.


🔹 Listing contents of /:

ls /

You may see folders like:

bin   boot  dev  etc  home  lib  media  mnt  opt  proc  root  run  sbin  tmp  usr  var

Each of these has a special purpose. For example:

  • /home → User folders

  • /etc → Configuration files

  • /bin → Basic Linux commands

  • /var → Logs and variable data


🔹 Visual Structure:

/
├── bin
├── etc
├── home
│   └── ashwin
├── var
├── usr
└── tmp

Try with Linux Pratice

5. mkdir

mkdir Command in Linux

mkdir stands for "make directory".
It is used to
create a new folder (directory) in the Linux file system.


🔹 Basic Syntax:

mkdir [directory_name]

🔹 Examples:

1. Create a single directory

mkdir myfolder

👉 Creates a folder named myfolder in the current directory.


2. Create multiple directories at once

mkdir folder1 folder2 folder3

👉 Creates three folders in the current location.


3. Create nested directories (with -p option)

mkdir -p projects/linux/scripts

👉 Creates the full path, including parent folders if they don’t exist.


🔹 Verify the creation:

Use ls to list and confirm:

ls

🔹 Example in Practice:

cd ~
mkdir practice
cd practice
mkdir -p linux/directory1
ls -R

👉 This creates a structure like:

practice/
└── linux/
    └── directory1/

Try with Linux Pratice

6. rm

rm stands for remove.
It is used to delete files and directories in Linux.

⚠️ Be careful: Once deleted with rm, files cannot be recovered easily.


🔹 Basic Syntax:

rm [options] [file_or_directory]

🔹 Examples:

1. Remove a single file

rm file.txt

👉 Deletes file.txt from the current directory.


2. Remove multiple files

rm file1.txt file2.txt

👉 Deletes both files at once.


3. Remove an empty directory (use -d)

rm -d empty_folder

👉 Deletes an empty folder.


4. Remove a non-empty directory (use -r or -rf)

rm -r myfolder

👉 Recursively deletes the folder and everything inside it.

Use -f to force delete (no confirmation):

rm -rf myfolder

⚠️ Dangerous! This will delete everything inside without asking.


🔹 Practice Example:

mkdir testfolder
touch testfolder/file1.txt
rm -r testfolder

👉 This creates a folder, adds a file, and then deletes the whole folder.

Try with Linux Practice

7. useradd

useradd Command in Linux

The useradd command is used to create a new user on a Linux system.
It’s a
simple and user-friendly command (more interactive than useradd).


🔹 Basic Syntax:

useradd [username]

🔹 Example:

useradd ashwin

👉 This will start an interactive setup like:

Adding user `ashwin` ...
Adding new group `ashwin` (1001) ...
Adding new user `ashwin` (1001) with group `ashwin` ...
Creating home directory `/home/ashwin` ...
Copying files from `/etc/skel` ...
New password:
Retype new password:
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]

🔹 What this does:

  • Creates a user account ashwin

  • Creates a home directory: /home/ashwin

  • Assigns a default group

  • Sets up a password

  • Adds user info (optional)


🔹 Verify the user was added:

id ashwin

or

cat /etc/passwd | grep ashwin

Try with Linux Practice

8. userdel

userdel Command in Linux

The userdel command is used to delete a user account from the system.

⚠️ Requires root privileges, so use it with sudo.

🔹 Basic Syntax:

userdel [username]

🔹 Example:

userdel ashwin

👉 This command deletes the user account named ashwin.

🔸 Note: By default, this does not delete the user's home directory.

🔹 Delete user and their home directory:

userdel -r ashwin

👉 This removes:

  • The user account ashwin

  • Their home folder: /home/ashwin

  • Their mail spool and personal files


🔹 How to confirm user is deleted:

cat /etc/passwd | grep ashwin

👉 If no output, the user is deleted.


🔹 Warning:

Don’t delete users while they are logged in.
It can cause
system issues. First, check:

who
Try with Linux Practice

9. vi

vi Command in Linux

vi is a text editor in Linux, used to view and edit files directly in the terminal.

It's one of the most powerful and widely available editors on Unix/Linux systems.


🔹 Basic Syntax:

vi filename

🔹 Example:

vi notes.txt

👉 This opens (or creates, if not exists) a file named notes.txt for editing.


🔹 Modes in vi:

  1. Normal Mode (default when you open vi)
    – For navigation and commands

  2. Insert Mode
    – For typing text
    – Press
    i to enter Insert mode

  3. Command Mode
    – For saving, quitting, searching
    – Press
    : to enter command mode


🔹 Basic Commands:

Action

Key

Enter Insert Mode

i

Save File (Command Mode)

:w

Quit vi

:q

Save & Quit

:wq or ZZ

Quit without saving

:q!

Delete a line

dd

Copy a line

yy

Paste

p

Move up/down

k / j

Move left/right

h / l


🔹 Example Flow:

  1. Open a file:

vi hello.txt
  1. Press i → Enter Insert Mode

  2. Type:

Hello, this is my first vi file!
  1. Press Esc → Go back to Normal Mode

  2. Type :wq! → Save and quit

    Try with Linux Practice

10. cat

cat Command in Linux

cat stands for "concatenate", but it’s commonly used to view the contents of a file.


🔹 Basic Syntax:

cat [options] filename

🔹 Most Common Uses & Examples:

1. View contents of a file

cat file.txt

👉 Displays the content of file.txt in the terminal.


2. Create a new file using cat

cat > newfile.txt

Then type your content:

Hello, this is a new file.

Press Ctrl + D to save and exit.


3. Append content to an existing file

cat >> file.txt

Type new content and press Ctrl + D.


4. Combine multiple files into one

cat file1.txt file2.txt > combined.txt

👉 Combines file1.txt and file2.txt into combined.txt.


5. Show line numbers

cat -n file.txt

👉 Displays the file with line numbers:

     1  This is line 1
     2  This is line 2

🔹 Summary Table:

Task

Command

View file

cat file.txt

Create file

cat > file.txt

Append file

cat >> file.txt

Combine files

cat file1 file2 > newfile

Line numbers

cat -n file.txt



Try with Linux Practice

11. clear

clear Command in Linux

The clear command is used to clear the terminal screen.
It doesn’t delete files or commands — it just makes your terminal window
look clean.


🔹 Syntax:

clear

🔹 Example:

Before:

$ ls
bin sbin root sys etc home
$ pwd
/home/ashwin
$ clear

After:
👉 All the previous output is cleared, and you’re left with a fresh terminal prompt.


🔹 Shortcut Alternative:

Instead of typing clear, you can also press:

Ctrl + L

👉 This also clears the screen (works in most terminals).


🔹 Important Note:

  • It does not delete history. You can still scroll up with the mouse or scroll bar to see old commands/output.

  • It’s purely for visual clarity.

Try With Linux Practice

12. history

history Command in Linux

The history command shows a list of all previously run commands in the terminal.
It’s very useful to recall or repeat past commands.


🔹 Syntax:

history

🔹 Example:

history

👉 Output:

  1  ls
  2  pwd
  3  cd /home/ashwin
  4  mkdir test
  5  history

This shows:

  • Line numbers for each command

  • The exact command you typed earlier


🔹 Useful Options & Examples:

1. Run a previous command by number

!3

👉 Runs the 3rd command from the history (in this case: cd /home/ashwin)


2. Search your command history

history | grep mkdir

👉 Finds all mkdir commands you've used before.


3. Repeat the last command

!!

👉 Runs the most recent command again.


4. Clear your command history

history -c

👉 Clears all saved history for the current session.


5. Save history to a file

history > my_history.txt

👉 Saves your full command history into my_history.txt.

Try with Linux Practice

13. touch

touch Command in Linux

The touch command is used to:

  1. Create an empty file.

  2. Update the timestamp of an existing file (access and modification time).


📌 Syntax:

touch filename

🔹 1. Create a New Empty File

touch myfile.txt

🟢 This creates an empty file named myfile.txt in the current directory.


🔹 2. Create Multiple Files at Once

touch file1.txt file2.txt file3.txt

🟢 This creates all three files in one command.


🔹 3. Update Timestamp of Existing File

touch existingfile.txt

🟢 If existingfile.txt already exists, touch will not delete it — it simply updates the last modified time to the current time.


🔹 4. Change Timestamp to Specific Time

touch -t 202507011200 myfile.txt

🟢 This sets the time to 1st July 2025, 12:00 PM.


🔹 5. Don’t Create New File (Only Update if Exists)

touch -c myfile.txt

🟢 If the file doesn’t exist, it does nothing.


Summary Table

Command

Description

touch newfile.txt

Create a new empty file

touch file1 file2 file3

Create multiple files

touch existing.txt

Update modified time of existing file

touch -t YYYYMMDDhhmm file

Set custom timestamp

touch -c file

Update time only if file exists

Try with Linux Practice