The Wayback Machine - https://web.archive.org/web/20240803120537/https://www.geeksforgeeks.org/touch-command-in-linux-with-examples/
Open In App

How to Create an Empty File in Linux | Touch Command

Last Updated : 12 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows:

touch command: It is used to create a file without any content. The file created using the touch command is empty. This command can be used when the user doesn’t have data to store at the time of file creation.

Using touch Command Initially, we are in the home directory and this can be checked using the pwd command. Checking the existing files using the command ls and then long listing command(ll) is used to gather more details about existing files. As you can see in the below figure there are no existing files.

1. How to Create an Empty Files by Using Touch Command in Linux

You can create a single file at a time using touch command.

Syntax of `touch` command in Linux:

The basic syntax of the touch command is:

touch [options] file_name


The file which is created can be viewed by ls command and to get more details about the file you can use long listing command ll or ls -l command . Here file with name ‘File1‘ is created using touch command.

2. How to Create Multiple Empty Files by Using Touch Command in Linux

Touch command can be used to create multiple numbers of files at the same time. These files would be empty while creation.

Syntax:

touch File1_name File2_name File3_name 




Multiple files with name Doc1, Doc2, Doc3 are created at the same time using touch command here.

Options and Pratical Implementation of How to Create an Empty File in Linux

Options

Description

-a

This option changes the access time only.

-c

Suppresses file creation if the file does not exist.

-d

Sets the access and modification times using the specified STRING.

-m

This option changes the modification time only.

-r

Uses the access and modification times from the reference file.

Like all other command Touch command have various options. These options are very useful for various purpose.

1)`-a` option in `touch` Command to Only Update Access Time

This command is used to change access time only. To change or update the last access or modification times of a file touch -a command is used.

Syntax:

touch -a fileName




Here touch -a command changes access time of the file named Doc1.

2) Creating an Empty File Using `-c` option in `touch` Command

This command is used to check whether a file is created or not. If not created then don’t create it. This command avoids creating files.

Syntax:

touch -c fileName


3)`-c` ,`-d` option in `touch` Command

This is used to update access and modification time.

Syntax:

touch -c-d fileName


4)`-m` option in `touch` Command to Only Update Modification Time

This is used to change the modification time only. It only updates last modification time.

Syntax:

touch -m fileName


5)`-d` ” ” option in `touch` Command

This command is used to change only modification date.

touch -d "17 Mar 2023" Geek.txt


6)`-r` option in `touch` Command for Copying Timestamps from Another File

This command is used to use the timestamp of another file. Here Doc2 file is updated with the time stamp of File 1.

Syntax:

touch -r second_file_name first_file_name




7) Creating an Empty File Using `-t` option in `touch` Command

This is used to create a file using a specified time.

Syntax:

touch -t YYMMDDHHMM fileName


3. Using the `Cat` Command to Create an Empty File in Linux

The cat command is used to redirect its standard input to a file named empty_file.txt. The `>` symbol is used for output redirection.

Let’s go through the steps interactively:

  • Open your terminal.
  • Run the command:
cat > empty_file.txt

  • Press `Enter` to move to the next line.
  • Press `Ctrl + D` to signal the end of input.

After these steps, the cat command will create the file named “empty_file.txt” in the current directory with no content.

Creating empty File using Cat Command

Creating empty File using Cat Command

Here we have used `ls` command to display the content inside the directory. Then we have also used `cat` command in the last to display the content inside the file name “empty_file.txt”

4. Using The Output Redirection Operator `>` to Create an Empty File in Linux

We can create an empty file using the output redirection operator (>) followed by the filename or path to the file. Here’s the basic syntax:

> filename

If you want to specify a path for the file, you can do so as follows:

> /path/to/filename

This command essentially redirects nothing (an empty input) to the specified file, creating an empty file. It’s a concise way to achieve the same result as using touch or cat with output redirection, but it’s important to note that this method is specific to shells that support this syntax, such as Bash.

> empty_file.txt

This command creates an empty file named “empty_file.txt” in the current working directory.

Creating an empty file using `>` operator

Creating an empty file using `>` operator

Here we have used `ls` command to display the content inside the directory. Then we have also used `cat` command in the last to display the content inside the file name “empty_file.txt”

5. Using A Text Editor To Create An Empty File In Linux

We can also use text editors to create empty file in Linux . To create an empty file using the Vim text editor in Linux, follow these steps:

Open the Terminal: Open a terminal window on your Linux system. You can usually find the terminal in your applications menu or by using a keyboard shortcut (commonly Ctrl + Alt + T).

Navigate to the Desired Directory: Use the cd command to navigate to the directory where you want to create the empty file. For example, to go to the home directory, you can use:

cd ~

Open Vim: To create or edit a file using Vim, type:

vim empty_file.txt

Replace “empty_file.txt” with the desired name for your empty file.

Enter Insert Mode:In Vim, you start in command mode. To enter insert mode where you can type and edit the file, press i.

Save and Exit in Vim:

  • Once you are in insert mode, press `Esc` to return to command mode.
  • To save your changes and exit Vim, type `:wq` and press `Enter`.
    • `:` enters command-line mode.
    • `w` stands for write (save).
    • `q` stands for quit.

Creating an empty file using Vim text editor

Creating an empty file using Vim text editor

Conclusion

In this article we discussed two main ways to create empty files in Linux: using the touch and cat commands. The touch command not only updates file timestamps but also conveniently makes empty files. The article explains the syntax and options of touch, with practical examples for single and multiple file creation. It also covers alternative methods like using cat and the > operator. Additionally, it introduces using the Vim text editor for creating empty files. This guide caters to both beginners and experienced Linux users, offering a straightforward approach to effectively manage and create empty files in Linux.

How to Create an Empty File in Linux | Touch Command – FAQs

How to create an empty folder in Linux?

To create an empty folder in Linux, use the mkdir command:

mkdir foldername

This command creates a directory named foldername.

How to create an empty log file in Linux?

To create an empty log file in Linux, use the touch command:

touch logfile.log

This command creates an empty file named logfile.log.

How to create 10 files in Linux?

To create 10 files in Linux, use a loop with the touch command:

for i in {1..10}; do touch file$i.txt; done

This command creates 10 empty files named file1.txt, file2.txt, …, file10.txt.

How to write in a file in Linux?

To write to a file in Linux, you can use the echo command and redirect the output to the file:

echo "Hello, World!" > filename.txt

This command writes “Hello, World!” to filename.txt.

For interactive writing, use the cat command with redirection:Type your text and press Ctrl+D to save and exit.

How to create a new file in Linux?

To create a new file in Linux, use the touch command:

touch newfile.txt

This command creates an empty file named newfile.txt.

Alternatively, use the cat command for interactive file creation:

cat > newfile.txt

Type your text and press Ctrl+D to save and exit.



Previous Article
Next Article

Similar Reads

How to Fix "Touch: Cannot Touch 'File': File Exists"
In Linux, encountering the error "Touch: Cannot Touch 'File': File Exists" indicates that the file you're attempting to create with the touch command already exists in the specified location. This error message alerts you that the file creation operation cannot proceed because a file with the same name is already present in the directory. In this a
4 min read
How to Create a Text File Using the Command Line in Linux
There are a couple of quick ways to create a text file from the Linux Command Line or Terminal. Some of them have been explained in the following article. 1) touch commandThis is the most standard command to quickly create an empty text file. The command is quite simple to type and makes it quite easier to create several text files at once. The com
6 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Different Ways to Empty or Delete a Large File Content in Linux
In this article, we will learn different methods to empty or delete large file content in the Linux/Unix system by overwriting the existing target file with a newly created empty file. Before we proceed with the given various ways, please make sure that the file we're emptying or deleting content is not important or system file because once the con
3 min read
How to Fix - Cp: Cannot Create Regular File 'File': File Exists
In Linux, while performing copying operations using the cp command, the error "Cp: Cannot Create Regular File 'File': File Exists" may occur when attempting to copy a file to a destination where a file with the same name already exists. This error is a safeguard to prevent accidental overwriting of files and potential data loss. To resolve this iss
3 min read
file command in Linux with examples
file command is used to determine the type of a file. .file type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’). This command tests each argument in an attempt to categorize it. It has three sets of tests as follows: filesystem test: This test is based on the result which returns from a stat system cal
3 min read
How to Save Output of Command in a File in Linux?
When we run a command on a terminal on Linux it generates some output of that command. Sometimes we need the output result of the commands. Today we are going to see how to save the output of the command. Using Redirections in Linux: We can use the redirections operators to save the output of commands into files. Redirection operators redirect the
3 min read
Extract Emails From a Text File Using Grep Command in Linux
Let's consider we have a text file that contains lots of text and in that text file there are some email IDs present, and we have to find the all email IDs present in that text file. So what we can do? How can we find all email IDs present in that text file?. One way to find all email IDs manually, but this is a very time-consuming and boring proce
3 min read
How to compress file in Linux | Compress Command
Linux, renowned for its powerful command-line utilities, provides users with various tools to streamline file management tasks. One such tool, the compress `command`, is specifically designed to compress individual files, reducing their size for efficient storage and transfer. In this comprehensive guide, we'll explore the ins and outs of the compr
5 min read
three90RightbarBannerImg