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 useradd command.

    • 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 usermod command.

    • 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 userdel command.

    • 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 passwd command.

    • 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 su command.

    • 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 groups command.

    • 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 id command.

    • 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 ls command.

    • 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 cd command.

    • 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 pwd command.

    • Purpose: Displays the current working directory.

    • Syntax: pwd.

    • Options: None.

    • Example: pwd.

  • Question 11: How do you create directories?

    Answer: Use the mkdir command.

    • 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 rm command.

    • 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 cp command.

    • 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 mv command.

    • 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 touch command.

    • 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 cat command.

    • 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 find command.

    • Purpose: Finds files and directories.

    • Syntax: find [path] [expression].

    • Options:

      • -name: Search for files by name.

      • -type: Specify the type of file (e.g., f for files, d for directories).

    • Example: find . -name "*.txt".

  • Question 18: How do you compress files?

    Answer: Use the tar command.

    • 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 gzip command.

    • 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 gunzip command.

    • 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 zip command.

    • 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 chmod command.

    • 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 chown command.

    • 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 chgrp command.

    • 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 ln command.

    • 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 grep command.

    • 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 sort command.

    • 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 awk command.

    • 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 sed command.

    • 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 cut command.

    • 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 tr command.

    • 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 echo command.

    • 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 wc command.

    • 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 head command.

    • 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 tail command.

    • 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 ifconfig command.

    • 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 ping command.

    • 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 ssh command.

    • 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 scp command.

    • 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 wget command.

    • 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 curl command.

    • 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 df command.

    • 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 du command.

    • 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 mount command.

    • 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 ps command.

    • 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 top command.

    • 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 date command.

    • 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 cal command.

    • 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 shutdown command.

    • 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 reboot command.

    • Purpose: Restarts the system.

    • Syntax: reboot.

    • Options: None.

    • Example: reboot.

  • Question 51: How do you display system information?

    Answer: Use the uname command.

    • 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 history command.

    • Purpose: Displays command history.

    • Syntax: history.

    • Options: None.

    • Example: history.

  • Question 53: How do you create a shortcut for a command?

    Answer: Use the alias command.

    • 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 sleep command.

    • 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 kill command.

    • 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 init command.

    • 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 man command.

    • 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 the ls command).

  • Question 58: How do you find commands related to a keyword in manual pages?

    Answer: Use the apropos command.

    • 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 info command.

    • Purpose: Reads documentation in info format.

    • Syntax: info [command].

    • Options: None.

    • Example: info ls (displays the info page for the ls command).

  • Question 60: How do you locate the executable file associated with a command?

    Answer: Use the which command.

    • Purpose: Locates the executable file associated with a command.

    • Syntax: which command.

    • Options: None.

    • Example: which ls (displays the path to the ls executable).

  • Question 61: How do you find the location of a command's source, binary, and manual sections?

    Answer: Use the whereis command.

    • 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 the ls binary, source, and man page).