5 most used touch commands in linux with examples

5 Most Used Touch Commands in Linux With Examples

Spread the love

In this tutorial, we are going to show you the most used touch commands in Linux with examples.

The touch command is a standard command in Linux operating system used on daily basis by the system administrators, developers, and other users familiar with the Linux command line. With the touch command, we can easily create files, empty files, modify files, timestamps and etc.

In this tutorial, we will execute the touch commands on Ubuntu 20.04 OS, but you can choose any Linux distro. Le’ts get started!

Prerequisites

  • A server with Ubuntu 20.04 OS
  • User privileges: root or non-root user with sudo privileges

Update the System

If you have a fresh installation of Ubuntu 20.04, it is recommended to update the system packages to the latest versions available.

sudo apt update -y && sudo apt upgrade -y

Once the system is updated, we are ready to show you the basic touch commands in Linux.

1. Create a File

To create an empty zero byte file, execute the following command:

touch file.txt

You can check the size of the file:

-rw-r--r--  1 root root 0 Aug 14 22:58 file.txt

2. Change only the access time

To change only the access time to the file, you need to execute the following command:

touch -a file.txt

This command will update the last access modification time and will create the file if it does not exists.

3. Use the timestamp of another file

Sometimes we need to set the same timestamp to a file from another file. To do that, execute the following command:

touch -r file1.txt file2.txt

This command will update the timestamp of file2.txt with the timestamp of file file1.txt.

4. Create a file with a specific timestamp

To create a file and specify the time, you need to execute the following command:

touch -t 202208181118.34 file-time

The command above will create the file-time with a timestamp on August 18, 2022, at 11:18:34 AM.

5. Creating Multiple files

In the above examples, we were creating only a single file. Sometimes we need multiple files to be created. To do that, we can simply use the touch command with multiple file names:

touch file1.txt file2.txt file3.txt

This command will create three files at once:

-rw-r--r--  1 root root    0 Aug 15 11:51 file1.txt
-rw-r--r--  1 root root    0 Aug 15 11:51 file2.txt
-rw-r--r--  1 root root    0 Aug 15 11:51 file3.txt

Learn more about the touch command

In the previous paragraphs, we covered the most used touch commands. There is a brief explanation of the touch command along with options in Linux. To learn more about the touch command, execute the man touch command in your terminal.

man touch

You should get the following output:

TOUCH(1)                                                                   User Commands                                                                   TOUCH(1)

NAME
       touch - change file timestamps

SYNOPSIS
       touch [OPTION]... FILE...

DESCRIPTION
       Update the access and modification times of each FILE to the current time.

       A FILE argument that does not exist is created empty, unless -c or -h is supplied.

       A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output.

       Mandatory arguments to long options are mandatory for short options too.

       -a     change only the access time

       -c, --no-create
              do not create any files

       -d, --date=STRING
              parse STRING and use it instead of current time

       -f     (ignored)

       -h, --no-dereference
              affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink)

       -m     change only the modification time

       -r, --reference=FILE
              use this file's times instead of current time

       -t STAMP
              use [[CC]YY]MMDDhhmm[.ss] instead of current time

       --time=WORD
              change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m

       --help display this help and exit

Congratulations! You learned the basic touch commands with examples applicable to every Linux distribution.

You learned how to create one file or multiple files with specific timestamps. If you find it challenging to understand the touch command and you are not able to manage the files on your server, you can simply contact our epic technical support, and they will do the rest. You only need to sign up for one of our NVMe VPS plans and submit a support ticket. We are available 24/7.

If you liked this post about the five most used touch commands in Linux, please share it with your friends on social networks or simply leave a reply below. Thanks.

One thought on “5 Most Used Touch Commands in Linux With Examples

Leave a Reply

Your email address will not be published. Required fields are marked *