linux commands
Here are questions and answers, exam-style, about all the commands in the source. Each answer includes the command's purpose, syntax, options, and an example of how to use it.
User Management
Question 1: How do you add user accounts to the system?
Answer: Use the
useraddcommand.Purpose: Adds user accounts to the system.
Syntax:
useradd [options] username.Options:
-m: Create the user's home directory if it does not exist.-G: Add the user to one or more supplementary groups.-s: Specify the login shell for the user.-d: Set the home directory.
Example:
useradd -m -G admin newuser.
Question 2: How do you modify user properties?
Answer: Use the
usermodcommand.Purpose: Modifies user properties.
Syntax:
usermod [options] username.Options:
-aG: Add user to a group.-s: Change the user’s login shell.-d: Change the user’s home directory.-L: Lock the user account.-U: Unlock the user account.
Example:
usermod -aG sudo username.
Question 3: How do you delete user accounts?
Answer: Use the
userdelcommand.Purpose: Deletes user accounts.
Syntax:
userdel [options] username.Options:
-r: Remove the user's home directory and mail spool.
Example:
userdel -r username.
Question 4: How do you change user passwords?
Answer: Use the
passwdcommand.Purpose: Changes user passwords.
Syntax:
passwd [options] username.Options:
-l: Lock the user’s password.-u: Unlock the user’s password.-e: Expire the user’s password immediately.
Example:
passwd username.
Question 5: How do you switch to another user account?
Answer: Use the
sucommand.Purpose: Switches to another user account.
Syntax:
su [options] username.Options:
-l: Start the shell as a login shell.-c: Execute a command as another user.
Example:
su -l username.
Question 6: How do you display a user's groups?
Answer: Use the
groupscommand.Purpose: Displays a user's groups.
Syntax:
groups [username].Options: None.
Example:
groups username.
Question 7: How do you find user and group names and IDs (UID or GID)?
Answer: Use the
idcommand.Purpose: Finds user and group names and IDs (UID or GID).
Syntax:
id [options] [username].Options:
-u: Display only the user ID.-g: Display only the group ID.-G: Display group IDs.
Example:
id username.
File Management
Question 8: How do you list directory contents?
Answer: Use the
lscommand.Purpose: Lists directory contents.
Syntax:
ls [options] [directory].Options:
-l: Use a long listing format.-a: Include hidden files.-h: Human-readable file sizes.
Example:
ls -la.
Question 9: How do you change the current directory?
Answer: Use the
cdcommand.Purpose: Changes the current directory.
Syntax:
cd [directory].Options: None.
Example:
cd /home/user.
Question 10: How do you display the current working directory?
Answer: Use the
pwdcommand.Purpose: Displays the current working directory.
Syntax:
pwd.Options: None.
Example:
pwd.
Question 11: How do you create directories?
Answer: Use the
mkdircommand.Purpose: Creates directories.
Syntax:
mkdir [options] directory.Options:
-p: Create parent directories if they don't exist.
Example:
mkdir newdir.
Question 12: How do you remove files or directories?
Answer: Use the
rmcommand.Purpose: Removes files or directories.
Syntax:
rm [options] file.Options:
-r: Remove directories recursively.-f: Force removal without prompting.
Example:
rm -rf directory.
Question 13: How do you copy files or directories?
Answer: Use the
cpcommand.Purpose: Copies files or directories.
Syntax:
cp [options] source destination.Options:
-r: Copy directories recursively.-i: Prompt before overwriting.
Example:
cp file.txt /home/user/.
Question 14: How do you move files or directories?
Answer: Use the
mvcommand.Purpose: Moves files or directories.
Syntax:
mv [options] source destination.Options:
-i: Prompt before overwriting.-u: Move only when the source file is newer than the destination.
Example:
mv file.txt /new/location/.
Question 15: How do you create an empty file?
Answer: Use the
touchcommand.Purpose: Creates an empty file.
Syntax:
touch [options] file.Options:
-c: Do not create any files.
Example:
touch newfile.txt.
Question 16: How do you display file content?
Answer: Use the
catcommand.Purpose: Displays file content.
Syntax:
cat [options] file.Options:
-n: Number the output lines.
Example:
cat file.txt.
Question 17: How do you find files and directories?
Answer: Use the
findcommand.Purpose: Finds files and directories.
Syntax:
find [path] [expression].Options:
-name: Search for files by name.-type: Specify the type of file (e.g.,ffor files,dfor directories).
Example:
find . -name "*.txt".
Question 18: How do you compress files?
Answer: Use the
tarcommand.Purpose: Compresses files.
Syntax:
tar [options] archive files.Options:
-c: Create an archive.-x: Extract an archive.-v: Verbose output.-f: Specify the archive file.
Example:
tar -czvf archive.tar.gz directory/.
Question 19: How do you compress files?
Answer: Use the
gzipcommand.Purpose: Compresses files.
Syntax:
gzip [options] file.Options:
-d: Decompress the file.-l: Display information about the compressed file.
Example:
gzip file.txt.
Question 20: How do you decompress files?
Answer: Use the
gunzipcommand.Purpose: Decompresses files.
Syntax:
gunzip [options] file.Options: None.
Example:
gunzip file.txt.gz.
Question 21: How do you compress files into a zip archive?
Answer: Use the
zipcommand.Purpose: Compresses files into a zip archive.
Syntax:
zip [options] archive files.Options:
-r: Recursively zip directories.-e: Encrypt the zip file.
Example:
zip archive.zip file.txt.
Question 22: How do you change file access permissions?
Answer: Use the
chmodcommand.Purpose: Changes file access permissions.
Syntax:
chmod [options] mode file.Options:
-R: Apply changes recursively to directories.
Example:
chmod +x script.sh.
Question 23: How do you change file ownership?
Answer: Use the
chowncommand.Purpose: Changes file ownership.
Syntax:
chown [options] user[:group] file.Options:
-R: Apply changes recursively.
Example:
chown user:group file.txt.
Question 24: How do you change the group ownership of a file or directory?
Answer: Use the
chgrpcommand.Purpose: Changes the group ownership of a file or directory.
Syntax:
chgrp [options] group file.Options:
-R: Apply changes recursively.
Example:
chgrp staff file.txt.
Question 25: How do you create links between files?
Answer: Use the
lncommand.Purpose: Creates links between files.
Syntax:
ln [options] target link.Options:
-s: Create a symbolic link.
Example:
ln -s /path/to/file link.txt.
Text Processing
Question 26: How do you search for a pattern in a file?
Answer: Use the
grepcommand.Purpose: Searches for a pattern in a file.
Syntax:
grep [options] pattern file.Options:
-i: Ignore case.-r: Recursively search directories.-v: Invert match (show lines that do not match).
Example:
grep -i "pattern" file.txt.
Question 27: How do you sort lines in a text file?
Answer: Use the
sortcommand.Purpose: Sorts lines in a text file.
Syntax:
sort [options] file.Options:
-r: Reverse the order.-n: Sort numerically.
Example:
sort file.txt.
Question 28: How do you manipulate data using a scripting language?
Answer: Use the
awkcommand.Purpose: A scripting language for data manipulation.
Syntax:
awk [options] 'pattern {action}' file.Options:
-F: Define the field separator.
Example:
awk '{print $1}' file.txt.
Question 29: How do you perform text substitution and manipulation?
Answer: Use the
sedcommand.Purpose: Used for text substitution and manipulation.
Syntax:
sed [options] 'expression' file.Options:
-i: Edit the file in place.-e: Specify multiple expressions.
Example:
sed 's/old/new/g' file.txt.
Question 30: How do you extract sections from lines of files?
Answer: Use the
cutcommand.Purpose: Extracts sections from lines of files.
Syntax:
cut [options] file.Options:
-d: Specify the delimiter.-f: Select fields to extract.
Example:
cut -d ',' -f1 file.txt.
Question 31: How do you translate or delete characters?
Answer: Use the
trcommand.Purpose: Translates or deletes characters.
Syntax:
tr [options] set1 set2.Options: None.
Example:
tr 'a-z' 'A-Z' < file.txt.
Question 32: How do you display a line of text?
Answer: Use the
echocommand.Purpose: Displays a line of text.
Syntax:
echo [options] text.Options:
-n: Do not output the trailing newline.
Example:
echo "Hello, world!".
Question 33: How do you count lines, words, bytes, and characters in a file?
Answer: Use the
wccommand.Purpose: Counts lines, words, bytes, and characters in a file.
Syntax:
wc [options] file.Options:
-l: Count lines.-w: Count words.-c: Count bytes.
Example:
wc -l file.txt.
Question 34: How do you print the top N lines of a file?
Answer: Use the
headcommand.Purpose: Prints the top N lines of a file.
Syntax:
head [options] file.Options:
-n: Specify the number of lines.
Example:
head -n 5 file.txt.
Question 35: How do you print the last N lines of a file?
Answer: Use the
tailcommand.Purpose: Prints the last N lines of a file.
Syntax:
tail [options] file.Options:
-n: Specify the number of lines.
Example:
tail -n 5 file.txt.
Network
Question 36: How do you configure network interfaces?
Answer: Use the
ifconfigcommand.Purpose: Configures network interfaces.
Syntax:
ifconfig [options] interface.Options:
up: Bring the interface up.down: Bring the interface down.
Example:
ifconfig eth0 up.
Question 37: How do you check network connectivity?
Answer: Use the
pingcommand.Purpose: Checks network connectivity.
Syntax:
ping [options] host.Options:
-c: Count the number of pings.-t: Set the time-to-live.
Example:
ping -c 4 google.com.
Question 38: How do you connect to a remote server securely?
Answer: Use the
sshcommand.Purpose: Connects to a remote server securely.
Syntax:
ssh [options] user@host.Options:
-p: Specify a custom port.
Example:
ssh user@remotehost.
Question 39: How do you securely copy files between servers?
Answer: Use the
scpcommand.Purpose: Securely copies files between servers.
Syntax:
scp [options] source destination.Options:
-r: Copy directories recursively.
Example:
scp file.txt user@remotehost:/path/.
Question 40: How do you download files from a server?
Answer: Use the
wgetcommand.Purpose: Downloads files from a server.
Syntax:
wget [options] URL.Options:
-O: Specify the output file name.-r: Download recursively.
Example:
wget https://example.com/file.txt.
Question 41: How do you transfer data to or from a server?
Answer: Use the
curlcommand.Purpose: Transfers data to or from a server.
Syntax:
curl [options] URL.Options:
-o: Specify output file.-L: Follow redirects.
Example:
curl -o file.txt https://example.com.
System
Question 42: How do you display file system disk space usage?
Answer: Use the
dfcommand.Purpose: Displays file system disk space usage.
Syntax:
df [options].Options:
-h: Human-readable format.
Example:
df -h.
Question 43: How do you estimate file space usage?
Answer: Use the
ducommand.Purpose: Estimates file space usage.
Syntax:
du [options] file.Options:
-sh: Display total size in human-readable format.
Example:
du -sh directory/.
Question 44: How do you mount a file system?
Answer: Use the
mountcommand.Purpose: Mounts a file system.
Syntax:
mount [options] device directory.Options:
-t: Specify the file system type.
Example:
mount /dev/sdb1 /mnt.
Question 45: How do you list running processes?
Answer: Use the
pscommand.Purpose: Lists running processes.
Syntax:
ps [options].Options:
aux: Display all running processes.
Example:
ps aux.
Question 46: How do you display a real-time view of the running system?
Answer: Use the
topcommand.Purpose: Displays a real-time view of the running system.
Syntax:
top.Options: None.
Example:
top.
Question 47: How do you display or set the system date and time?
Answer: Use the
datecommand.Purpose: Displays or sets the system date and time.
Syntax:
date [options].Options:
+FORMAT: Format the output.
Example:
date '+%Y-%m-%d %H:%M:%S'.
Question 48: How do you display a calendar?
Answer: Use the
calcommand.Purpose: Displays a calendar.
Syntax:
cal [options].Options:
-3: Display the previous, current, and next month.
Example:
cal.
Question 49: How do you shut down the system?
Answer: Use the
shutdowncommand.Purpose: Shuts down the system.
Syntax:
shutdown [options] time.Options:
-h: Halt the system.-r: Reboot after shutdown.
Example:
shutdown -h now.
Question 50: How do you restart the system?
Answer: Use the
rebootcommand.Purpose: Restarts the system.
Syntax:
reboot.Options: None.
Example:
reboot.
Question 51: How do you display system information?
Answer: Use the
unamecommand.Purpose: Displays system information.
Syntax:
uname [options].Options:
-a: Display all available system information.
Example:
uname -a.
Question 52: How do you display command history?
Answer: Use the
historycommand.Purpose: Displays command history.
Syntax:
history.Options: None.
Example:
history.
Question 53: How do you create a shortcut for a command?
Answer: Use the
aliascommand.Purpose: Creates a shortcut for a command.
Syntax:
alias name='command'.Options: None.
Example:
alias la='ls -la'.
Question 54: How do you delay execution for a specified amount of time?
Answer: Use the
sleepcommand.Purpose: Delays execution for a specified amount of time.
Syntax:
sleep time.Options: None.
Example:
sleep 10.
Question 55: How do you terminate processes?
Answer: Use the
killcommand.Purpose: Terminates processes.
Syntax:
kill [options] PID.Options:
-9: Forcefully kill the process.
Example:
kill -9 1234.
Question 56: How do you shut down the system using the init command?
Answer: Use the
initcommand.Purpose: The parent of all processes. It is the first process started by the kernel at boot, with PID 1.
Syntax:
init [options].Options:
0: Shutdown the system.6: Reboot the system.
Example:
init 0(to shut down the system).
Help and Information
Question 57: How do you display the user manual for a command?
Answer: Use the
mancommand.Purpose: Displays the user manual (man page) for a command.
Syntax:
man [options] command.Options:
-k: Search for a string in the manual page descriptions.-f: Display a short description of a command.
Example:
man ls(displays the manual for thelscommand).
Question 58: How do you find commands related to a keyword in manual pages?
Answer: Use the
aproposcommand.Purpose: Helps find commands by keyword in manual pages.
Syntax:
apropos [keyword].Options: None.
Example:
apropos disk(searches for commands related to "disk").
Question 59: How do you read documentation in info format?
Answer: Use the
infocommand.Purpose: Reads documentation in info format.
Syntax:
info [command].Options: None.
Example:
info ls(displays the info page for thelscommand).
Question 60: How do you locate the executable file associated with a command?
Answer: Use the
whichcommand.Purpose: Locates the executable file associated with a command.
Syntax:
which command.Options: None.
Example:
which ls(displays the path to thelsexecutable).
Question 61: How do you find the location of a command's source, binary, and manual sections?
Answer: Use the
whereiscommand.Purpose: Finds the location of a command's source, binary, and manual sections.
Syntax:
whereis command.Options: None.
Example:
whereis ls(shows the location of thelsbinary, source, and man page).