Linux Terminal Commands Practice Questions (Ubuntu and Bash)
Practice questions on common Linux and Ubuntu terminal commands (pwd, cd, ls, and more), with explanations for every answer.
Questions
What does pwd do?
- Prints the current directory's path
- Changes the password for the current user
- Lists the files in the current directory
- Prints the contents of a file
You are in /home/student. What does this output? $ cd projects/site; pwd
- /home/student/projects/site
- projects/site
- /projects/site
- /home/student
You are in /home/student. What does this output? $ cd projects/site; cd ../..; pwd
- /home/student
- /home/student/projects
- /home
- /
You are in /home/student. What does this output? $ cd /tmp; cd ~; pwd
- /home/student
- /tmp
- /
- ~
Which of these is an absolute path?
- /home/student/docs
- docs/readme.md
- ../docs
- ./docs
What does ~ stand for in a path?
- Your home directory
- The root of the file system
- The previous directory you were in
- The current directory
What does cd / do?
- Goes to the root, /
- Moves to your home directory
- Moves up one directory
- Moves to the last directory you used
In a path, what does .. mean?
- The parent directory
- The current directory
- The home directory
- A hidden file
Your home contains docs/, projects/, notes.txt, report.txt, and a hidden file named .hidden. You are in /home/student. What does this output? $ ls | wc -l
- 4
- 5
- 2
- 3
Your home contains a hidden file named .hidden. You are in /home/student. What does this output? $ ls -a | grep hidden
- .hidden
- Nothing
- hidden
- ls: invalid option
What does ls -l add to a normal listing?
- Permissions, owner, size, and date
- Only the hidden files in the directory
- A list sorted from largest to smallest file
- The full path of each file
projects/site contains index.html and style.css. You are in /home/student. What does this output? $ ls projects/site | wc -l
- 2
- 1
- 3
- 0
You type cd Doc and press Tab. What happens?
- The shell completes the name, such as Documents
- The shell deletes the folder
- The shell lists every command on the system
- Nothing; Tab does nothing in a terminal
In Linux, Notes.txt and notes.txt are two different files.
Linux commands are case-sensitive, so LS is not the same command as ls.
Which command prints the full path of the directory you are in?
You are in /home/student. What does this output? $ mkdir -p a/b/c; ls a/b
- c
- a/b/c
- b
- mkdir: cannot create directory
You are in /home/student, which has no folder named x. What happens? $ mkdir x/y
- An error: No such file or directory
- It creates both x and y
- It creates only x
- Nothing; it silently does nothing
You are in /home/student. What does this output? $ touch new.txt; ls | grep new
- new.txt
- Nothing
- touch: missing operand
- new
notes.txt holds six lines: apple, banana, apple, cherry, banana, apple. You are in /home/student. What does this output? $ cp notes.txt copy.txt; wc -l copy.txt
- 6 copy.txt
- 0 copy.txt
- 6 notes.txt
- 12 copy.txt
notes.txt holds six lines: apple, banana, apple, cherry, banana, apple. You are in /home/student. What does this output? $ mv notes.txt list.txt; ls | grep -c notes
- 0
- 1
- 2
- list.txt
projects/ contains a folder named site with two files. What happens? $ rmdir projects
- An error: Directory not empty
- projects and everything in it are deleted
- Only the empty folders inside projects are deleted
- projects is moved to the trash
You are in /home/student. What does this output? $ echo hi > a.txt; cp a.txt b.txt; echo bye > a.txt; cat b.txt
- hi
- bye
- hi bye
- Nothing
Besides moving files, what else can mv do?
- Rename a file
- Copy a file to a new folder
- Delete a file permanently
- Change a file's permissions
Which command deletes a folder named old and everything inside it?
- rm -r old
- rmdir old
- rm old
- del old
Which command copies the folder site and everything in it to backup?
- cp -r site backup
- cp site backup
- mv -r site backup
- copy site backup
Why is sudo rm -rf / considered extremely dangerous?
- It force-deletes everything from the root down
- It only clears the screen
- It removes the terminal program
- It deletes the current user's password
Files deleted with rm go to a trash can where they can be restored.
Which command creates a new, empty file (one word)?
Which command creates a new directory?
Which commands can delete something?
- rm
- rmdir
- touch
- ls
- cat
- chmod -x
notes.txt holds six lines: apple, banana, apple, cherry, banana, apple. You are in /home/student. What does this output? $ head -n 2 notes.txt
- apple banana
- apple apple
- banana apple
- apple
report.txt holds four lines: Error: disk full / warning: low memory / Error: timeout / info: started. You are in /home/student. What does this output? $ tail -n 1 report.txt
- info: started
- Error: disk full
- Error: timeout
- warning: low memory
notes.txt holds six lines: apple, banana, apple, cherry, banana, apple. You are in /home/student. What does this output? $ wc -l notes.txt
- 6 notes.txt
- 6
- 3 notes.txt
- 36 notes.txt
notes.txt holds six lines: apple, banana, apple, cherry, banana, apple. You are in /home/student. What does this output? $ cat notes.txt | wc -l
- 6
- 6 notes.txt
- 3
- 1
You are in /home/student. What does this output? $ echo one > a.txt; echo two > b.txt; cat a.txt b.txt
- one two
- two one
- one
- onetwo
You open a long file with less. How do you quit?
- Press q
- Press Ctrl+S
- Type exit and press Enter
- Close and reopen the terminal
Which command watches a log file and shows new lines as they are added?
- tail -f app.log
- head -f app.log
- cat -w app.log
- less -n app.log
How many lines does head show if you don't give a number?
nano is a text editor that runs inside the terminal.
What does wc stand for?
- Word count
- Write copy
- Web connect
- Wildcard
notes.txt holds six lines: apple, banana, apple, cherry, banana, apple. You are in /home/student. What does this output? $ grep apple notes.txt | wc -l
- 3
- 1
- 6
- 2
report.txt holds four lines: Error: disk full / warning: low memory / Error: timeout / info: started. You are in /home/student. What does this output? $ grep -c Error report.txt
- 2
- 1
- 4
- 3
report.txt holds four lines: Error: disk full / warning: low memory / Error: timeout / info: started. You are in /home/student. What does this output? $ grep error report.txt | wc -l
- 0
- 2
- 1
- 4
notes.txt holds six lines: apple, banana, apple, cherry, banana, apple. You are in /home/student. What does this output? $ grep -v apple notes.txt | wc -l
- 3
- 0
- 6
- 1
notes.txt holds six lines: apple, banana, apple, cherry, banana, apple. You are in /home/student. What does this output? $ grep -n cherry notes.txt
- 4:cherry
- cherry
- 1:cherry
- cherry:4
projects/site contains index.html and style.css. You are in /home/student. What does this output? $ find . -name "*.css"
- ./projects/site/style.css
- style.css
- projects/site
- ./style.css
What is the difference between grep and find?
- grep searches inside files; find searches for files
- They do exactly the same thing
- grep only works on folders
- find searches inside files; grep finds file names
Which option makes grep ignore uppercase and lowercase differences?
- -i
- -c
- -v
- -n
Which commands are used to find files by name?
- find
- locate
- grep
- cat
- grep -r