Linux Commands QA Part1
Linux Commands QA Part1
1. Q: Create a file named hello.dat and write “hello world” as file contents.
Note: Creates a file hello.dat and writes "hello world" into it.
A: kill <PID>
A: wc -l filename.txt
Note: Creates subdir (if not exists) and then creates abc.txt inside it.
A: echo */
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
A: who | more
A: who -a
10. Q: What will be the output for cat file name | more ls -l > temp?
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: kill 5678
A: echo */
16. Q: List all files, including hidden ones, within the directory “files_archive”.
A: ls -a files_archive
17. Q: Display a calendar for the month of May in the year 2023.
A: cal 05 2023
A: who -a
20. Q20: Execute the command ‘cat file.txt | grep “pattern” | wc -l’ and interpret the output.
21. Q21: Create a symbolic link named “shortcut” to a file named “important_data.txt”.
A: ln -s important_data.txt shortcut
22. Q22: Find and kill all processes associated with a user named “john_doe”.
A: pkill -u john_doe
A: du -k report.pdf
24. Q24: Archive all files in the “documents” directory into a compressed file named
“backup.tar.gz”.
26. Q26: List all files modified in the last 24 hours in the current directory.
27. Q27: Create a new user named “guest_user” with home directory “/home/guest” and
assign a password.
28. Q28: Search for the word “error” in all files within the “logs” directory.
29. Q29: Check the disk space usage of the root file system using the ‘df’ command.
A: df -h /
30. Q30: Display the process hierarchy using the ‘pstree’ command.
A: pstree
31. Q31: Change the permissions of a file named “confidential.txt” to read-only for the
owner.
A: chmod 400 confidential.txt
32. Q32: Retrieve the IP address of the machine using the ‘hostname -I’ command.
A: hostname -I
33. Q33: Redirect the output of a command to a file named ‘output.txt” and append the
result if the file already exists.
34. Q34: Find and replace all occurrences of “apple” with “orange” in a file named
“fruits.txt.”
35. Q35: Sort the lines of a file named “numbers.txt” in descending order numerically.
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
38. Q38: Search for files with the extension “.log” in the entire file system.
39. Q39: Extract the contents of a tarball named “archive.tar.gz” to a directory named
“extracted_data”
40. Q40: Check the status of a network interface named “eth0” using the ‘ifconfig’ command.
A: ifconfig eth0
41. Q41: Set an environment variable named “MY_VAR” with the value “hello_world”
A: export MY_VAR=hello_world
42. Q42: List all users in the system sorted alphabetically using the ‘cut’ and ’sort’
commands.
44. Q44: Display the current date and time using the ’date’ command.
A: date
45. Q45: Count the number of files in the current directory with a “.txt” extension.
A: ls *.txt 2>/dev/null | wc -l
46. Q46: Monitor real-time changes in a log file named “system.log” using the ‘tail -f’
command.
A: tail -f system.log
47. Q47: Identify the file type of a document named “resume.doc” using the ‘file’ command.
A: file resume.doc
48. Q48: Find and replace a string in multiple files within a directory using the ‘sed’
command.
50. Q50: Rename multiple files with the extension “.jpg” to have a prefix “image_”.
51. Q51: Display the system information, including kernel version and architecture, using
the ‘uname -a’ command.
A: uname -a
53. Q54: Calculate the total size of a directory named “data” and its subdirectories using the
‘du’ command.
A: du -sh data
54. Q55: Identify the process ID of a running program named “my_program” using the
‘pgrep’ command.
A: pgrep my_program
56. Q57: Create a hard link named “hardlink” to a file named “original_file.txt”
A: ln original_file.txt hardlink
57. Q58: Monitor the system resource usage in real-time using the ‘htop’ command.
A: htop
58. Q59: Display the contents of a file named “info.txt” excluding lines containing the word
“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
A: find . -type f
A: chmod +x filename.sh
A: cp -r sourcedir/ targetdir/
64. Q65: Display only the unique lines from a sorted file
65. Q66: Create a new empty file with the name “newfile.txt”
A: touch newfile.txt
66. Q67: Display the size of a directory and its contents in a human-readable format
A: du -sh directory/
A: head filename.txt
70. Q71: How do you create a compressed tar archive of a directory without preserving the
directory structure?
71. Q72: What command would you use to display the current user’s login shell?
A: echo $SHELL
72. Q73: How do you recursively delete all files with a specific extension in a directory?
A: lsof
74. Q75: What command would you use to remove a symbolic link named “symlink”?
A: rm symlink
75. Q76: How do you display the current date and time in a specific format using the ‘date’
command?
76. Q77: How can you change the ownership of a file to a specific user and group?
A: watch command
78. Q79: How do you display the available disk space on a specific partition?
A: df -h /dev/sda1
A: xxd filename
80. Q81: How can you create a new user without assigning a home directory?
A: useradd -M username
A: file filename
82. Q83: How do you find files modified within the last 7 days in a specific directory?
A: killall processname
84. Q85: How do you list all USB devices connected to the system?
A: lsusb
85. Q86: What command would you use to display the detailed information about a network
interface?
A: ip addr show eth0
86. Q87: How can you find the process ID of a running program named “my_process”?
A: pgrep my_process
A: free -h
88. Q89: How do you display the total number of lines in a file using the ‘wc’ command?
A: wc -l filename.txt
89. Q90: What command would you use to recursively copy a directory to another location?
A: cp -r sourcedir/ targetdir/
A: groupadd developers
91. Q92: What command would you use to display the current system uptime?
A: uptime
A: unzip file.zip
A: route -n
94. Q95: How can you set a specific process’s priority using the ‘renice’ command?
A: renice -n 10 -p <PID>
95. Q96: What command would you use to display the contents of a compressed file without
extracting it?
A: zcat file.gz
96. Q97: How do you check if a specific process is running using the command line?
A: pgrep processname
97. Q98: What command would you use to display the current user’s environment
variables?
A: printenv
100. Q106: How can you view the contents of a file in reverse order using the command
line?
A: tac filename.txt
101. Q107: What command would you use to display the available network interfaces on
the system?
A: ip link show
102. Q108: How do you find and delete all empty files in a directory?
A: du -sh folder/
105. Q111: What command would you use to display the list of loaded kernel modules?
A: lsmod
106. Q112: How can you recursively delete all empty directories in a directory?
107. Q113: How do you display the permissions of a file in octal format?
108. Q114: How do you create a new directory if it does not exist?
A: mkdir -p newdir
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")
110. Q116: How to find the common elements between two arrays in Linux script?
A: comm -12 <(sort file1) <(sort file2)
111. Q117: How to calculate the area of a circle using user input in a script?
A: date +%A
A: head -n N filename.txt
116. Q122: How to create a new file with specific name and extension?
A: touch file_name.txt
118. Q124: How to create a log file and write system information to it?
119. Q125: How to fetch latest stock prices in terminal (e.g., with curl)?
A: curl "https://api.example.com/stocks?symbol=XYZ"
121. Q127: How to find failed login attempts from log files?
A: wc filename.txt
124. Q130: How to search for keywords in multiple documents and generate a report?