Linux & Shell Scripting Questions and Answers
1. What is the difference between cp, mv and rm?
cp → Copies files or directories. Original remains.
mv → Moves (or renames) files/directories. Original disappears from source.
rm → Removes (deletes) files/directories permanently.
2. How do you list files and directories?
Use the ls command.
Options:
- ls -l → detailed list
- ls -a → includes hidden files
- ls -lh → human readable sizes
3. What does the chmod command do?
Changes file or directory permissions.
Example: chmod 755 script.sh
4. Difference between absolute and relative paths?
Absolute path: full path starting from root (/).
Relative path: path relative to current directory.
5. How do you search for a string in a file using grep?
grep 'error' logfile.txt
Options: -i (ignore case), -n (line numbers), -r (recursive)
6. How do you find current working directory?
pwd
7. How do you create and remove files or folders?
Create file: touch file.txt
Remove file: rm file.txt
Create directory: mkdir dir1
Remove directory: rmdir dir1 (if empty) or rm -r dir1
8. How do you see running processes?
ps, ps aux, top, htop
9. What does the echo command do?
Displays text to terminal or writes to file.
Example: echo 'Hello' > file.txt
10. How will you check disk usage and memory usage?
Disk: df -h
Directory: du -sh folder
Memory: free -h, top
11. How do you view the contents of a file? Explain cat, head, tail, less, more.
cat file.txt → show whole file
head -n 10 file.txt → first 10 lines
tail -n 10 file.txt → last 10 lines
less file.txt → scrollable view
more file.txt → similar but less flexible
12. How do you kill a process?
Find PID: ps aux | grep name
Kill: kill -9 PID
13. What is a pipe and explain with one example?
A pipe (|) passes output of one command as input to another.
Example: grep 'error' logfile.txt | wc -l
14. How do you extract fields from a file using cut, awk, or sed?
cut -d ':' -f1 /etc/passwd
awk '{print $1, $3}' file.txt
sed -n '1,3p' file.txt
15. What is the use of xargs?
Converts input into command arguments.
Example: find . -name '*.log' | xargs rm -f
16. What is a zombie process? Explain how will you deal with it?
Zombie: finished process but entry remains in table. Appears as Z in ps.
Fix: kill/restart parent process.
17. What is a shell script?
A text file containing commands executed by a shell interpreter.
18. How do you create and run a shell script?
nano script.sh → add commands with shebang
chmod +x script.sh
./script.sh
19. What does #!/bin/bash mean?
Shebang line specifying bash as the interpreter.
20. How do you pass arguments to a shell script?
Run: ./script.sh arg1 arg2
Inside script: $1, $2, $@
21. How do you read input from the user in a shell script?
read name
echo Hello $name
22. What is the difference between $* and $@?
$* → all arguments as one string
$@ → all arguments as separate strings
23. What is the use of $? in shell scripting?
Holds exit status of last command (0=success, non-zero=failure).
24. How do you perform conditional statements (if, elif, else) in bash?
if [ condition ]; then
commands
elif [ condition ]; then
commands
else
commands
fi
25. What are loops in shell scripting? Give examples for for, while, and until
loops.
For loop: for i in 1 2 3; do echo $i; done
While loop: while [ $n -le 5 ]; do echo $n; n=$((n+1)); done
Until loop: until [ $n -gt 5 ]; do echo $n; n=$((n+1)); done
26. How do you write and call a function in shell scripting?
myfunc() { echo Hello $1; }
myfunc Prajwal