0
0
Linux CLIscripting~3 mins

touch (create empty files) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to create a new empty file quickly or update the timestamp of an existing file. The touch command helps you do this easily from the command line without opening any editor.
When you want to create a new empty file to start writing content later.
When you need to update the last modified time of a file without changing its content.
When a script requires a file to exist before running but you don't have the content yet.
When you want to create multiple empty files at once for organizing your project.
When you want to reset the timestamp of a file to the current time for tracking purposes.
Commands
This command creates a new empty file named example.txt in the current directory. If the file already exists, it updates its last modified time to the current time.
Terminal
touch example.txt
Expected OutputExpected
No output (command runs silently)
This command lists the details of example.txt so you can verify that the file exists and see its timestamp.
Terminal
ls -l example.txt
Expected OutputExpected
-rw-r--r-- 1 user user 0 Jun 10 12:00 example.txt
-l - Shows detailed information including permissions, owner, size, and timestamp.
This command creates three empty files named file1.txt, file2.txt, and file3.txt at once.
Terminal
touch file1.txt file2.txt file3.txt
Expected OutputExpected
No output (command runs silently)
This command tries to update the timestamp of missing.txt but does not create it if it does not exist, because of the -c flag.
Terminal
touch -c missing.txt
Expected OutputExpected
No output (command runs silently)
-c - Do not create any files; only update existing ones.
Key Concept

If you remember nothing else from this pattern, remember: touch creates empty files or updates timestamps without changing file content.

Common Mistakes
Expecting touch to add content to the file.
Touch only creates empty files or updates timestamps; it does not write any text.
Use a text editor or echo command to add content after creating the file.
Using touch with a filename that does not exist and expecting no file to be created.
By default, touch creates the file if it does not exist.
Use the -c flag to avoid creating new files when updating timestamps.
Not verifying the file creation with ls or similar commands.
You might think the file was created but it might be in a different directory or not created due to permissions.
Always check with ls -l or similar commands to confirm file creation.
Summary
Use touch filename to create an empty file or update its timestamp.
Use touch with multiple filenames to create several empty files at once.
Use touch -c filename to update timestamps without creating new files.
Verify file creation or timestamp update with ls -l filename.