Introduction

Before diving into the command list, ensure you’re familiar with accessing the command-line interface (CLI). If you need guidance, consider reviewing a CLI tutorial.

Accessing the command-line may vary depending on your Linux distribution, but it’s typically found in the Utilities section. On Ubuntu, for example, you can open it by pressing Ctrl+Alt+T.


Basic Linux Commands Overview

pwd (Print Working Directory)

Use pwd to display the absolute path of your current working directory. This is useful to verify where you are in the filesystem:

pwd

Example output:

/home/username

cd (Change Directory)

Navigate directories using cd. Provide either the full path or directory name:

cd Photos
cd /home/username/Movies

Shortcuts:

  • cd .. moves one directory up.
  • cd (with no arguments) goes to the home folder.
  • cd - moves to the previous directory.

ls (List)

View directory contents with ls:

ls
ls /home/username/Documents

Variations:

  • ls -R lists files in subdirectories recursively.
  • ls -a shows hidden files (files starting with a dot (.).
  • ls -al provides a detailed list, including permissions, number of links, owner, group, size, and modification time.

For example:

ls -al /home/username

cat (Concatenate)

List file contents with cat:

cat file.txt

Other usages:

  • cat > filename (create a new file and write to it):

    cat > newfile.txt
    

    Then type your text and press Ctrl+D to save.

  • cat filename1 filename2 > filename3 (join files):

    cat file1.txt file2.txt > combined.txt
    

cp (Copy)

Duplicate files using cp:

cp scenery.jpg /home/username/Pictures

For copying directories, use -r (recursive):

cp -r /source_directory /destination_directory

mv (Move/Rename)

Move or rename files with mv:

mv file.txt /home/username/Documents
mv oldname.ext newname.ext

mkdir (Make Directory)

Create directories using mkdir:

mkdir Music

Additional Options:

  • mkdir Music/Newfile (create directory within another):

    mkdir Music/Newfile
    

    mkdir -p Music/2020/Newfile (create nested directory structure):

    mkdir -p Music/2020/Newfile
    

rmdir (Remove Directory)

Delete empty directories with rmdir:

rmdir emptydir

rm (Remove)

Delete directories and their contents using rm.

rm filename.ext
rm -r directoryname  # to delete a directory and its contents

touch (Create File)

Create empty files with touch:

touch /home/username/Documents/Web.html

locate

Locate files quickly by name:

locate filename

Use -i for case-insensitivity and asterisks * for partial matches:

locate -i *partoffilename*

find

Search for files within specified directories:

find /home/username -name "filename.ext"

Find all .txt files:

find /home/username -type f -name "*.txt"

grep

Search text within files using grep:

grep "search_term" file.txt

Search recursively in all files within a directory:

grep -r "search_term" /home/username/Documents

sudo

Execute commands with administrative privileges:

sudo apt update

df (Disk Free)

Check disk space usage with df:

df -h  # human-readable format

du (Disk Usage)

View file or directory disk usage with du:

du -sh /home/username/Documents

Display the first lines of a text file:

head -n 5 filename.ext  # show first 5 lines

tail

Display the last lines of a text file:

tail -n 5 filename.ext  # show last 5 lines

diff

Compare file contents line by line:

diff file1.txt file2.txt

tar

Archive files into a tarball format using tar:

To create a tar archive:

tar -cvf archive.tar /path/to/directory

To extract a tar archive:

tar -xvf archive.tar

chmod (Change Mode)

Change file or directory permissions with chmod:

chmod 755 filename.ext  # owner can read/write/execute, others can read/execute

chown (Change Owner)

Change file or directory ownership with chown:

sudo chown username:groupname filename.ext

jobs

Display current jobs and their statuses:

jobs

kill

Terminate unresponsive programs using kill:

kill PID  # replace PID with the process ID

Common signals:

  • SIGTERM (15) — request program to stop.
  • SIGKILL (9) — force stop program immediately.
kill -9 PID

ping

Check connectivity to a server:

ping example.com

wget

Download files from the internet:

wget http://example.com/file.zip

uname

Display system information:

uname -a  # all information

top

Monitor running processes and CPU usage:

top

history

Review previously entered commands:

history

man (Manual)

Access manual pages for commands:

man ls

echo

Move data into a file using echo:

echo "Hello World" > hello.txt

zip, unzip

Compress files with zip:

zip archive.zip file1 file2

Extract zipped files with unzip:

unzip archive.zip

hostname

Display or set the system’s hostname:

hostname

useradd, userdel

Manage user accounts:

To create a new user:

sudo useradd newusername

To delete a user:

sudo userdel username

Additional Commands

ps (Process Status)

Displays a list of active processes:

ps aux

whoami

Displays the current logged-in username:

whoami

uname -r

Shows the system’s kernel version:

uname -r

df -i (Disk Free Inodes)

Shows the number of available inodes:

df -i

free (Memory Usage)

Displays information about memory usage:

free -h

scp (Secure Copy)

Copies files between hosts over SSH:

scp file.txt username@remote_host:/path/to/destination

rsync

Synchronizes files and directories between two locations:

rsync -avh /source/directory /destination/directory

cron (Crontab)

Manages scheduled tasks. Introduce this for explaining how scheduled tasks work:

crontab -e

iptables

Manages network traffic and firewall rules. This is an advanced topic but useful for system administration:

sudo iptables -L

systemctl

Manages systemd services. Essential for modern Linux distributions:

sudo systemctl status service_name
sudo systemctl start service_name
sudo systemctl stop service_name
sudo systemctl enable service_name
sudo systemctl disable service_name

Additional Topics

Environment Variables

Explanation of setting and using environment variables:

export VAR_NAME="value"
echo $VAR_NAME

Aliases

How to create your own aliases for frequently used commands:

alias ll='ls -la'

SSH

Secure access to another computer over the network:

ssh username@hostname

Package Management

Examples of package managers such as apt for Debian/Ubuntu and yum or dnf for Red Hat/Fedora:

sudo apt install package_name
sudo yum install package_name
sudo dnf install package_name

Network Commands

Commands like netstat, ifconfig, and ip for network management:

ifconfig
ip a
netstat -tuln

Log Files

How to view system logs, such as using dmesg and files in /var/log:

dmesg
tail -f /var/log/syslog

Shell Scripting

An introduction to shell scripting for automation and more complex tasks:

#!/bin/bash
echo "Hello, World!"

Bonus Tips and Tricks

  • Use clear to clean the terminal screen:

    clear
    
  • Utilize TAB for autofilling commands or filenames.

  • Ctrl+C to terminate a running command.

  • Ctrl+Z to pause a running command, and bg to resume it in the background.

  • Ctrl+S freezes terminal output; Ctrl+Q unfreezes it.

  • Ctrl+A moves the cursor to the beginning of the line; Ctrl+E to the end.

  • Run multiple commands in sequence with ; or conditionally with &&:

    command1; command2
    command1 && command2  # command2 runs only if command1 is successful
    

These examples should provide you with a good starting point for using the Linux command-line. Experiment with these commands to become more comfortable with the CLI environment.

Last updated 22 Sep 2024, 12:15 CEST . history