Bird
Raised Fist0
Linux CLIscripting~10 mins

touch (create empty files) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - touch (create empty files)
Start
↓
Run touch command
↓
Check if file exists
↓
Update timestamp if file exists
↓
Create empty file
↓
End
The touch command checks if a file exists. If yes, it updates the timestamp. If no, it creates an empty file.
Execution Sample
Linux CLI
touch file1.txt
ls -l file1.txt
Creates an empty file named file1.txt if it doesn't exist, then lists its details.
Execution Table
StepCommandActionResultOutput
1touch file1.txtCheck if file1.txt existsNoNo output
2touch file1.txtCreate empty file file1.txtSuccessNo output
3ls -l file1.txtList file detailsSuccess-rw-r--r-- 1 user user 0 date time file1.txt
4touch file1.txtFile exists, update timestampSuccessNo output
5ls -l file1.txtList file details with updated timestampSuccess-rw-r--r-- 1 user user 0 date updated_time file1.txt
💡 Command ends after file creation or timestamp update with no errors.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
file1.txt existenceNoNoYes (empty file created)Yes (timestamp updated)Yes (timestamp updated)
Key Moments - 3 Insights
Why does touch not show any output when creating a file?
Because touch is designed to silently create or update files without printing messages, as seen in execution_table steps 1 and 2.
What happens if the file already exists when using touch?
Touch updates the file's timestamp without changing its content, shown in execution_table step 4.
Does touch add any content to the file when creating it?
No, touch creates an empty file with zero bytes, as shown by the file size 0 in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the file size of file1.txt after step 3?
AUnknown
B1 byte
C0 bytes
DFile does not exist
💡 Hint
Check the output column in step 3 showing file details including size.
At which step does touch update the timestamp of an existing file?
AStep 4
BStep 2
CStep 1
DStep 3
💡 Hint
Look for the action describing timestamp update in the execution_table.
If file1.txt already exists, what will touch do according to the execution_table?
ACreate a new file with the same name
BUpdate the file's timestamp
CDelete the file
DPrint an error message
💡 Hint
Refer to step 4 in the execution_table where the file exists.
Concept Snapshot
touch filename

- Creates an empty file if it does not exist.
- If file exists, updates its last modified timestamp.
- No output is shown on success.
- Useful for creating or refreshing files quickly.
Full Transcript
The touch command in Linux is used to create an empty file or update the timestamp of an existing file. When you run 'touch file1.txt', the system checks if the file exists. If it does not, touch creates an empty file with zero bytes. If the file exists, touch updates its last modified timestamp without changing the content. The command runs silently without output. You can verify the file creation or timestamp update by listing the file details with 'ls -l file1.txt'. This command is simple and useful for quickly creating or refreshing files.

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

  1. Step 1: Understand the purpose of touch

    The touch command is used to create empty files or update the modification time of existing files.
  2. 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.
  3. Final Answer:

    Creates an empty file or updates the timestamp of an existing file -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    touch notes.txt -> Option A
  4. 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

  1. Step 1: Understand touch with multiple files

    The touch command can create multiple files at once by listing them separated by spaces.
  2. 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.
  3. Final Answer:

    Lists details of file1.txt and file2.txt, both created as empty files -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    You do not have write permission in the directory -> Option A
  4. Quick Check:

    Permission denied = no write access [OK]
Hint: Permission denied means no write rights in folder [OK]
Common Mistakes:
  • Assuming file existence causes permission error
  • Thinking touch command typo causes permission error
  • Ignoring directory permissions
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

  1. Step 1: Understand brace expansion

    The syntax log{1..5}.txt uses brace expansion to create multiple filenames from 1 to 5.
  2. 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.
  3. Final Answer:

    touch log{1..5}.txt -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard