Skip to content

Linux Command Line Basics

Introduction

The Linux Command Line (Terminal, Shell) is a powerful tool that allows users to interact with the system using text-based commands. It provides greater control, automation capabilities, and flexibility compared to graphical user interfaces (GUIs). This tutorial covers essential Linux commands to help you efficiently navigate and manage the command line.


Case Sensitivity Matters!

Linux differentiates between uppercase and lowercase letters. Commands such as PWD instead of pwd will cause errors or unexpected behavior. Always ensure correct capitalization.

Display the Current Directory

To check your current working directory, use:

pwd # (1)
  1. Displays the current working directory.
Example Output
/home/user

Changing Directories

  • Navigate to a specific directory:
cd /path/to/folder # (1)
  1. Moves to the specified directory.

  2. Move up one level:

cd .. # (1)
  1. Moves up one directory level.

  2. Return to the home directory:

cd ~ # (1)
  1. Returns to the user’s home directory.

Absolute vs. Relative Paths!

  • Relative Paths: cd Documents changes the directory relative to the current location.
  • Absolute Paths: cd /etc always goes to the same location, regardless of your current directory.

Managing Files and Folders

Creating Files

  • Create an empty file:
touch file.txt # (1)
  1. Creates a new empty file.

  2. Create a file with content:

echo "Hello World" > file.txt # (1)
  1. Creates a new file and writes "Hello World" into it.

Viewing Files

  • Display the contents of a file:
cat file.txt # (1)
  1. Displays the content of the specified file.

Creating Folders

mkdir MyFolder # (1)
  1. Creates a new folder with the specified name.

  2. Create multiple nested directories at once:

mkdir -p Folder1/Subfolder2/Subfolder3 # (1)
  1. Creates a hierarchical directory structure in one command.

Listing Directory Contents

  • Display all files and folders in the current directory:
ls # (1)
  1. Lists the contents of the current directory.

Moving and Renaming Files and Folders

  • Move a file:
mv file.txt MyFolder/ # (1)
  1. Moves the file to the specified folder.

  2. Rename a file:

mv old_file.txt new_file.txt # (1)
  1. Renames the file.

  2. Move a folder and its contents:

mv MyFolder NewFolder # (1)
  1. Moves the entire folder and its contents.

Deleting Files and Folders

  • Delete a file:
rm file.txt # (1)
  1. Permanently deletes the specified file.

  2. Delete an empty folder:

rmdir MyFolder # (1)
  1. Deletes the specified folder if it is empty.

  2. Delete a folder and all its contents:

rm -r MyFolder # (1)
  1. Deletes the folder and all files within it recursively.

Retrieving System Information

  • Display the current IP address:
ip a (or `ip addr show`) # (1)
  1. Shows network and IP information.

  2. Display running processes:

ps aux # (1)
  1. Displays a list of currently running processes.

  2. Terminate a process:

killall nano # (1)
  1. Forcefully ends all nano processes.

Download Cheat Sheet


Conclusion

The Linux Command Line is a useful tool for system management, automation, and file operations. With these fundamental commands, you can work more efficiently. A great way to develop CLI skills is by experimenting in a test environment.