Linux Command Line Basics

The terminal is the most powerful tool in your Linux toolkit. Master these essential commands and you will be able to do anything.

Opening the Terminal

On most Linux desktop environments, you can open the terminal with the keyboard shortcut Ctrl + Alt + T. You can also find it in your applications menu under "Terminal", "Console", or "Konsole" depending on your desktop environment.

When the terminal opens, you will see a prompt that typically looks like this:

username@hostname:~$

This tells you your username, the computer name, your current directory (~ means your home folder), and that you are a regular user ($). A # symbol means you are the root (admin) user.

Navigating the File System

The Linux file system starts at the root directory / and branches out into directories. Understanding navigation is the first skill to master.

CommandDescriptionExample
pwdPrint current working directorypwd → /home/user
lsList files and directoriesls -la (detailed list with hidden files)
cdChange directorycd /var/log
cd ..Go up one directory levelcd ..
cd ~Go to your home directorycd ~ or just cd
treeDisplay directory structure as a treetree -L 2 (two levels deep)
# See where you are
$ pwd
/home/user

# List files with details
$ ls -lah
drwxr-xr-x 5 user user 4.0K Apr 11 10:30 Documents
drwxr-xr-x 2 user user 4.0K Apr 10 09:15 Downloads
-rw-r--r-- 1 user user 220 Apr 1 12:00 .bashrc

# Navigate to Documents
$ cd Documents
$ pwd
/home/user/Documents

Working with Files and Directories

CommandDescriptionExample
touchCreate an empty filetouch newfile.txt
mkdirCreate a directorymkdir projects
mkdir -pCreate nested directoriesmkdir -p a/b/c
cpCopy files or directoriescp file.txt backup.txt
mvMove or rename filesmv old.txt new.txt
rmRemove filesrm unwanted.txt
rm -rRemove directories recursivelyrm -r old-project
Tip: Use rm with caution as there is no trash bin in the terminal. Consider using rm -i for interactive confirmation before each deletion.

Viewing and Editing Files

CommandDescriptionExample
catDisplay entire file contentscat config.txt
lessView file with scrolling (press q to quit)less /var/log/syslog
headShow first lines of a filehead -20 file.txt
tailShow last lines of a filetail -f /var/log/syslog
nanoSimple text editor in the terminalnano myfile.txt
vimPowerful text editor (steep learning curve)vim script.sh
# View a file
$ cat /etc/hostname
my-linux-pc

# Watch a log file in real time
$ tail -f /var/log/syslog

# Edit a file with nano (Ctrl+O to save, Ctrl+X to exit)
$ nano ~/notes.txt

Searching and Finding

CommandDescriptionExample
findSearch for files by name, type, or sizefind / -name "*.conf"
grepSearch text inside filesgrep "error" logfile.txt
grep -rSearch recursively in directoriesgrep -r "TODO" ./src
whichFind the location of a commandwhich python3
locateFast file search using a databaselocate nginx.conf

Permissions and Ownership

Every file in Linux has permissions that control who can read, write, and execute it. Understanding permissions is essential for security and daily usage.

# View permissions
$ ls -l script.sh
-rwxr-xr-- 1 user group 1024 Apr 11 10:00 script.sh

# Change permissions (make executable)
$ chmod +x script.sh

# Change ownership
$ sudo chown user:group file.txt

# Set specific permissions (owner: rwx, group: rx, others: r)
$ chmod 754 script.sh

The permission string -rwxr-xr-- breaks down as: file type (-), owner permissions (rwx), group permissions (r-x), and others' permissions (r--).

System Information

CommandDescription
uname -aDisplay kernel and system information
df -hShow disk space usage in human-readable format
free -hDisplay memory usage
top or htopShow running processes and resource usage
whoamiDisplay your current username
uptimeShow how long the system has been running

Pipes and Redirection

One of the most powerful features of the Linux command line is the ability to chain commands together using pipes (|) and redirect output to files.

# Pipe output from one command to another
$ ls -la | grep ".txt"

# Count the number of files in a directory
$ ls | wc -l

# Redirect output to a file
$ echo "Hello Linux" > greeting.txt

# Append to a file
$ echo "Another line" >> greeting.txt

# Sort and remove duplicates
$ cat names.txt | sort | uniq

Essential Keyboard Shortcuts

ShortcutAction
TabAuto-complete commands and file names
Ctrl + CCancel the current command
Ctrl + LClear the screen
Ctrl + RSearch command history
Ctrl + AMove cursor to the beginning of the line
Ctrl + EMove cursor to the end of the line
Up ArrowRecall the previous command
Tip: Pressing Tab twice shows all possible completions. This is the single most useful habit you can develop when using the terminal.

Ready to automate tasks?

Shell Scripting Guide →