Linux Commands and Directory Structure

Essential Linux Commands

Command Description
pwd Print current working directory
ls List files and directories
ls -l List with details (long format)
cd [directory] Change directory
cd .. Go up one directory
cp [src] [dest] Copy files or directories
mv [src] [dest] Move or rename files/directories
rm [file] Remove a file
rm -r [dir] Remove a directory and its contents
touch [file] Create an empty file
mkdir [dir] Create a new directory
cat [file] Display file contents
less [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
grep ‘pattern’ [file] Search for a pattern in a file
find [dir] -name [pattern] Find files by name
chmod [options] [file] Change file permissions
chown [user]:[group] [file] Change file owner/group
df -h Show disk usage (human-readable)
du -sh [dir/file] Show size of directory/file
top Show running processes
ps aux Show all running processes
kill [PID] Kill a process by process ID
man [command] Show manual/help for a command
history Show command history
sudo [command] Run command as superuser

Linux Directory Structure (Common Directories Explained)

/
├── bin        # Essential user binaries (commands)
├── boot       # Boot loader files, kernel, etc.
├── dev        # Device files
├── etc        # Configuration files
├── home       # User home directories
├── lib        # Essential shared libraries
├── media      # Mount points for removable media (CD, USB)
├── mnt        # Temporary mount points
├── opt        # Optional application software packages
├── proc       # Kernel and process information (virtual filesystem)
├── root       # Home directory for root user
├── run        # Runtime data
├── sbin       # System binaries (admin commands)
├── srv        # Data for services provided by the system
├── sys        # System files (virtual filesystem)
├── tmp        # Temporary files
├── usr        # Secondary hierarchy (user programs, libraries)
│   ├── bin    # Non-essential user binaries
│   ├── lib    # Non-essential libraries
│   └── share  # Shared data
└── var        # Variable data (logs, spool files, etc.)

Key Points

  • / is the root of the whole file system.
  • /home/username is your personal directory.
  • /etc contains system-wide configuration files.
  • /var/log has system log files.
  • /tmp is for temporary files (often cleared on reboot).
  • /usr holds user programs and data.

Example: Navigating and Listing Directories

pwd                 # Shows current directory, e.g., /home/user
cd /etc             # Changes to /etc directory
ls /var/log         # Lists log files in /var/log

Basic Commands:

ls List all files and directories
ls -l List all files and directories with some extra information
dir Display directories
mkdir <name> Create a directory
mkidr -p <dir_name1>/<dir_name2> Create multiple directories
rmdir <dir_name> Remove an empty directory
rm <file_name> Remove a file/directory with confirmation
rm -rf <file/dir_name> Remove file/directory without confirmation
cat <file_name> View a file
cat > <file_name> Create a new file and edit it
touch <file_name> Create a file
vi <file_name> File editor
vim <file_name> File editor
command >file_name Write output of the command into the file
cd Change directory
cd .. Move one directory back
cd – Move to previous directory
cd ~ Move to current user’s home directory
cd /home/me Move to /home/me directory
shutdown -h now Shuts the system down to halt immediately.
shutdown -r now Shuts the system down immediately and the system reboots.
mv -i myfile yourfile Move the file from “myfile” to “yourfile”. This effectively changes the name of “myfile” to “yourfile”.
mv -i /data/myfile . Move the file from “myfile” from the directory “/data” to the current working directory.
echo <text> Display the text
find Search for files in a directory hierarchy
grep Search
wc Word count
kill To kill a process
reboot Reboot the system
poweroff poweroff the system
mount mount a partition
umount unmount a partition
fdisk -l Partition manipulator

System Informations:

pwd Prints present working directory
hostname Prints hostname
whoami Prints your login name
date Prints system date
cal <year> Prints calendar of the year
who Determine the users logged on the machine
w Determine who is logged on the system
rwho -a Determine the remote users
finger <user_name> System info about user
last Show list of users last logged-in on your system
lastb  Show last unsuccessful login attempts on your system
history Show the used commands
comman Run the most recent command from the bash history commands that start with the string “ comman “
uptime Display the system uptime
ps Process status
ps -aux | more List all the currently running process
top List the currently running process, sorted by CPU usage
gtop, ktop, htop GUI choice for top
uname Information of your system
arch Display the system architecture
Xorg -version Show the version of X windows I have on my system
cat /etc/issue Check what distribution you are using
free -m Check your usage, free memory of primary memory
df -h Disk free information in human readable form
du / -bh | more Print detailed disk usage for each sub-directory starting at the “/” (root) directory
cat /proc/cpuinfo Displays cpu information
cat /etc/interrupts List the interrupts in use
cat /proc/version Linux version and other info
cat /proc/filesystems Show the type of filesystem currently in use
cat /etc/printcap | less Show the setup of printers
lsmod Show the currently loaded kernel modules
set | more Show the current user environment
env | more Show environment variables
dmesg | less Print kernel messages
chage -l <user_login_name> See my password expiry information
quota Display my disk quota
sysctl -a | more Display  all the configurable Linux kernel parameters
runlevel Print the previous and current runlevel

Help Commands:

man <command_name>  Display man pages of the command
<command_name> –help  Command help
info <command_name>  Helping command
whatis <command_name>  Display man pages description

Compress and Decompress Commands:

tar –cvf <file_name.tar> <file_name_1> <file_name_2> . . . Compress files
tar –xvf <file_name.tar> Decompress the compressed file
tar –xvf <file_name.tar> – C <location> Decompress files to desired location
tar –zcvf <file_name.tar.gz> <file_name_1> <file_name_2> Compress files with gz
tar –zxvf <file_name.tar.gz> Decompress the compressed gz files
tar –zxvf <file_name.tar.gz> -C <location> Decompress files to desired location

 apt-get Commands:

apt-get install <package_name> Installing package(s)
apt-get remove <package_name> Removing package(s)
apt-get update Update the repository
apt-cdrom add Add CD ROM archives to repository
apt-cdrom ident Identify CD-ROM disk
apt-get  -d install <package_name> Download packages, no installation or unpacking
apt-get –purge remove <package_name>  Remove all traces of a package, incl. Configuration files etc.,
apt-get –u update  Upgrades all installed packages, but does not remove any packages to resolve dependencies
apt-get –u dist-upgrade Upgrades all the installed packages, removes or installs packages as needed to satisfy all dependencies
apt-cache search <package_name> Search package in the cache
apt-get check Check broken dependencies
apt-cache autoclean Remove cached packages that are no longer needed
apt-cache clean Remove all cached packages
apt-get help Help

dpkg Commands:

dpkg –l List all the installed packages
dpkg –L  <package_name> List files belonging to a package
dpkg –S <file_name> To See which package a file belongs to
dpkg –s <package_name> To show complete package information
dpkg –yet-to-unpack To look for downloaded, uninstalled packages
dpkg –audit Show partially installed packages
dpkg -i <package> Install a new package
dpkg -r <package> Remove a package

yum Commands:

yum list [available|installed|extras|updates|obsoletes|all|recent] [pkgspec]
yum list List packages enabled in the repository
yum list all List packages enabled in the repository
yum list available Lists all the packages available to be installed in any enabled repository on your system
yum list installed Lists all the packages installed on the system
yum list extras Lists any installed package which no longer appears in any of your enabled repositories
yum list obsoletes Lists any obsoleting relationships between any available package and any installed package
yum list updates Lists any package in an enabled repository which is an update for any installed package
yum list recent  Lists any package added to any enabled repository in the last seven(7) days
yum list pkgspec Refine your listing for particular packages
yum check-update It returns an exit code of 100 if there are any updates available
yum info Displays information about any package installed or available
yum search Search and list the packages
yum provides/yum whatprovides Searches for which packages provide the requested dependency of file and also takes wildcards for files
yum clean Clean up the cache of metadata and packages
yum clean packages Cleans up any cached packages in any enabled repository cache directory
yum clean metadata Cleans up any xml metadata that may have been cached from any enabled repository
yum clean dbcache Clean up the cached copies of those from any enabled repository cache
yum clean all Clean all cached files from any enabled repository
yum shell  /  yum makecache These two commands are used to download and make usable all the metadata for the currently enabled yum repos

rpm Commands:

rpm –ivh <package_name> Install a new package
rpm –Uvh <package_name> Update an already installed package
rpm –e<package_name> Remove a package
rpm –aq To list all rpm packages installed on your system
rpm –F <package_name> Freshening up the already installed package
rpm –version Prints rpm version

Linux Directory Structure:

/bin User Binaries
/sbin System Binaries
/etc Configuration Files
/root Root User’s Home Directory
/dev Device Files
/proc Precess Information
/media Removable Media
/var Variable Files
/tmp Temporary Files
/usr User Programs
/home Home Directories
/boot Bootloader Files
/lib System Libraries
/opt Optional add-on Apps
/mnt Mount Directory
/srv Service Data

Networking Commands:

 ifconfig  Displays all the interface information
 ifstat  Check the current network usage
 iptraf  A network utility allows you check the network activities
 ifup  Bring a network interface up
 ifdown  Bring a network interface down
Avatar photo

Asif Khan

Responsible and proactive professional with more than 13 years of experience in IT systems, open source software applications, DevOps, Linux systems, and cloud operations. My main goals are to automate things, keep them safe, and make sure they are strong. I am very good at planning and building the infrastructure for services that people really want. I was drawn to the fast-paced world of cloud computing because it has resources that can be scaled up or down as needed. One of my best skills is being able to use a lot of different DevOps tools to set up, release management, and microservices ecosystems, as well as for provisioning, orchestration, and configuration management.