DEV Community

Jayaprasanna Roddam
Jayaprasanna Roddam

Posted on

Linux commands

Linux Basics

Linux is a free, open-source operating system used on servers, desktops, and embedded systems. It supports both command-line (CLI) and graphical (GUI) interfaces.


Key Concepts

  • Case Sensitivity: Files and directories are case-sensitive.
  • Everything is a File: Devices, processes, and sockets are represented as files.
  • Multi-user Support: Multiple users can operate simultaneously.
  • Multitasking: Multiple processes can run at the same time.
  • Permissions: Access to files and directories is controlled by permissions.
  • Processes: Each running program is a process with a unique PID.
  • Shell: The command-line interface (e.g., Bash) interprets user commands.

File System Structure

Directory Purpose
/ Root directory
/home User home directories
/etc System configuration files
/var Variable files (logs, spool)
/usr User programs and data
/bin Essential user binaries
/sbin System binaries
/tmp Temporary files
/dev Device files
/proc Process and kernel information
/lib Essential shared libraries
/opt Optional application software packages
/mnt Mount point for temporary filesystems
/media Removable media (USB, CD-ROM)

Common Linux Commands

Command Description
pwd Print working directory
ls List files and directories
ls -l Long listing format
ls -a Show hidden files
cd <dir> Change directory
cd ~ Go to home directory
cd .. Go up one directory
cp <src> <dst> Copy files or directories
cp -r <src> <dst> Copy directories recursively
mv <src> <dst> Move or rename files/directories
rm <file> Remove files
rm -r <dir> Remove directories recursively
mkdir <dir> Create a new directory
rmdir <dir> Remove an empty directory
cat <file> Display file contents
less <file> View file contents page by page
more <file> View file contents page by page
head <file> Show first 10 lines of a file
tail <file> Show last 10 lines of a file
tail -f <file> Follow appended data in real time
touch <file> Create an empty file or update timestamp
echo <text> Print text to terminal
man <command> Show manual for a command
info <command> Show detailed info about a command
chmod <mode> <file> Change file permissions
chown <user>:<group> <file> Change file owner/group
ps Show running processes
ps aux Detailed process list
kill <pid> Kill a process by PID
killall <name> Kill processes by name
top Display running processes
htop Interactive process viewer
df -h Show disk space usage
du -sh <dir> Show directory size
free -h Show memory usage
ifconfig / ip a Show network interfaces
ping <host> Test network connectivity
wget <url> Download files from the web
curl <url> Transfer data from/to a server
ssh <user>@<host> Secure shell remote login
scp <src> <dst> Secure copy files over SSH
tar -czvf <file.tar.gz> <dir> Create compressed archive
tar -xzvf <file.tar.gz> Extract compressed archive
zip <file.zip> <files> Create zip archive
unzip <file.zip> Extract zip archive
find <dir> -name <pattern> Search for files
grep <pattern> <file> Search text in files
history Show command history
alias ll='ls -l' Create command alias
sudo <command> Run command as superuser
passwd Change user password
date Show current date and time
cal Display a calendar
whoami Show current user
uname -a Show system information
reboot Reboot the system
shutdown now Shutdown the system immediately

File Permissions

  • r: read
  • w: write
  • x: execute

Example: -rwxr-xr--

  • Owner: rwx (read, write, execute)
  • Group: r-x (read, execute)
  • Others: r-- (read)

Change permissions:

chmod u+x file      # Add execute for user
chmod g-w file      # Remove write for group
chmod o+r file      # Add read for others
chmod 755 file      # rwxr-xr-x
Enter fullscreen mode Exit fullscreen mode

Change ownership:

chown user file
chown user:group file
Enter fullscreen mode Exit fullscreen mode

Process Management

  • ps, top, htop: View processes
  • kill <pid>: Terminate process
  • jobs: List background jobs
  • bg, fg: Move jobs to background/foreground
  • &: Run command in background (command &)

Networking

  • ifconfig / ip a: Show interfaces
  • ping <host>: Test connectivity
  • netstat -tuln: Show listening ports
  • ss -tuln: Show socket statistics
  • scp, ssh: Secure file transfer and remote login

Package Management

  • Debian/Ubuntu: apt, dpkg
    • sudo apt update
    • sudo apt upgrade
    • sudo apt install <package>
    • sudo apt remove <package>
  • RedHat/CentOS: yum, dnf, rpm
    • sudo yum install <package>
    • sudo dnf install <package>
    • sudo rpm -i <package.rpm>

Useful Tips

  • Use tab for auto-completion.
  • Use history to see previous commands.
  • Use sudo to run commands as superuser.
  • Use ctrl + c to stop a running command.
  • Use ctrl + l to clear the terminal.
  • Use !! to repeat the last command.
  • Use !n to repeat command number n from history.
  • Use clear to clear the terminal screen.
  • Use exit to close the terminal session.

Editing Files

  • nano <file>: Simple text editor
  • vim <file>: Advanced text editor
  • emacs <file>: Powerful text editor

Redirection and Pipes

  • >: Redirect output to file (ls > out.txt)
  • >>: Append output to file (echo hi >> out.txt)
  • <: Use file as input (sort < file.txt)
  • |: Pipe output to another command (ls | grep txt)

Environment Variables

  • View: echo $HOME
  • Set: export VAR=value
  • List: env

Getting Help

  • man <command>: Manual page for a command
  • <command> --help: Most commands support this flag
  • info <command>: More detailed help

System Monitoring

  • top, htop: Monitor processes
  • free -h: Memory usage
  • df -h: Disk usage
  • du -sh <dir>: Directory size
  • uptime: System uptime

Users and Groups

  • who: List logged-in users
  • id: Show user and group IDs
  • adduser <user>: Add new user
  • deluser <user>: Delete user
  • usermod -aG <group> <user>: Add user to group
  • groups <user>: Show user groups

Scheduling Tasks

  • crontab -e: Edit user cron jobs
  • at <time>: Schedule one-time task

Disk Management

  • lsblk: List block devices
  • fdisk -l: List disk partitions
  • mount <device> <dir>: Mount device
  • umount <dir>: Unmount device

Archiving and Compression

  • tar -czvf archive.tar.gz dir/: Create archive
  • tar -xzvf archive.tar.gz: Extract archive
  • gzip file: Compress file
  • gunzip file.gz: Decompress file

Working with Files from the Terminal

  • Write to a file:

    echo "text" > file.txt — Overwrites file with text

    echo "text" >> file.txt — Appends text to file

  • Save output of a command to a file:

    ls > files.txt — Saves output of ls to files.txt

  • Edit and save files interactively:

    • nano file.txt — Edit and save with Ctrl+O, then exit with Ctrl+X
    • vim file.txt — Enter insert mode with i, save with :w, quit with :q
    • emacs file.txt — Save with Ctrl+X Ctrl+S, exit with Ctrl+X Ctrl+C
  • Close a file in an editor:

    • In nano: Ctrl+X
    • In vim: :q (quit), :wq (write and quit)
    • In emacs: Ctrl+X Ctrl+C
  • Saving and closing files is editor-specific; always check the help (Ctrl+G in nano, :help in vim, C-h in emacs) for more shortcuts.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.