300 Linux Command Questions with Answers
Basic Commands and Navigation
- What command displays your current working directory?
Ans: pwd (Print Working Directory)
- How do you list files in a directory?
Ans: ls (List)
- What does the -l option do when used with ls?
Ans: Shows files in long format with details like permissions, ownership, size, and date
- How do you list hidden files in Linux?
Ans: ls -a (lists all files including hidden ones that start with a dot)
- What command changes directories?
Ans: cd (Change Directory)
- How do you navigate to your home directory quickly?
Ans: cd or cd ~
- What command creates a new directory?
Ans: mkdir (Make Directory)
- How do you remove an empty directory?
Ans: rmdir (Remove Directory)
- What command removes a non-empty directory?
Ans: rm -r (Remove recursively)
- How do you create an empty file?
Ans: touch filename
- What is the difference between rm and rmdir?
Ans: rm removes files by default, and with -r flag can remove directories and their contents rmdir only removes empty directories
- How do you copy a file?
Ans: cp source destination
- How do you move a file?
Ans: mv source destination
- What does the cp -r command do?
Ans: Copies directories recursively (including all files and subdirectories)
- How do you display the contents of a file?
Ans: cat filename (concatenate and display)
- What command shows just the first few lines of a file?
Ans: head filename
- What command shows just the last few lines of a file?
Ans: tail filename
- How do you concatenate and display file contents?
Ans: cat file1 file2 file3
- What command shows file or directory information?
Ans: stat filename
- How do you find recently modified files?
Ans: find /path -type f -mtime -n (where n is the number of days)
File Permissions and Ownership
- What command changes file permissions?
Ans: chmod (Change Mode)
- How do you read file permissions in Linux?
Ans: In the format -rwxrwxrwx where first character is file type, and the next 9 characters represent owner, group, and others permissions (read, write, execute)
- What do the numbers 754 represent in chmod?
Ans: 7 (rwx=4+2+1) for owner, 5 (r-x=4+0+1) for group, 4 (r–=4+0+0) for others
- How do you make a file executable?
Ans: chmod +x filename
- What command changes file ownership?
Ans: chown user filename
- How do you change both user and group ownership at once?
Ans: chown user:group filename
- What command sets default permissions for new files?
Ans: umask
- What is the difference between symbolic and numeric permission notation?
Ans: Symbolic uses letters (r,w,x) and operators (+,-,=)
Numeric uses octal numbers (0-7)
- How do you view the permissions in octal format?
Ans: stat -c ‘%a %n’ filename
- What is the sticky bit and how do you set it?
Ans: It restricts file deletion; set with chmod +t directory or chmod 1xxx directory
- What does the setuid bit do?
Ans: Allows users to run an executable with the permissions of the file owner; set with chmod u+s filename
- What does the setgid bit do?
Ans: When applied to directories, new files inherit the directory’s group; set with chmod g+s directory
- How do you find all files with specific permissions?
Ans: find /path -perm mode
- What permissions are required to delete a file?
Ans: Write permission on the directory containing the file
- What command displays your current user ID and group IDs?
Ans: id
- How do you change only specific permission bits?
Ans: chmod u+x,g-w,o=r filename
- How do you copy file permissions between files?
Ans: chmod –reference=reference_file target_file
- What is ACL in Linux and how do you use it?
Ans: Access Control Lists provide more granular permissions; use setfacl to set and getfacl to view
- How do you check if a file has extended attributes?
Ans: lsattr filename
- What command shows the security context of a file?
Ans: ls -Z filename (on SELinux-enabled systems)
Users and Groups
- What command creates a new user?
Ans: useradd username or adduser username
- How do you delete a user account?
Ans: userdel username
- What command creates a new group?
Ans: groupadd groupname
- How do you add a user to a group?
Ans: usermod -aG groupname username
- What command changes your password?
Ans: passwd
- How do you switch to another user?
Ans: su username
- What command shows all currently logged-in users?
Ans: who or w
- How do you list all users on the system?
Ans: cat /etc/passwd or getent passwd
- What file contains user information?
Ans: /etc/passwd
- What file contains group information?
Ans: /etc/group
- How do you lock a user account?
Ans: passwd -l username or usermod -L username
- What command modifies user account details?
Ans: usermod
- How do you change a user’s default shell?
Ans: chsh -s /path/to/shell username or usermod -s /path/to/shell username
- What command shows your current username?
Ans: whoami
- How do you temporarily become the superuser?
Ans: sudo -i or sudo su
- What is the difference between su and sudo?
Ans: su switches user (often to root) and requires the target user’s password
sudo executes a single command with elevated privileges and requires your own password
- How do you edit the sudoers file safely?
Ans: visudo
- What command sets user resource limits?
Ans: ulimit for shell limits or prlimit for process limits
- How do you check the groups a user belongs to?
Ans: groups username
- How do you remove a user from a group?
Ans: gpasswd -d username groupname or deluser username groupname
Process Management
- What command lists running processes?
Ans: ps
- How do you view processes in a hierarchical tree?
Ans: pstree
- What command shows dynamic real-time processes?
Ans: top or htop
- How do you kill a process?
Ans: kill PID or killall process_name
- What signal does kill -9 send?
Ans: SIGKILL (forcefully terminates the process)
- How do you find the process ID of a specific program?
Ans: pidof program_name or pgrep program_name
- What command runs a process in the background?
Ans: Add & at the end of command or press Ctrl+Z followed by bg
- How do you bring a background process to the foreground?
Ans: fg [job_number]
- What command displays the status of jobs in the current session?
Ans: jobs
- How do you set process priority when launching a program?
Ans: nice -n value command
- What command changes the priority of a running process?
Ans: renice -n value -p PID
- How do you list processes by resource usage?
Ans: ps aux –sort=-%cpu or ps aux –sort=-%mem
- What command shows process information for a specific user?
Ans: ps -u username
- How do you check which processes are using a file?
Ans: lsof filename
- What command displays information about process limits?
Ans: ulimit -a
- How do you pause and resume a process?
Ans: Pause with kill -STOP PID, resume with kill -CONT PID
- What command shows processes sorted by memory usage?
Ans: ps aux –sort=-%mem
- How do you start a process that continues after you log out?
Ans: nohup command & or using screen/tmux
- What command shows process ancestry?
Ans: ps f or pstree
- How do you kill all processes of a specific user?
Ans: pkill -u username
Text Processing
- What command searches for text patterns in files?
Ans: grep pattern files
- How do you use regular expressions with grep?
Ans: grep -E “regex_pattern” file or egrep “regex_pattern” file
- What command counts lines, words, and characters?
Ans: wc filename
- How do you replace text in a file without opening it?
Ans: sed ‘s/old_text/new_text/g’ filename
- What command sorts the content of a file?
Ans: sort filename
- How do you remove duplicate lines from a file?
Ans: sort filename | uniq or awk ‘!seen[$0]++’ filename
- What command extracts specific columns from text?
Ans: cut -d delimiter -f fields filename
- How do you combine multiple files side by side?
Ans: paste file1 file2
- What command compares two files?
Ans: diff file1 file2
- How do you display differences between files in a unified format?
Ans: diff -u file1 file2
- What command checks a file for spelling errors?
Ans: aspell check filename
- How do you convert tabs to spaces in a file?
Ans: expand filename
- What command shows non-printable characters in a file?
Ans: cat -A filename or cat -v filename
- How do you extract specific lines from a file?
Ans: sed -n ‘start,endp’ filename or head -n end | tail -n count
- What command joins lines from multiple files based on a common field?
Ans: join file1 file2
- How do you convert text to uppercase or lowercase?
Ans: To uppercase: tr ‘a-z’ ‘A-Z’ < file
To lowercase: tr ‘A-Z’ ‘a-z’ < file
- What command formats text into columns?
Ans: column -t filename
- How do you print specific fields from each line?
Ans: awk ‘{print $1, $3}’ filename
- What command applies a command to each line of a file?
Ans: xargs command < input_file
- How do you find lines matching multiple patterns?
Ans: grep -E ‘pattern1|pattern2’ file or grep pattern1 file | grep pattern2
Shell Scripting Basics
- How do you create a shell script?
Ans: Create a text file with commands and make it executable with chmod +x script.sh
- What permissions should a shell script have?
Ans: At minimum, executable (chmod +x script.sh or chmod 755 script.sh)
- How do you pass arguments to a shell script?
Ans: Add arguments after the script name: ./script.sh arg1 arg2
Access inside script with $1, $2, etc. or $@ for all arguments
- What does #!/bin/bash mean at the start of a script?
Ans: It’s a shebang that specifies which interpreter should execute the script (in this case, bash)
- How do you define variables in shell scripts?
Ans: variable_name=value (no spaces around =)
- What command returns the exit status of the last command?
Ans: echo $?
- How do you implement if-else statements in bash?
Ans: if [ condition ]; then
commands
elif [ condition ]; then
commands
else
commands
fi
- What is the syntax for loops in bash?
Ans: For loop:
for var in list; do
commands
done
While loop:
while [ condition ]; do
commands
done
- How do you read user input in a script?
Ans: read variable_name
What are shell script functions and how do you define them?
Ans: function_name() {
commands
return value
}
- How do you perform arithmetic operations in bash?
Ans: $((expression)) or expr expression
- What command executes another command and captures its output?
Ans: Command substitution: $(command) or backticks `command`
- How do you check if a file exists in a script?
Ans: if [ -f filename ]; then
- What is command substitution and how is it used?
Ans: Captures output of a command: variable=$(command) or variable=`command`
- How do you create arrays in bash?
Ans: array=(value1 value2 value3)
- What special variables are available in shell scripts?
Ans: $0 (script name), $1-$9 (arguments), $# (argument count), $@ (all arguments), $$ (PID), $? (exit status)
- How do you debug a shell script?
Ans: Run with -x flag: bash -x script.sh or add set -x in script
- What is the difference between single and double quotes in scripts?
Ans: Single quotes preserve literal value
Double quotes allow variable substitution and command expansion
- How do you handle errors in shell scripts?
Ans: Check exit status, use set -e to exit on error, use trap to catch signals
- What command returns the length of a string variable?
Ans: ${#variable}
System Information
- What command shows system hardware information?
Ans: lshw or dmidecode
- How do you display kernel version?
Ans: uname -r or cat /proc/version
- What command shows system uptime?
Ans: uptime
- How do you check CPU information?
Ans: lscpu or cat /proc/cpuinfo
- What command displays memory usage information?
Ans: free -h
- How do you check disk usage?
Ans: df -h
- What command shows free and used disk space?
Ans: df -h
- How do you check disk I/O statistics?
Ans: iostat
- What command displays network interface information?
Ans: ifconfig or ip addr show
- How do you show the system hostname?
Ans: Hostname
- What command displays system load average?
Ans: uptime or cat /proc/loadavg
- How do you view the current date and time?
Ans: date
- What command lists attached USB devices?
Ans: lsusb
- How do you view information about PCI devices?
Ans: lspci
- What command shows installed hardware components?
Ans: lshw
- How do you display the system’s boot messages?
Ans: dmesg
- What command checks the battery status on a laptop?
Ans: acpi -b or upower -i /org/freedesktop/UPower/devices/battery_BAT0
- How do you find out your Linux distribution?
Ans: lsb_release -a or cat /etc/*-release
- What command shows running time of processes?
Ans: ps -eo pid,cmd,etime
- How do you display the file system type?
Ans: df -T or findmnt
Disk and File System
- What command creates a file system on a partition?
Ans: mkfs -t filesystem /dev/partition
- How do you check and repair a file system?
Ans: fsck /dev/partition
- What command shows mounted file systems?
Ans: mount or df -h
- How do you mount a file system?
Ans: mount /dev/partition /mount_point
- What command unmounts a file system?
Ans: umount /mount_point or umount /dev/partition
- How do you create a swap space?
Ans: mkswap /dev/partition followed by swapon /dev/partition
- What command shows disk partition information?
Ans: fdisk -l or parted -l
- How do you create disk partitions?
Ans: Using fdisk, parted, or gparted
- What is the command to create a CD/DVD ISO image?
Ans: dd if=/dev/cdrom of=image.iso or readom dev=/dev/cdrom f=image.iso
- How do you burn an ISO to a CD/DVD?
Ans: wodim dev=/dev/cdrom image.iso or cdrecord -v dev=/dev/cdrom image.iso
- What command checks disk space usage by directory?
Ans: du -sh /path/to/directory
- How do you find large files on your system?
Ans: find /path -type f -size +100M or du -h /path | sort -hr | head
- What command creates a soft link?
Ans: ln -s target_file link_name
- How do you create a hard link?
Ans: ln target_file link_name
- What is the difference between soft and hard links?
Ans: Soft links point to a file path; can cross file systems; break if target is deleted
Hard links point to inode; must be on same file system; persist even if original is deleted
- How do you check inode usage on a file system?
Ans: df -i
- What command shows file system block size?
Ans: tune2fs -l /dev/partition | grep “Block size” or blockdev –getbsz /dev/partition
- How do you set disk quotas for users?
Ans: Use quotacheck, edquota, and quotaon commands
- What command displays read/write statistics for devices?
Ans: iostat or vmstat
- How do you clone a disk or partition?
Ans: dd if=/dev/source of=/dev/destination or using clonezilla
Package Management
- How do you install a package using apt?
Ans: apt install package_name
- What command removes a package in Debian/Ubuntu?
Ans: apt remove package_name or apt purge package_name
- How do you update the package list?
Ans: apt update
- What command upgrades all installed packages?
Ans: apt upgrade or apt full-upgrade
- How do you search for packages?
Ans: apt search keyword
- What command shows information about a package?
Ans: apt show package_name or dpkg -s package_name
- How do you list all installed packages?
Ans: dpkg -l or apt list –installed
- What command installs a local DEB file?
Ans: dpkg -i package.deb or apt install ./package.deb
- How do you add a new repository?
Ans: add-apt-repository ppa:repository/name or edit /etc/apt/sources.list
- What command cleans the local package cache?
Ans: apt clean or apt autoclean
- How do you hold a package at its current version?
Ans: apt-mark hold package_name
- What is the difference between upgrade and dist-upgrade?
Ans: upgrade only upgrades installed packages
dist-upgrade (or full-upgrade) may add or remove packages to satisfy dependencies
- How do you install a package using yum/dnf?
Ans: yum install package_name or dnf install package_name
- What command removes a package in Red Hat/Fedora?
Ans: yum remove package_name or dnf remove package_name
- How do you download a package without installing it?
Ans: apt download package_name or yumdownloader package_name
- What command shows package dependencies?
Ans: apt-cache depends package_name or rpm -qR package_name
- How do you list files installed by a package?
Ans: dpkg -L package_name or rpm -ql package_name
- What command finds which package a file belongs to?
Ans: dpkg -S /path/to/file or rpm -qf /path/to/file
- How do you check for broken dependencies?
Ans: apt check or yum check dependencies
- What command manages package repositories?
Ans: apt-add-repository in Debian/Ubuntu or editing repo files in /etc/yum.repos.d/
Networking
- What command shows your IP address?
Ans: ip addr or ifconfig
- How do you ping a remote host?
Ans: ping hostname
- What command shows active network connections?
Ans: netstat -tuln or ss -tuln
- How do you check open ports on your system?
Ans: netstat -tuln or ss -tuln or lsof -i
- What command traces the route to a host?
Ans: traceroute hostname or tracepath hostname
- How do you download files from the web?
Ans: wget URL or curl -O URL
- What command transfers files over SSH?
Ans: scp source user@host:destination
- How do you configure a network interface?
Ans: ifconfig interface options or ip addr add IP/mask dev interface
- What command shows network routing tables?
Ans: route -n or ip route show
- How do you check DNS resolution?
Ans: nslookup hostname or dig hostname
- What command captures and analyzes network packets?
Ans: tcpdump or wireshark
- How do you scan ports on a remote host?
Ans: nmap host
- What command checks if a port is reachable?
Ans: telnet host port or nc -zv host port
- How do you display socket statistics?
Ans: ss or netstat
- What command shows network interface statistics?
Ans: ip -s link or ifconfig
- How do you set up a basic firewall?
Ans: Using iptables, ufw, or firewalld
- What command checks bandwidth usage?
Ans: iftop or nethogs
- How do you share files over HTTP temporarily?
Ans: python -m http.server 8000 or python3 -m http.server 8000
- What command shows hostname resolution?
Ans: host hostname or getent hosts hostname
- How do you manage SSH keys?
Ans: ssh-keygen to create keys, ssh-copy-id to copy to remote host
System Administration
- How do you view system logs?
Ans: less /var/log/syslog or using journalctl
- What command schedules a job to run once?
Ans: at time or systemd-run –on-active=time command
- How do you schedule recurring jobs?
Ans: Using crontab -e or by adding scripts to /etc/cron.d/
- What command shows resource limits for users?
Ans: ulimit -a
- How do you change the system hostname?
Ans: hostnamectl set-hostname new_name or edit /etc/hostname
- What command synchronizes system time?
Ans: ntpdate pool.ntp.org or timedatectl set-ntp true
- How do you shut down the system?
Ans: shutdown -h now or systemctl poweroff
- What command reboots the system?
Ans: reboot or systemctl reboot
- How do you check if a service is running?
Ans: systemctl status service_name or service service_name status
- What command starts a service?
Ans: systemctl start service_name or service service_name start
- How do you enable a service to start at boot?
Ans: systemctl enable service_name
- What command shows memory consumption of each process?
Ans: ps aux –sort=-%mem
- How do you clear the terminal screen?
Ans: clear or Ctrl+L
- What command shows currently logged-in users with their activity?
Ans: w
- How do you view system boot logs?
Ans: journalctl -b
- What command manages background services?
Ans: systemctl or the older service
- How do you manage runlevels or targets?
systemctl get-default and systemctl set-default target
- What command displays the last login times?
Ans: last
- How do you send a message to all logged-in users?
Ans: wall “message”
- What command shows system initialization messages?
Ans: dmesg
Advanced Commands
- What command executes a program at specified intervals?
Ans: watch command
- How do you find file duplicates?
Ans: fdupes path or using find with checksums
- What command displays process wait I/O statistics?
Ans: pidstat -d or iotop
- How do you convert file encoding?
Ans: iconv -f source_encoding -t target_encoding file
- What command shows file access attempts?
Ans: auditctl with ausearch or aureport
- How do you stress test your system?
Ans: stress or stress-ng
- What command splits a file into smaller parts?
Ans: split -b size file or split -l lines file
- How do you merge multiple files into one?
Ans: cat file1 file2 file3 > merged_file
- What command connects to a remote server?
Ans: ssh user@hostname
- How do you sync files between directories or systems?
Ans: rsync source destination
- What command searches for files by content or attributes?
Ans: find with -exec grep or just grep -r
- How do you batch rename files?
Ans: rename ‘s/old/new/’ files or using a for loop with mv
- What command runs programs with modified environment variables?
Ans: env VAR=value command
- How do you limit process resource usage?
Ans: ulimit for shell limits or systemd-run with resource controls
- What command extracts archives?
Ans: tar -xf archive.tar or unzip archive.zip
- How do you create encrypted files?
Ans: Using gpg -c filename
- What command generates random data?
Ans: dd if=/dev/urandom or openssl rand
- How do you secure delete files?
Ans: shred filename or wipe filename
- What command benchmarks disk performance?
Ans: hdparm -t /dev/device or dd if=/dev/zero of=test bs=1M count=1000
- How do you run commands at low or high priority?
Ans: nice -n value command for low, nice -n -value command for high
Security
- What command changes file security contexts?
Ans: chcon or restorecon
- How do you check for listening services?
Ans: netstat -tulnp or ss -tulnp
- What command displays and controls SELinux?
Ans: sestatus shows status, setenforce changes modes
- How do you check for root kits?
Ans: rkhunter or chkrootkit
- What command shows failed login attempts?
Ans: lastb or journalctl _COMM=sshd
- How do you generate cryptographic hashes?
Ans: md5sum, sha1sum, sha256sum, etc.
- What command verifies file integrity?
Ans: md5sum -c checksum_file or other hash verification tools
- How do you encrypt a partition?
Ans: Using cryptsetup for LUKS encryption
- What command scans for vulnerabilities?
Ans: lynis or openvas
- How do you check password aging information?
Ans: chage -l username
- What command changes password expiry information?
Ans: chage options username
- How do you set up two-factor authentication?
Ans: Using google-authenticator or libpam-yubico
- What command lists system capabilities?
Ans: getcap or setcap
- How do you audit system events?
Ans: Using auditd with ausearch and aureport
- What command checks file permissions for security issues?
Ans: ls -la or specialized tools like lynis
- How do you manage firewall rules?
Ans: Using iptables, ufw, or firewall-cmd
- What command shows active authorization sessions?
Ans: loginctl list-sessions
- How do you create and manage SSL certificates?
Ans: Using openssl or certbot
- What command shows failed authentication attempts?
Ans: journalctl _COMM=sshd | grep Failed
- How do you set file access control lists?
Ans: setfacl and getfacl
Archive and Compression
- What command creates a tar archive?
Ans: tar -cf archive.tar files
- How do you extract a tar archive?
Ans: tar -xf archive.tar
- What command compresses files using gzip?
Ans: gzip filename or tar -czf archive.tar.gz files for multiple files
- How do you decompress gzip files?
Ans: gunzip filename.gz or gzip -d filename.gz
- What command creates zip archives?
Ans: zip archive.zip files
- How do you extract zip archives?
Ans: unzip archive.zip
- What command shows the contents of an archive without extracting?
Ans: tar -tf archive.tar or unzip -l archive.zip
- How do you create encrypted archives?
Ans: zip -e archive.zip files or tar -czf – files | gpg -c > archive.tar.gz.gpg
- What command compresses files with better compression than gzip?
Ans: bzip2 filename or xz filename
- How do you check the integrity of a compressed file?
Ans: gzip -t file.gz, bzip2 -t file.bz2, or unzip -t file.zip
- What command combines archiving and compression in one step?
Ans: tar -czf archive.tar.gz files (gzip) or tar -cjf archive.tar.bz2 files (bzip2)
- How do you compare archives without extracting?
Ans: diff <(tar -tf archive1.tar) <(tar -tf archive2.tar)
- What command splits a large archive into parts?
Ans: split -b size archive.tar archive.part. or using zip/tar with volume options
- How do you join split archives?
Ans: cat archive.part.* > archive.tar or using specific tools like zip
- What command shows compression ratio statistics?
Ans: gzip -l file.gz or 7z l archive.7z
- How do you convert between archive formats?
Ans: Extract and recreate using the desired format’s tools
- What command creates differential backups?
Ans: tar –listed-incremental=snapshot-file -czf backup.tar.gz directory
- How do you create incremental backups?
Ans: tar –listed-incremental=snapshot-file -czf backup.tar.gz directory
- What command performs better compression for text files?
Ans: xz typically offers better compression for text files than gzip or bzip2
- How do you password-protect archives?
Ans: zip -e archive.zip files or gpg -c archive.tar
Miscellaneous
- What command sets terminal options?
Ans: stty
- How do you create a desktop shortcut from the command line?
Ans: Create a .desktop file in ~/.local/share/applications
- What command manages system services?
Ans: systemctl or the older service
- How do you add a scheduled shutdown?
Ans: shutdown -h time where time can be +minutes or hh:mm
- What command cancels a scheduled shutdown?
Ans: shutdown -c
- How do you run a command immune to hangups?
Ans: nohup command &
- What command manages environment variables?
Ans: export VAR=value to set or unset VAR to remove
- How do you execute commands from standard input?
Ans: bash -s or using a pipe with sh
- What command sets file timestamps?
Ans: touch -t YYYYMMDDHHMM.SS filename
- How do you run a previous command?
Ans:!! for the last command or !n for command number n in history
- What command displays the manual for a command?
Ans: man command
- How do you check command syntax without executing it?
Ans: Shell-specific: bash -n script.sh or using a “dry run” mode with some commands
- What command displays a calendar?
Ans: cal or ncal
- How do you add a user to the sudoers file?
Ans: visudo and add username ALL=(ALL) ALL or add to the sudo group
- What command kills processes by name?
Ans: pkill process_name or killall process_name
- How do you substitute characters in text?
Ans: tr ‘old_chars’ ‘new_chars’ < input_file or using sed
- What command shows file type information?
Ans: file filename
- How do you set the system time and date?
Ans: date -s “YYYY-MM-DD HH:MM:SS” or timedatectl set-time “YYYY-MM-DD HH:MM:SS”
- What command displays directory sizes?
Ans: du -sh directory or du -h –max-depth=1
- How do you disown a running process?
Ans: disown %job_number or start with nohup command &