Your Resource for Linux Learning

Essential Linux Commands

A solid understanding of command-line tools is essential for working effectively in Linux. This guide covers the most important commands that every Linux user should know, organized by category.

File and Directory Operations

ls List Directory Contents

The ls command lists files and directories in the current directory or a specified location.

Basic usage:
$ ls
List all files (including hidden):
$ ls -a
List with details:
$ ls -l
List with human-readable file sizes:
$ ls -lh

Tips:

  • Use ls -la to combine detailed listing with showing hidden files
  • Use ls -lt to sort files by time (newest first)
  • Use ls -lS to sort files by size (largest first)

cd Change Directory

The cd command changes your current working directory.

Go to home directory:
$ cd
Go to specific directory:
$ cd /etc/nginx
Go up one directory:
$ cd ..
Go to previous directory:
$ cd -

Tips:

  • Use tab completion to avoid typing full path names
  • cd ~ is a shortcut to your home directory
  • cd ~/Documents takes you to the Documents folder in your home directory

pwd Print Working Directory

The pwd command displays the full path of your current directory.

$ pwd

mkdir Make Directory

The mkdir command creates a new directory.

Create a directory:
$ mkdir new_directory
Create nested directories (including parents):
$ mkdir -p parent_dir/child_dir/grandchild_dir

cp Copy Files and Directories

The cp command copies files and directories.

Copy a file:
$ cp file.txt destination/
Copy and rename a file:
$ cp file.txt destination/new_name.txt
Copy a directory recursively:
$ cp -r source_directory/ destination/

Tips:

  • Use cp -i for interactive mode (prompts before overwriting)
  • Use cp -v for verbose output to see what's being copied

mv Move/Rename Files and Directories

The mv command moves files and directories or renames them.

Rename a file:
$ mv oldname.txt newname.txt
Move a file to another directory:
$ mv file.txt /path/to/destination/
Move multiple files:
$ mv file1.txt file2.txt destination/

Tips:

  • Use mv -i for interactive mode to prevent accidental overwrites
  • The mv command works the same way for directories as it does for files

rm Remove Files and Directories

The rm command deletes files and directories. Use with caution!

Remove a file:
$ rm file.txt
Remove multiple files:
$ rm file1.txt file2.txt
Remove a directory and its contents:
$ rm -r directory/

Warning:

  • Files deleted with rm cannot be easily recovered
  • Use rm -i for interactive mode (asks for confirmation)
  • Be extremely careful with rm -rf as it removes directories recursively without prompting

touch Create Empty Files

The touch command creates empty files or updates timestamps of existing files.

Create an empty file:
$ touch newfile.txt
Create multiple files:
$ touch file1.txt file2.txt file3.txt

File Content Operations

cat Concatenate and Display Files

The cat command displays the contents of files or combines multiple files.

Display file contents:
$ cat file.txt
Display contents of multiple files:
$ cat file1.txt file2.txt
Create a new file from multiple files:
$ cat file1.txt file2.txt > combined.txt

less View Files with Pagination

The less command allows you to view file contents page by page, with searching capabilities.

View a file:
$ less file.txt

Navigation in less:

  • Space or Page Down: Move forward one page
  • b or Page Up: Move back one page
  • g: Go to beginning of file
  • G: Go to end of file
  • /pattern: Search forward for "pattern"
  • q: Quit

head Display Beginning of Files

The head command displays the first part of files.

Show first 10 lines (default):
$ head file.txt
Show first N lines:
$ head -n 5 file.txt

tail Display End of Files

The tail command displays the last part of files.

Show last 10 lines (default):
$ tail file.txt
Show last N lines:
$ tail -n 20 file.txt
Monitor file (follow mode):
$ tail -f /var/log/syslog

Tips:

  • The -f option is extremely useful for watching log files in real-time

grep Search Text

The grep command searches for patterns in text.

Search for a pattern in a file:
$ grep "search term" file.txt
Search case-insensitively:
$ grep -i "search term" file.txt
Search recursively in directories:
$ grep -r "search term" /path/to/directory
Show line numbers with matches:
$ grep -n "search term" file.txt

Tips:

  • Combine with pipes for powerful text processing: cat file.txt | grep "search"
  • Use grep -v to invert the match (show lines that don't match)

System Information

df Display Disk Space Usage

The df command shows disk space usage.

Show all filesystems:
$ df
Show in human-readable format:
$ df -h

du Display Directory Space Usage

The du command shows directory space usage.

Show size of current directory:
$ du -sh
Show sizes of subdirectories:
$ du -sh */

top Display System Processes

The top command provides a dynamic real-time view of running system processes.

$ top

Keyboard shortcuts in top:

  • k: Kill a process (prompts for PID)
  • r: Renice a process (change priority)
  • q: Quit
  • M: Sort by memory usage
  • P: Sort by CPU usage

ps List Processes

The ps command shows information about active processes.

Show your processes:
$ ps
Show all processes with full details:
$ ps aux
Show process tree:
$ ps axjf

Command Combinations

Linux commands become even more powerful when combined. Here are some useful combinations:

Command Description
find . -name "*.txt" | xargs grep "pattern" Find all text files containing "pattern"
ps aux | grep firefox Find Firefox processes
ls -la | grep "^d" List only directories
cat /var/log/syslog | tail -n 50 Show last 50 lines of syslog
du -sh */ | sort -hr Sort directories by size (largest first)

Next Steps

Now that you've learned the essential Linux commands, you can: