Linux Basics

Linux is built around commands, and learning a few basic ones makes it much easier to work comfortably in the terminal. This page gives a simple introduction to useful Linux commands for beginners.


Basic Linux Commands

tty — Shows the current logged-in terminal

tty

whoami — Shows the currently logged-in user

whoami

which — Reveals where in the search path a program is located

which ls

echo — Prints text to the screen

echo "Hello Linux"

echo $PATH — Displays the current PATH variable

echo $PATH

echo $PWD — Displays the current working directory

echo $PWD

echo $OLDPWD — Displays the previously visited directory

echo $OLDPWD

clear — Clears the terminal screen

clear

reset — Resets the terminal screen buffer

reset

history — Shows previously executed commands

history

!110 — Executes command number 110 from history

!110

Files and Directories

ls — Lists files and directories

ls

ls -ltr — Lists files sorted by modification time

ls -ltr

ls -a — Lists all files including hidden files

ls -a

ls -l — Displays detailed file information

ls -l

mkdir — Creates a directory

mkdir mydir

mkdir -p — Creates nested directories

mkdir -p dir1/dir2/dir3

touch — Creates a new file

touch file.txt

cp — Copies files or directories

cp dir1 /root/

mv — Moves or renames files

mv dir1 /root/

rm — Removes a file or directory

rm file.txt

rm -rf — Removes directory recursively and forcefully

rm -rf dir1

Command Help

Use manual or help options to learn more about commands:

man <command>
<command> --help

Example:

rm --help
mkdir --help
ls --help

Directory Navigation

cd — Changes to home directory

cd

cd ~ — Changes to home directory

cd ~

cd / — Changes to root directory

cd /

cd Desktop/ — Changes to relative directory

cd Desktop/

cd .. — Moves one level up

cd ..

cd ../.. — Moves two levels up

cd ../..

User and Group Commands

useradd — Creates a user

useradd username

passwd — Assigns password to user

passwd username

groupadd — Creates a group

groupadd groupname

usermod -a -G — Adds user to group

usermod -a -G groupname username

groupmod -A — Adds user to group

groupmod -A username groupname

useradd (advanced) — Creates user with custom options

useradd -d /home/user -s /bin/bash -g groupname -u 1001 username

File Content Commands

head — Displays first lines of a file

head /var/log/messages

head -10 — Displays first 10 lines

head -10 /var/log/messages

tail — Displays last lines of a file

tail /var/log/messages

tail -10 — Displays last 10 lines

tail -10 /var/log/messages

wc -l — Counts number of lines in file

wc -l /var/log/messages