Linux Commands - Q&A Notes
1. Q: Create a file named hello.dat and write “hello world” as file contents.
A: echo "hello world" > hello.dat
Note: Creates a file hello.dat and writes "hello world" into it.
2. Q: Stop any process using PID.
A: kill <PID>
Note: Replaces <PID> with the actual process ID to stop it.
3. Q: Count number of lines in a given text file.
A: wc -l filename.txt
Note: Prints the number of lines in filename.txt.
4. Q: Create a file in a sub-directory with name abc.txt
A: mkdir -p subdir && touch subdir/abc.txt
Note: Creates subdir (if not exists) and then creates abc.txt inside it.
5. Q: List all the directories using echo command only
A: echo */
Note: Prints names of directories in the current folder.
6. Q: List all the files within a directory including hidden files
A: ls -a
Note: Lists all files including hidden ones (those starting with .).
7. Q: Display a calendar for a specific month and year
A: cal 05 2023
Note: Shows calendar for May 2023.
8. Q: What will be the output for who | more
A: who | more
Note: Displays logged-in users one screen at a time.
9. Q: What will be the output for who -a
A: who -a
Note: Shows detailed info about all logged-in users.
10. Q: What will be the output for cat file name | more ls -l > temp?
A: This command is incorrect. Possibly meant:
ls -l | tee temp | more
Note: Lists files in long format, saves to temp, and paginates output.
11. Q: Create a file name greetings.txt and write “Welcome to Linux” as file contents, then
close the file.
A: echo "Welcome to Linux" > greetings.txt
Note: Creates greetings.txt with specified content.
12. Q: Find and terminate the process with PID 5678.
A: kill 5678
Note: Stops process with PID 5678.
13. Q: Determine the number of words in a text file named data.txt.
A: wc -w data.txt
Note: Counts words in data.txt.
14. Q: Create a file name notes.txt in a sub-directory called “documents”.
A: mkdir -p documents && touch documents/notes.txt
Note: Creates documents directory and notes.txt inside it.
15. Q: Echo the name of all directories in the current location.
A: echo */
Note: Shows all directory names in current folder.
16. Q: List all files, including hidden ones, within the directory “files_archive”.
A: ls -a files_archive
Note: Lists all files, including hidden ones, in files_archive.
17. Q: Display a calendar for the month of May in the year 2023.
A: cal 05 2023
Note: Displays May 2023 calendar.
Linux Commands - Q&A Notes (Part 2)
18. Q18: What is the output of ‘ps aux | grep bash’ ?
A: ps aux | grep bash
Note: Lists processes related to "bash".
19. Q19: Retrieve the information about all users currently logged in using the ‘who -a’
command.
A: who -a
Note: Displays all login-related information of users.
20. Q20: Execute the command ‘cat file.txt | grep “pattern” | wc -l’ and interpret the output.
A: cat file.txt | grep "pattern" | wc -l
Note: Counts lines in file.txt that contain "pattern".
21. Q21: Create a symbolic link named “shortcut” to a file named “important_data.txt”.
A: ln -s important_data.txt shortcut
Note: Creates symbolic link "shortcut" to "important_data.txt".
22. Q22: Find and kill all processes associated with a user named “john_doe”.
A: pkill -u john_doe
Note: Kills all processes run by user john_doe.
23. Q23: Display the size of a file named “report.pdf” in kilobytes.
A: du -k report.pdf
Note: Displays size of report.pdf in kilobytes.
24. Q24: Archive all files in the “documents” directory into a compressed file named
“backup.tar.gz”.
A: tar -czf backup.tar.gz documents/
Note: Creates a compressed archive of the documents directory.
25. Q25: Rename a directory from “old_folder” to “new_folder”.
A: mv old_folder new_folder
Note: Renames old_folder to new_folder.
26. Q26: List all files modified in the last 24 hours in the current directory.
A: find . -type f -mtime -1
Note: Finds files modified in the last 24 hours.
27. Q27: Create a new user named “guest_user” with home directory “/home/guest” and
assign a password.
A: sudo useradd -m -d /home/guest guest_user
sudo passwd guest_user
Note: Creates user with home and sets a password.
28. Q28: Search for the word “error” in all files within the “logs” directory.
A: grep -r "error" logs/
Note: Searches recursively for "error" in logs directory.
29. Q29: Check the disk space usage of the root file system using the ‘df’ command.
A: df -h /
Note: Shows disk usage of root filesystem in human-readable format.
30. Q30: Display the process hierarchy using the ‘pstree’ command.
A: pstree
Note: Shows running processes in tree format.
31. Q31: Change the permissions of a file named “confidential.txt” to read-only for the
owner.
A: chmod 400 confidential.txt
Note: Gives read-only permission to owner.
32. Q32: Retrieve the IP address of the machine using the ‘hostname -I’ command.
A: hostname -I
Note: Displays the system’s IP address(es).
33. Q33: Redirect the output of a command to a file named ‘output.txt” and append the
result if the file already exists.
A: command >> output.txt
Note: Appends output to output.txt if it exists.
34. Q34: Find and replace all occurrences of “apple” with “orange” in a file named
“fruits.txt.”
A: sed -i "s/apple/orange/g" fruits.txt
Note: Replaces "apple" with "orange" in fruits.txt.
35. Q35: Sort the lines of a file named “numbers.txt” in descending order numerically.
A: sort -nr numbers.txt
Note: Sorts lines in numbers.txt in descending numeric order.
Linux Commands - Q&A Notes (Part 3)
36. Q36: Archive all JPEG files in the “photos” directory into a compressed file named
“pictures_backup.zip.”
A: zip pictures_backup.zip photos/*.jpg
Note: Compresses all .jpg files in photos directory into a zip file.
37. Q37: Print the last 10 lines of a file named “log.txt” using the ‘tail’ command
A: tail log.txt
Note: Displays last 10 lines of log.txt.
38. Q38: Search for files with the extension “.log” in the entire file system.
A: find / -type f -name "*.log" 2>/dev/null
Note: Finds all .log files, suppressing permission errors.
39. Q39: Extract the contents of a tarball named “archive.tar.gz” to a directory named
“extracted_data”
A: mkdir -p extracted_data && tar -xzf archive.tar.gz -C extracted_data
Note: Extracts archive.tar.gz into extracted_data folder.
40. Q40: Check the status of a network interface named “eth0” using the ‘ifconfig’ command.
A: ifconfig eth0
Note: Displays info about eth0 network interface.
41. Q41: Set an environment variable named “MY_VAR” with the value “hello_world”
A: export MY_VAR=hello_world
Note: Sets MY_VAR in current shell session.
42. Q42: List all users in the system sorted alphabetically using the ‘cut’ and ’sort’
commands.
A: cut -d: -f1 /etc/passwd | sort
Note: Extracts usernames from passwd file and sorts them.
43. Q43: Create a backup of a directory named “important_files” using the ‘’rsync”
command.
A: rsync -av important_files/ backup/
Note: Backs up important_files to backup directory.
44. Q44: Display the current date and time using the ’date’ command.
A: date
Note: Displays system date and time.
45. Q45: Count the number of files in the current directory with a “.txt” extension.
A: ls *.txt 2>/dev/null | wc -l
Note: Counts .txt files in the current directory.
46. Q46: Monitor real-time changes in a log file named “system.log” using the ‘tail -f’
command.
A: tail -f system.log
Note: Shows real-time updates to system.log.
47. Q47: Identify the file type of a document named “resume.doc” using the ‘file’ command.
A: file resume.doc
Note: Displays the type of resume.doc.
48. Q48: Find and replace a string in multiple files within a directory using the ‘sed’
command.
A: find . -type f -name "*.txt" -exec sed -i "s/old/new/g" {} +
Note: Replaces "old" with "new" in all .txt files recursively.
49. Q49: Display the top 5 CPU-consuming processes using the ‘top’ command.
A: top -b -n1 | head -n 12
Note: Shows top processes by CPU usage (approx).
50. Q50: Rename multiple files with the extension “.jpg” to have a prefix “image_”.
A: for f in *.jpg; do mv "$f" "image_$f"; done
Note: Renames each .jpg file by adding image_ prefix.
51. Q51: Display the system information, including kernel version and architecture, using
the ‘uname -a’ command.
A: uname -a
Note: Prints detailed system information.
Linux Commands - Q&A Notes (Part 4)
52. Q53: Create a compressed tarball of a directory named “project” excluding certain files.
A: tar --exclude="*.tmp" -czf project.tar.gz project/
Note: Compresses project directory excluding .tmp files.
53. Q54: Calculate the total size of a directory named “data” and its subdirectories using the
‘du’ command.
A: du -sh data
Note: Shows total size of data directory in human-readable format.
54. Q55: Identify the process ID of a running program named “my_program” using the
‘pgrep’ command.
A: pgrep my_program
Note: Displays PID(s) of processes named my_program.
55. Q56: List all installed packages
A: dpkg -l # For Debian-based
rpm -qa # For RHEL-based
Note: Lists installed packages depending on the system.
56. Q57: Create a hard link named “hardlink” to a file named “original_file.txt”
A: ln original_file.txt hardlink
Note: Creates a hard link named hardlink.
57. Q58: Monitor the system resource usage in real-time using the ‘htop’ command.
A: htop
Note: Interactive system monitor (needs to be installed).
58. Q59: Display the contents of a file named “info.txt” excluding lines containing the word
“deprecated”.
A: grep -v "deprecated" info.txt
Note: Prints all lines that do NOT contain "deprecated".
59. Q60: Determine the file system type of a partition using the ‘blkid‘ command.
A: blkid /dev/sdX1
Note: Shows filesystem type of partition (replace sdX1 with device name).
60. Q61: Check the availability of a domain using the ‘ping’ command.
A: ping domain.com
Note: Sends ICMP packets to check connectivity to domain.com.
61. Q62: List all files in the current directory and its subdirectories recursively
A: find . -type f
Note: Recursively lists all files from the current directory.
62. Q63: Change the permissions of a file to make it executable
A: chmod +x filename.sh
Note: Makes the file executable by the user.
63. Q64: Copy a directory and its contents to another location
A: cp -r sourcedir/ targetdir/
Note: Recursively copies contents of sourcedir to targetdir.
64. Q65: Display only the unique lines from a sorted file
A: sort file.txt | uniq
Note: Sorts and filters duplicate lines from file.txt.
65. Q66: Create a new empty file with the name “newfile.txt”
A: touch newfile.txt
Note: Creates a new empty file called newfile.txt.
66. Q67: Display the size of a directory and its contents in a human-readable format
A: du -sh directory/
Note: Shows total size of directory in readable units.
67. Q68: What is the purpose of the ‘tee’ command in Linux?
A: command | tee file.txt
Note: Saves output to a file and also prints it to the screen.
Linux Commands - Q&A Notes (Part 5)
68. Q69: How can you find and replace a specific word in a file using the command line?
A: sed -i "s/oldword/newword/g" filename.txt
Note: Replaces all occurrences of oldword with newword in filename.txt.
69. Q70: What is the purpose of the ‘head’ command in Linux?
A: head filename.txt
Note: Displays the first 10 lines of filename.txt.
70. Q71: How do you create a compressed tar archive of a directory without preserving the
directory structure?
A: tar -czf archive.tar.gz -C dir .
Note: Creates archive of contents without parent dir.
71. Q72: What command would you use to display the current user’s login shell?
A: echo $SHELL
Note: Prints the shell used by the current user.
72. Q73: How do you recursively delete all files with a specific extension in a directory?
A: find . -type f -name "*.log" -delete
Note: Deletes all .log files under current dir.
73. Q74: How can you list all open files and the processes that opened them?
A: lsof
Note: Lists all open files and associated processes.
74. Q75: What command would you use to remove a symbolic link named “symlink”?
A: rm symlink
Note: Removes the symbolic link named symlink.
75. Q76: How do you display the current date and time in a specific format using the ‘date’
command?
A: date "+%Y-%m-%d %H:%M:%S"
Note: Shows date/time in custom format.
76. Q77: How can you change the ownership of a file to a specific user and group?
A: chown user:group filename
Note: Changes file ownership to specified user and group.
77. Q78: What is the purpose of the ‘watch’ command in Linux?
A: watch command
Note: Runs a command repeatedly at intervals and shows output.
78. Q79: How do you display the available disk space on a specific partition?
A: df -h /dev/sda1
Note: Displays space usage on a given partition.
79. Q80: What command would you use to display the contents of a file in hexadecimal
format?
A: xxd filename
Note: Shows file content in hex (xxd may need to be installed).
80. Q81: How can you create a new user without assigning a home directory?
A: useradd -M username
Note: Creates a user without creating a home dir.
81. Q82: What is the purpose of the ‘file’ command in Linux?
A: file filename
Note: Identifies file type of the specified file.
82. Q83: How do you find files modified within the last 7 days in a specific directory?
A: find /path -type f -mtime -7
Note: Finds files modified in last 7 days.
83. Q84: What is the purpose of the ‘killall’ command in Linux?
A: killall processname
Note: Terminates all processes by name.
84. Q85: How do you list all USB devices connected to the system?
A: lsusb
Note: Lists connected USB devices.
85. Q86: What command would you use to display the detailed information about a network
interface?
A: ip addr show eth0
Note: Shows details of eth0 network interface.
86. Q87: How can you find the process ID of a running program named “my_process”?
A: pgrep my_process
Note: Displays PID(s) of my_process.
87. Q88: What is the purpose of the ‘free’ command in Linux?
A: free -h
Note: Displays memory usage in human-readable format.
88. Q89: How do you display the total number of lines in a file using the ‘wc’ command?
A: wc -l filename.txt
Note: Counts number of lines in filename.txt.
89. Q90: What command would you use to recursively copy a directory to another location?
A: cp -r sourcedir/ targetdir/
Note: Copies all contents of sourcedir to targetdir.
90. Q91: How do you create a new group named “developers”?
A: groupadd developers
Note: Creates a user group called developers.
91. Q92: What command would you use to display the current system uptime?
A: uptime
Note: Shows how long the system has been running.
92. Q93: How do you extract files from a ZIP archive using the command line?
A: unzip file.zip
Note: Extracts contents of file.zip.
93. Q94: What is the purpose of the ‘route’ command in Linux?
A: route -n
Note: Displays the routing table (deprecated in favor of ip route).
94. Q95: How can you set a specific process’s priority using the ‘renice’ command?
A: renice -n 10 -p <PID>
Note: Changes priority of a process (replace <PID> with actual one).
95. Q96: What command would you use to display the contents of a compressed file without
extracting it?
A: zcat file.gz
Note: Displays content of a compressed .gz file.
96. Q97: How do you check if a specific process is running using the command line?
A: pgrep processname
Note: Returns PID if process is running.
97. Q98: What command would you use to display the current user’s environment
variables?
A: printenv
Note: Displays environment variables.
98. Q99: How do you rename multiple files in a directory by adding a prefix “backup_”?
A: for f in *.txt; do mv "$f" "backup_$f"; done
Note: Adds backup_ prefix to each .txt file.
Linux Commands - Q&A Notes (Part 6)
99. Q105: What is the purpose of the ‘cut’ command in Linux?
A: cut -d: -f1 /etc/passwd
Note: Extracts specific columns/fields from text (e.g., usernames).
100. Q106: How can you view the contents of a file in reverse order using the command
line?
A: tac filename.txt
Note: Displays lines in reverse order (last to first).
101. Q107: What command would you use to display the available network interfaces on
the system?
A: ip link show
Note: Lists all network interfaces on the system.
102. Q108: How do you find and delete all empty files in a directory?
A: find . -type f -empty -delete
Note: Finds and deletes all empty files in current directory.
103. Q109: What is the purpose of the ‘du’ command in Linux?
A: du -sh folder/
Note: Shows folder size (disk usage).
104. Q110: How do you create a compressed tar archive of a directory and encrypt it with
a password?
A: tar -czf - folder/ | openssl enc -aes-256-cbc -out archive.tar.gz.enc
Note: Compresses and encrypts folder.
105. Q111: What command would you use to display the list of loaded kernel modules?
A: lsmod
Note: Shows all currently loaded kernel modules.
106. Q112: How can you recursively delete all empty directories in a directory?
A: find . -type d -empty -delete
Note: Finds and deletes empty folders under current directory.
107. Q113: How do you display the permissions of a file in octal format?
A: stat -c "%a" filename
Note: Shows permissions in octal (e.g., 755).
108. Q114: How do you create a new directory if it does not exist?
A: mkdir -p newdir
Note: Creates newdir only if it doesn’t exist.
109. Q115: How do you check if a number is positive, negative, or zero in a script?
A: [[ $num -gt 0 ]] && echo "Positive" || ([[ $num -lt 0 ]] && echo "Negative" || echo
"Zero")
Note: Checks and prints the type of number.
110. Q116: How to find the common elements between two arrays in Linux script?
A: comm -12 <(sort file1) <(sort file2)
Note: Finds common lines in two sorted files or arrays.
111. Q117: How to calculate the area of a circle using user input in a script?
A: area=$(echo "3.14 * $r * $r" | bc)
Note: Calculates circle area with radius r.
112. Q118: How to perform a case-insensitive search in a text file?
A: grep -i "pattern" filename
Note: Searches for pattern ignoring case.
113. Q119: How to display the current day of the week?
A: date +%A
Note: Prints full weekday name (e.g., Monday).
114. Q120: How to calculate the sum of squares of numbers in a range?
A: sum=0; for i in {1..10}; do sum=$((sum + i*i)); done; echo $sum
Note: Adds square of numbers from 1 to 10.
115. Q121: How to display the top N lines of a file?
A: head -n N filename.txt
Note: Prints first N lines of a file.
116. Q122: How to create a new file with specific name and extension?
A: touch file_name.txt
Note: Creates an empty file with given name and extension.
117. Q123: How to find the number of files with each extension in a directory?
A: find . -type f | sed -n "s/.*\.\([a-zA-Z0-9]*\)$/\1/p" | sort | uniq -c
Note: Counts files by extension.
118. Q124: How to create a log file and write system information to it?
A: uname -a > sysinfo.log
Note: Saves system info to sysinfo.log.
119. Q125: How to fetch latest stock prices in terminal (e.g., with curl)?
A: curl "https://api.example.com/stocks?symbol=XYZ"
Note: Fetches data from stock API (example only).
120. Q126: How to find largest files in a directory and subdirectories?
A: find . -type f -exec du -h {} + | sort -rh | head -n 10
Note: Lists top 10 largest files.
121. Q127: How to find failed login attempts from log files?
A: grep "Failed password" /var/log/auth.log
Note: Searches for failed login attempts.
122. Q128: How to count lines, words, and characters in a file?
A: wc filename.txt
Note: Prints line, word, and character count.
123. Q129: How to display all user activity and commands?
A: last -f /var/log/wtmp
Note: Shows login history (commands require audit setup).
124. Q130: How to search for keywords in multiple documents and generate a report?
A: grep -r "keyword" docs/ > report.txt
Note: Searches keyword and saves matches in report.txt.