Linux commands are essential for navigating and managing files in a Linux-based system. This tutorial covers eight fundamental Linux commands:
ls(List directory contents)cd(Change directory)pwd(Print working directory)cp(Copy files and directories)mv(Move or rename files and directories)rm(Remove files and directories)mkdir(Create directories)rmdir(Remove empty directories)
1. ls – List Directory Contents
The ls command is used to list files and directories in the current or specified directory.
Basic Usage
lsThis command lists all files and directories in the current location.
Common Options
| Option | Description |
|---|---|
-l | Long listing format (shows permissions, owner, size, etc.) |
-a | Show hidden files (files starting with .) |
-h | Human-readable format (file sizes in KB, MB, GB) |
-t | Sort by modification time (newest first) |
-r | Reverse order |
Examples
- List files with details
ls -l - List hidden files
ls -a - List files in human-readable format
ls -lh
2. cd – Change Directory
The cd command is used to navigate between directories.
Basic Usage
cd directory_nameMoves into directory_name.
Common Usage
| Command | Description |
|---|---|
cd /path/to/directory | Navigate to a specific directory |
cd .. | Move up one level (to the parent directory) |
cd ~ | Go to the home directory |
cd / | Go to the root directory |
cd - | Switch to the previous directory |
Examples
- Move into a directory
cd Documents - Move up one level
cd .. - Go to home directory
cd ~
3. pwd – Print Working Directory
The pwd command prints the full path of the current directory.
Usage
pwdExample
$ pwd
/home/user/DocumentsThis command outputs the absolute path of the directory you are currently in.
4. cp – Copy Files and Directories
The cp command is used to copy files and directories.
Basic Usage
cp source destinationCopies source to destination.
Common Options
| Option | Description |
|---|---|
-r | Recursively copy directories |
-i | Prompt before overwriting files |
-u | Copy only if the destination file is older |
-v | Show detailed output of copying process |
Examples
- Copy a file
cp file1.txt backup.txt - Copy a directory
cp -r my_folder backup_folder - Copy a file with a prompt before overwriting
cp -i file.txt /backup/
5. mv – Move or Rename Files and Directories
The mv command is used to move or rename files and directories.
Basic Usage
mv source destinationMoves source to destination.
Common Options
| Option | Description |
|---|---|
-i | Prompt before overwriting files |
-u | Move only if the destination file is older |
-v | Show detailed output of moving process |
Examples
- Rename a file
mv old_name.txt new_name.txt - Move a file to another directory
mv file.txt /home/user/Documents/ - Move a directory
mv my_folder /home/user/Documents/
6. rm – Remove Files and Directories
The rm command is used to delete files and directories.
Basic Usage
rm filenameDeletes filename.
Common Options
| Option | Description |
|---|---|
-r | Recursively delete directories |
-i | Prompt before deletion |
-f | Force delete (no prompt) |
Examples
- Remove a file
rm file.txt - Remove a directory and its contents
rm -r my_folder - Force delete a file
rm -f file.txt
⚠ Warning: rm -rf / can delete your entire system!
7. mkdir – Create Directories
The mkdir command creates new directories.
Basic Usage
mkdir directory_nameCreates directory_name.
Common Options
| Option | Description |
|---|---|
-p | Create parent directories if they don’t exist |
-v | Show detailed output |
Examples
- Create a directory
mkdir new_folder - Create nested directories
mkdir -p parent_folder/child_folder
8. rmdir – Remove Empty Directories
The rmdir command removes empty directories.
Basic Usage
rmdir directory_nameDeletes directory_name only if it is empty.
Example
- Remove an empty directory
rmdir empty_folder - Remove nested empty directories
rmdir -p parent_folder/child_folder
This removes child_folder first, then parent_folder if both are empty.
Summary Table
| Command | Description |
|---|---|
ls | List files and directories |
cd | Change directory |
pwd | Show current directory |
cp | Copy files and directories |
mv | Move or rename files |
rm | Delete files and directories |
mkdir | Create directories |
rmdir | Remove empty directories |
Mastering these commands will help you efficiently navigate and manage files in Linux! 🚀