Learn Linux CLI Commands
For experienced candidates, acing Linux interview questions is key to standing out. Focus on Linux interview questions for 5 years experience, which often cover advanced topics like system administration, performance tuning, and security. Be prepared for detailed queries on Linux commands and practical applications. Mastering these Linux commands interview questions will highlight your expertise and increase your chances of success.
ls
: List files and directories in the current directory.
Description: Displays a list of files and directories in the current working directory.
Example:
ls
Shows the files and directories in the current directory.
cd
: Change directory.
Description: Moves to a different directory.
Example:
cd /path/to/directory
Changes the current directory to/path/to/directory
.
pwd
: Print the current working directory.
Description: Displays the full path of the current working directory.
Example:
pwd
Outputs the path of the current directory.
mkdir
: Create a new directory.
Description: Creates a new directory with the specified name.
Example:
mkdir new_directory
Creates a new directory namednew_directory
.
rm
: Remove files and directories.
Description: Deletes specified files or directories.
Example:
rm file.txt
Removes the file namedfile.txt
.
cp
: Copy files and directories.
Description: Copies files or directories from one location to another.
Example:
cp file.txt /path/to/destination
Copiesfile.txt
to/path/to/destination
.
mv
: Move or rename files and directories.
Description: Moves or renames files or directories.
Example:
mv file.txt /path/to/destination
Movesfile.txt
to/path/to/destination
.
touch
: Create an empty file.
Description: Creates a new empty file or updates the timestamp of an existing file.
Example:
touch file.txt
Creates an empty file namedfile.txt
.
cat
: Display the contents of a file.
Description: Shows the contents of a file in the terminal.
Example:
cat file.txt
Displays the contents offile.txt
.
grep
: Search for a pattern in a file.
Description: Searches for a specified pattern in a file and displays matching lines.
Example:
grep "pattern" file.txt
Finds lines containingpattern
infile.txt
.
head
: Display the first few lines of a file.
Description: Shows the beginning portion of a file.
Example:
head file.txt
Displays the first 10 lines offile.txt
.
tail
: Display the last few lines of a file.
Description: Shows the end portion of a file.
Example:
tail file.txt
Displays the last 10 lines offile.txt
.
chmod
: Change the permissions of a file or directory.
Description: Modifies file or directory permissions.
Example:
chmod 755 file.txt
Sets permissions forfile.txt
torwxr-xr-x
.
chown
: Change the ownership of a file or directory.
Description: Alters the owner and/or group of a file or directory.
Example:
chown user:group file.txt
Changes ownership offile.txt
touser
and groupgroup
.
ln
: Create a symbolic link to a file.
Description: Creates a link to a file that points to another file or directory.
Example:
ln -s /path/to/file link
Creates a symbolic link namedlink
pointing to/path/to/file
.
find
: Search for files and directories.
Description: Locates files and directories based on specified criteria.
Example:
find /path/to/search -name "pattern"
Searches for files matchingpattern
in/path/to/search
.
tar
: Archive files and directories.
Description: Combines multiple files or directories into a single archive file.
Example:
tar -czvf archive.tar.gz files/
Creates a compressed archivearchive.tar.gz
of thefiles/
directory.
unzip
: Extract files from a zip archive.
Description: Unpacks files from a zip archive.
Example:
unzip archive.zip
Extracts the contents ofarchive.zip
.
man
: Display the manual page/Help of a command.
Description: Shows the manual or help documentation for a command.
Example:
man ls
Displays the manual page for thels
command.
history
: View command history.
Description: Shows a list of previously executed commands.
Example:
history
Lists the command history for the current session.
ps
: Display currently running processes.
Description: Lists currently active processes.
Example:
ps aux
Displays detailed information about all running processes.
kill
: Terminate a process.
Description: Sends a signal to terminate a process by its process ID (PID).
Example:
kill PID
Sends a termination signal to the process with IDPID
.
df
: Display disk space usage.
Description: Shows the amount of disk space used and available on filesystems.
Example:
df -h
Displays disk space usage in a human-readable format.
du
: Estimate file and directory sizes.
Description: Displays the disk usage of files and directories.
Example:
du -sh directory
Shows the total size ofdirectory
.
scp
: Copy files between hosts securely.
Description: Securely transfers files between local and remote hosts.
Example:
scp file.txt user@remote:/path/to/destination
Copiesfile.txt
to/path/to/destination
onremote
server.
ssh
: Connect to a remote host securely.
Description: Provides a secure command-line interface to a remote host.
Example:
ssh user@host
Connects tohost
asuser
.
ping
: Send ICMP echo requests to a host.
Description: Checks connectivity to a network host.
Example:
ping google.com
Sends ping requests togoogle.com
.
ifconfig
: Display network interface information.
Description: Shows details about network interfaces.
Example:
ifconfig
Lists network interfaces and their configurations.
wget
: Download files from the web.
Description: Retrieves files from the web.
Example:
wget https://example.com/file.txt
Downloadsfile.txt
fromexample.com
.
curl
: Transfer data from or to a server.
Description: Transfers data using various protocols.
Example:
curl https://example.com
Fetches the content fromhttps://example.com
.
top
: Display system resource usage and running processes.
Description: Monitors system performance and process activity in real-time.
Example:
top
Shows real-time system resource usage.
apt-get
: Package manager for Debian-based distributions.
Description: Manages packages for Debian-based systems.
Example:
apt-get install package
Installs the specified package.
yum
: Package manager for Red Hat-based distributions.
Description: Manages packages for Red Hat-based systems.
Example:
yum install package
Installs the specified package.
systemctl
: Control system services.
Description: Manages system services usingsystemd
.
Example:
systemctl start service
Starts the specified service.
journalctl
: Query and display system logs.
Description: Views and manages logs collected bysystemd
.
Example:
journalctl -u service
Displays logs for the specified service.
sed
: Stream editor for text manipulation.
Description: Edits text streams based on patterns.
Example:
sed 's/old/new/' file.txt
Replaces occurrences ofold
withnew
infile.txt
.
awk
: Text processing and pattern scanning.
Description: Performs text processing and pattern matching.
Example:
awk '{print $1}' file.txt
Prints the first column offile.txt
.
sort
: Sort lines of text files.
Description: Arranges lines of text files in order.
Example:
sort file.txt
Sorts the lines offile.txt
alphabetically.
uniq
: Report or omit repeated lines.
Description: Removes duplicate lines from sorted files.
Example:
uniq file.txt
Removes adjacent duplicate lines fromfile.txt
.
gzip
: Compress files.
Description: Reduces the size of files using thegzip
compression format.
Example:
gzip file.txt
Compressesfile.txt
intofile.txt.gz
.
gunzip
: Decompress files.
Description: Expands files compressed withgzip
.
Example:
gunzip file.txt.gz
Decompressesfile.txt.gz
tofile.txt
.
ssh-keygen
: Generate SSH keys.
Description: Creates a new SSH key pair for secure connections.
Example:
ssh-keygen -t rsa
Generates a new RSA SSH key pair.
ssh-copy-id
: Copy SSH public key to a remote host.
Description: Adds the SSH public key to the remote host’s authorized keys.
Example:
ssh-copy-id user@host
Copies the public key tohost
for useruser
.
mount
: Mount a file system.
Description: Attaches a filesystem to the system’s file hierarchy.
Example:
mount /dev/sdb1 /mnt
Mounts the device/dev/sdb1
at the/mnt
directory.
umount
: Unmount a file system.
Description: Detaches a filesystem from the file hierarchy.
Example:
umount /mnt
Unmounts the filesystem mounted at/mnt
.
lsblk
: List block devices.
Description: Displays information about block devices.
Example:
lsblk
Lists all block devices and their mount points.
fdisk
: Partition table manipulator for disks.
Description: Manages disk partitions.
Example:
fdisk /dev/sdb
Opens the partition editor for disk/dev/sdb
.
date
: Display the current date and time.
Description: Shows or sets the system date and time.
Example:
date
Outputs the current date and time.
echo
: Print text or variables.
Description: Outputs text or variable values to the terminal.
Example:
echo "Hello, World!"
PrintsHello, World!
to the terminal.
tee
: Read from standard input and write to standard output and files.
Description: Directs output to both the terminal and a file.
Example:
echo "Hello" | tee file.txt
WritesHello
to both the terminal andfile.txt
.
nc
: Read and write data across network connections.
Description: Provides network connectivity for reading and writing data.
Example:
echo "Hello" | nc host port
SendsHello
tohost
onport
.
basename
: Strip directory and suffix from filenames.
Description: Extracts the filename from a path.
Example:
basename /path/to/file.txt
Returnsfile.txt
from the path.
dirname
: Strip the last component from file names.
Description: Extracts the directory path from a filename.
Example:
dirname /path/to/file.txt
Returns/path/to
from the path.
uptime
: Display system uptime.
Description: Shows how long the system has been running.
Example:
uptime
Displays the system’s uptime and load averages.
whoami
: Print the current username.
Description: Shows the username of the currently logged-in user.
Example:
whoami
Outputs the current username.
su
: Switch user.
Description: Changes the current user to another user.
Example:
su username
Switches to the userusername
.
sudo
: Execute a command as a superuser.
Description: Runs commands with superuser privileges.
Example:
sudo command
Executescommand
with root privileges.
useradd
: Create a new user account.
Description: Adds a new user account to the system.
Example:
useradd username
Creates a new user namedusername
.
passwd
: Change user password.
Description: Updates the password for a user account.
Example:
passwd username
Changes the password forusername
.
groupadd
: Create a new group.
Description: Adds a new group to the system.
Example:
groupadd groupname
Creates a new group namedgroupname
.
usermod
: Modify user account.
Description: Alters user account settings.
Example:
usermod -aG groupname username
Addsusername
togroupname
.
groups
: Display group membership for a user.
Description: Lists the groups a user belongs to.
Example:
groups username
Shows the groups ofusername
.
chgrp
: Change group ownership of a file or directory.
Description: Changes the group ownership of a file or directory.
Example:
chgrp groupname file.txt
Changes the group ownership offile.txt
togroupname
.
wc
: Count lines, words, and characters in files.
Description: Counts lines, words, and characters in a file.
Example:
wc file.txt
Shows the number of lines, words, and characters infile.txt
.
free
: Display free and used memory in the system.
Description: Shows memory usage and availability.
Example:
free -h
Displays memory usage in a human-readable format.
whereis
: Locate the binary, source, and manual page files for a command.
Description: Finds the location of a command’s binary, source, and manual pages.
Example:
whereis ls
Provides locations ofls
binary, source, and manual.
cmp
: Compare two files byte by byte.
Description: Compares two files byte by byte and reports differences.
Example:
cmp file1.txt file2.txt
Comparesfile1.txt
andfile2.txt
byte by byte.
diff
: Compare files line by line.
Description: Shows differences between two files line by line.
Example:
diff file1.txt file2.txt
Displays line-by-line differences betweenfile1.txt
andfile2.txt
.
shutdown
: Shutdown or restart the system.
Description: Powers off or restarts the system.
Example:
shutdown -h now
Immediately shuts down the system.
reboot
: Reboot the system.
Description: Restarts the system.
Example:
reboot
Reboots the system immediately.
ip
: Show or manipulate routing, devices, policy routing, and tunnels.
Description: Provides detailed network configuration and management.
Example:
ip addr show
Displays information about network interfaces and their addresses.
traceroute
: Print the route packets take to a network host.
Description: Traces the path packets take to reach a network host.
Example:
traceroute google.com
Shows the path packets take togoogle.com
.
netstat
: Print network connections, routing tables, and interface statistics.
Description: Displays network connections and interface statistics.
Example:
netstat -tuln
Shows network connections and listening ports.
lsof
: List open files.
Description: Displays information about files opened by processes.
Example:
lsof
Lists all open files and their associated processes.
strace
: Trace system calls and signals.
Description: Monitors system calls and signals used by a process.
Example:
strace command
Traces system calls and signals forcommand
.
perf
: Performance analysis tool.
Description: Provides performance monitoring and analysis.
Example:
perf top
Displays a real-time performance monitoring view.
tmux
: Terminal multiplexer.
Description: Allows multiple terminal sessions within a single window.
Example:
tmux
Starts a new tmux session.
screen
: Terminal multiplexer.
Description: Manages multiple terminal sessions in one window.
Example:
screen
Starts a new screen session.
chmod
: Change file modes or Access Control Lists.
Description: Modifies the permissions of files or directories.
Example:
chmod 644 file.txt
Sets read and write permissions for the owner, and read-only permissions for others.
stat
: Display file or file system status.
Description: Shows detailed information about a file or filesystem.
Example:
stat file.txt
Provides detailed status information forfile.txt
.
file
: Determine file type.
Description: Identifies the type of a file based on its content.
Example:
file file.txt
Displays the file type offile.txt
.
alias
: Create or view command aliases.
Description: Defines shortcuts for longer commands.
Example:
alias ll='ls -la'
Creates an aliasll
forls -la
.
unalias
: Remove command aliases.
Description: Deletes previously defined command aliases.
Example:
unalias ll
Removes the aliasll
.
export
: Set environment variables.
Description: Defines environment variables for the current shell session.
Example:
export PATH=$PATH:/new/path
Adds/new/path
to thePATH
environment variable.
env
: Display or set environment variables.
Description: Shows or sets environment variables.
Example:
env
Lists all environment variables.
printenv
: Print environment variables.
Description: Displays the value of environment variables.
Example:
printenv PATH
Shows the value of thePATH
variable.
source
: Execute commands from a file in the current shell.
Description: Runs commands from a file in the current shell environment.
Example:
source ~/.bashrc
Reloads the.bashrc
file in the current shell session.
set
: Set or unset shell options and positional parameters.
Description: Configures shell options and parameters.
Example:
set -x
Enables debugging mode in the shell.
unset
: Unset shell variables or functions.
Description: Removes shell variables or functions.
Example:
unset VAR
Deletes the shell variableVAR
.
xargs
: Build and execute command lines from standard input.
Description: Constructs and executes command lines from input.
Example:
find . -name '*.txt' | xargs rm
Deletes all.txt
files found in the current directory and subdirectories.
crontab
: Schedule periodic tasks.
Description: Manages cron jobs for scheduled tasks.
Example:
crontab -e
Edits the cron table for scheduling tasks.
at
: Schedule one-time tasks.
Description: Schedules tasks to be run once at a specified time.
Example:
at 3pm
Enters theat
command prompt to schedule a task for 3 PM.
jobs
: List active jobs.
Description: Displays jobs running in the background or stopped.
Example:
jobs
Lists active jobs in the current shell.
bg
: Resume a suspended job in the background.
Description: Continues a job in the background.
Example:
bg %1
Resumes job number 1 in the background.
fg
: Bring a background job to the foreground.
Description: Moves a background job to the foreground.
Example:
fg %1
Brings job number 1 to the foreground.
killall
: Terminate processes by name.
Description: Sends a signal to terminate all processes with a specific name.
Example:
killall processname
Kills all processes namedprocessname
.
pkill
: Send signals to processes based on name and other attributes.
Description: Sends a signal to processes matching criteria.
Example:
pkill -f pattern
Kills processes matchingpattern
.
ps aux
: List all running processes with detailed information.
Description: Provides a detailed list of all processes running on the system.
Example:
ps aux
Lists all processes with detailed information.
df -T
: Show filesystem types.
Description: Displays disk usage and filesystem types for mounted filesystems.
Example:
df -T
Shows filesystem types and disk usage.
uptime
: Show how long the system has been running.
Description: Displays the system’s uptime, number of users, and load averages.
Example:
uptime
Shows how long the system has been up and its load averages.