Essential Linux Commands

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

ls

This command lists all files and directories in the current location.

Common Options

OptionDescription
-lLong listing format (shows permissions, owner, size, etc.)
-aShow hidden files (files starting with .)
-hHuman-readable format (file sizes in KB, MB, GB)
-tSort by modification time (newest first)
-rReverse order

Examples

  1. List files with details ls -l
  2. List hidden files ls -a
  3. 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_name

Moves into directory_name.

Common Usage

CommandDescription
cd /path/to/directoryNavigate 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

  1. Move into a directory cd Documents
  2. Move up one level cd ..
  3. Go to home directory cd ~

3. pwd – Print Working Directory

The pwd command prints the full path of the current directory.

Usage

pwd

Example

$ pwd
/home/user/Documents

This 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 destination

Copies source to destination.

Common Options

OptionDescription
-rRecursively copy directories
-iPrompt before overwriting files
-uCopy only if the destination file is older
-vShow detailed output of copying process

Examples

  1. Copy a file cp file1.txt backup.txt
  2. Copy a directory cp -r my_folder backup_folder
  3. 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 destination

Moves source to destination.

Common Options

OptionDescription
-iPrompt before overwriting files
-uMove only if the destination file is older
-vShow detailed output of moving process

Examples

  1. Rename a file mv old_name.txt new_name.txt
  2. Move a file to another directory mv file.txt /home/user/Documents/
  3. 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 filename

Deletes filename.

Common Options

OptionDescription
-rRecursively delete directories
-iPrompt before deletion
-fForce delete (no prompt)

Examples

  1. Remove a file rm file.txt
  2. Remove a directory and its contents rm -r my_folder
  3. 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_name

Creates directory_name.

Common Options

OptionDescription
-pCreate parent directories if they don’t exist
-vShow detailed output

Examples

  1. Create a directory mkdir new_folder
  2. Create nested directories mkdir -p parent_folder/child_folder

8. rmdir – Remove Empty Directories

The rmdir command removes empty directories.

Basic Usage

rmdir directory_name

Deletes directory_name only if it is empty.

Example

  1. Remove an empty directory rmdir empty_folder
  2. Remove nested empty directories rmdir -p parent_folder/child_folder

This removes child_folder first, then parent_folder if both are empty.


Summary Table

CommandDescription
lsList files and directories
cdChange directory
pwdShow current directory
cpCopy files and directories
mvMove or rename files
rmDelete files and directories
mkdirCreate directories
rmdirRemove empty directories

Mastering these commands will help you efficiently navigate and manage files in Linux! 🚀

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *