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:
-
List files in current directory
ls
👉 Shows names of files and folders in the current location.
-
List with details (
-lfor 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.txtdrwxr-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).
-
Combine options (
-la)
ls -la
👉 Shows all files including hidden ones, in detailed format.
-
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:
-
Go to a specific directory
cd /etc/sysconfig/network-scripts
👉 Moves you to the Documents folder.
-
Go to your home directory
cd
or
cd ~
👉 Takes you to your user's home directory, like /home/ashwin.
-
Go back to the previous directory
cd ..
👉 Takes you to the previous directory you were in.
-
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 practicecd practicemkdir -plinux/directory1ls -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 testfoldertouch testfolder/file1.txtrm -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 Practice8. 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:
whoTry 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:
-
Normal Mode (default when you open
vi)
– For navigation and commands -
Insert Mode
– For typing text
– Pressito enter Insert mode -
Command Mode
– For saving, quitting, searching
– Press:to enter command mode
🔹 Basic Commands:
|
Action |
Key |
|---|---|
|
Enter Insert Mode |
|
|
Save File (Command Mode) |
|
|
Quit |
|
|
Save & Quit |
|
|
Quit without saving |
|
|
Delete a line |
|
|
Copy a line |
|
|
Paste |
|
|
Move up/down |
|
|
Move left/right |
|
🔹 Example Flow:
-
Open a file:
vi hello.txt
-
Press
i→ Enter Insert Mode -
Type:
Hello, this is my first vi file!
-
Press
Esc→ Go back to Normal Mode -
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 12 This is line 2
🔹 Summary Table:
|
Task |
Command |
|---|---|
|
View file |
|
|
Create file |
|
|
Append file |
|
|
Combine files |
|
|
Line numbers |
|
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:
$ lsbin 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 ls2 pwd3 cd /home/ashwin4 mkdir test5 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:
-
Create an empty file.
-
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 |
|---|---|
|
|
Create a new empty file |
|
|
Create multiple files |
|
|
Update modified time of existing file |
|
|
Set custom timestamp |
|
|
Update time only if file exists |
Try with Linux Practice