The touch command creates an empty file if it does not exist or updates the file's last modified time if it does exist.
Click to reveal answer
beginner
How do you create multiple empty files at once using touch?
You list all the file names separated by spaces after touch. For example: touch file1.txt file2.txt file3.txt creates three empty files.
Click to reveal answer
beginner
What happens if you use touch on a file that already exists?
The file's last access and modification timestamps are updated to the current time, but the file content stays the same.
Click to reveal answer
intermediate
How can you use touch to create a file with a specific timestamp?
Use the -t option followed by the timestamp in the format [[CC]YY]MMDDhhmm[.ss]. For example: touch -t 202401011200 file.txt sets the file's timestamp to Jan 1, 2024, 12:00.
Click to reveal answer
beginner
Why might you use touch in scripting or automation?
You can create placeholder files, update timestamps to trigger other processes, or ensure files exist before running commands that need them.
Click to reveal answer
What does the command touch newfile.txt do if newfile.txt does not exist?
ARenames newfile.txt
BDeletes newfile.txt
CDisplays the contents of newfile.txt
DCreates an empty file named newfile.txt
✗ Incorrect
touch creates an empty file if it does not exist.
If file.txt exists, what does touch file.txt do?
AUpdates the file's last modified time
BDeletes the file
CChanges file permissions
DCopies the file
✗ Incorrect
touch updates the file's timestamps without changing its content.
How can you create three empty files named a.txt, b.txt, and c.txt in one command?
Arm a.txt b.txt c.txt
Btouch a.txt b.txt c.txt
Cmkdir a.txt b.txt c.txt
Dcat a.txt b.txt c.txt
✗ Incorrect
Listing multiple file names after touch creates all of them.
Which option with touch lets you set a custom timestamp?
A-c
B-r
C-t
D-a
✗ Incorrect
The -t option sets a specific timestamp for the file.
Why might a script use touch before running another command?
ATo create or update files needed by the command
BTo delete files
CTo change file ownership
DTo compress files
✗ Incorrect
touch ensures files exist or updates timestamps to trigger other actions.
Explain how the touch command works and give two common uses.
Think about creating files and changing file times.
You got /4 concepts.
Describe how to use touch to set a file's timestamp to a specific date and time.
Look for the option that controls time.
You got /3 concepts.
Practice
(1/5)
1. What does the touch command do in Linux?
easy
A. Copies a file to another location
B. Deletes a file permanently
C. Creates an empty file or updates the timestamp of an existing file
D. Displays the contents of a file
Solution
Step 1: Understand the purpose of touch
The touch command is used to create empty files or update the modification time of existing files.
Step 2: Compare with other commands
Deleting files is done with rm, copying with cp, and displaying contents with cat. None of these are touch.
Final Answer:
Creates an empty file or updates the timestamp of an existing file -> Option C
Quick Check:
touch = create/update file time [OK]
Hint: Remember: touch makes or updates files fast [OK]
Common Mistakes:
Confusing touch with delete or copy commands
Thinking touch shows file content
Assuming touch edits file content
2. Which of the following is the correct syntax to create an empty file named notes.txt using touch?
easy
A. touch notes.txt
B. touch -c notes.txt
C. touch > notes.txt
D. touch -r notes.txt
Solution
Step 1: Identify the basic syntax of touch
The basic command to create or update a file is touch filename. So touch notes.txt is correct.
Step 2: Understand other options
-c prevents creating a file if it doesn't exist, > is shell redirection (not touch syntax), and -r copies timestamps from another file.
Final Answer:
touch notes.txt -> Option A
Quick Check:
Basic touch syntax = touch filename [OK]
Hint: Use simple: touch filename to create empty file [OK]
Common Mistakes:
Using shell redirection instead of touch syntax
Adding unnecessary options like -c or -r
Confusing touch options with other commands
3. What will be the output of the following commands?
touch file1.txt file2.txt
ls -l file1.txt file2.txt
medium
A. Creates only file1.txt and ignores file2.txt
B. Shows an error because multiple files cannot be created with touch
C. Deletes file1.txt and file2.txt
D. Lists details of file1.txt and file2.txt, both created as empty files
Solution
Step 1: Understand touch with multiple files
The touch command can create multiple files at once by listing them separated by spaces.
Step 2: Check ls -l output
The ls -l command lists detailed info of the files, showing their size (0 bytes if empty) and timestamps.
Final Answer:
Lists details of file1.txt and file2.txt, both created as empty files -> Option D
Quick Check:
touch multiple files = creates all listed [OK]
Hint: touch can create many files at once by listing them [OK]
Common Mistakes:
Thinking touch only creates one file at a time
Expecting an error with multiple filenames
Confusing touch with delete or move commands
4. You run touch newfile.txt but get an error: touch: cannot touch 'newfile.txt': Permission denied. What is the likely cause?
medium
A. You do not have write permission in the directory
B. The file newfile.txt already exists
C. The touch command is misspelled
D. The disk is full
Solution
Step 1: Analyze the error message
The error says "Permission denied" which means the user lacks rights to create or modify files in the directory.
Step 2: Check other options
If the file exists, touch updates timestamp without error. Misspelling touch causes command not found, not permission denied. Disk full causes different errors.
Final Answer:
You do not have write permission in the directory -> Option A
Quick Check:
Permission denied = no write access [OK]
Hint: Permission denied means no write rights in folder [OK]
5. You want to create empty files named log1.txt, log2.txt, ..., log5.txt quickly using a single command. Which command achieves this?
hard
A. touch log*.txt
B. touch log{1..5}.txt
C. touch log1.txt log2.txt log3.txt log4.txt log5.txt
D. touch log[1-5].txt
Solution
Step 1: Understand brace expansion
The syntax log{1..5}.txt uses brace expansion to create multiple filenames from 1 to 5.
Step 2: Evaluate other options
log[1-5].txt matches single characters in brackets, not ranges. Listing all files manually works but is long. log*.txt matches existing files, not create new ones.
Final Answer:
touch log{1..5}.txt -> Option B
Quick Check:
Brace expansion creates multiple files fast [OK]
Hint: Use brace expansion: touch file{1..5}.ext [OK]
Common Mistakes:
Using square brackets instead of braces for ranges
Typing all filenames manually instead of using expansion
Using wildcard which matches existing files only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.