Open In App
Related Articles

How to Create an Empty File in Linux | Touch Command

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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.



Last Updated : 19 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads