×

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple command that is used to update the access and modification timestamps of a file. It is often used to create new empty files or to update the timestamps of existing files.

The basic syntax of the touch command is:

touch [options] [file]

When executed without any options, the touch command will create a new empty file with the specified name if it does not already exist. If the file already exists, it will update the access and modification timestamps of the file to the current time. For example, the following command will create a new empty file named "example.txt" in the current directory:

touch example.txt

If you want to create multiple files at the same time, you can use the touch command multiple times with different file names or you can use wildcard characters to specify multiple files at once.

touch file1 file2 file3

or

touch file*

The touch command also has options that allow you to specify the timestamps for the access and modification time of a file. The option -a is used to change the access time only, and the option -m is used to change the modification time only. The following command will change the access time of a file to the current time:

touch -a file.txt

and the following command will change the modification time of a file to the current time:

touch -m file.txt

Additionally, the touch command also has option -d which is used to set the timestamp of a file to a specific date and time. The option -d takes a date and time as an argument in the format of [[CC]YY]MMDDhhmm[.SS].

touch -d '2022-01-01 12:00:00' file.txt

The touch command is also often used in shell scripts and automation tasks to create new empty files or update the timestamps of existing files. For example, a script may use the touch command to create a new empty file before writing some data to it, or to update the timestamps of a file before or after performing some action.

The touch command is a simple command that can be used in many ways to create new empty files or update the timestamps of existing files. Here are some additional features and uses of the touch command:

  • The touch command can also be used to update the timestamps of a directory. By default, touch command only updates the timestamps of the file, but with the option -t, it can be used to update the timestamps of the directory.
    • touch -t 202212251200 directory
  • The touch command can be used to create a new file with specific permissions using the option -c. When this option is used, touch will not create a new file if it does not exist, but it will update the timestamp of the file.
    • touch -c file.txt
  • The touch command can also be used to change the owner and group of a file or directory. The option -r is used to reference another file and change the timestamps of the current file to match the reference file.
    • touch -r reference_file file.txt
  • The touch command can also be used with the option -h, this option is used to change the timestamps of a symlink instead of the file it points to.
    • touch -h symlink
  • The touch command can also be used to create a new file with specific size, the option -s is used to specify the size of the file in bytes.
    • touch -s 100 file.txt
  • The touch command can also be used to create a new file with specific date and time using the option -d and -t together. The option -d is used to specify the date and time in the format of [[CC]YY]MMDDhhmm[.SS], and -t is used to specify the time in the format of [[CC]YY]MMDDhhmm[.SS].
    • touch -d '2022-01-01 12:00:00' -t 202212251200 file.txt

In summary, the touch command is a versatile command that can be used in many ways to create new empty files or update the timestamps of existing files. It can be used with various options like -t, -c, -r, -h, -s, -d, -t which can be used to update timestamps of files, directories, symlinks and also to create files with specific permissions, size, and timestamps. The touch command is an essential command for system administrators, developers, and other users to create new empty files or update the timestamps of existing files, directories, and symlinks.


Related Topics

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

3 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

12 minutes read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes read.

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

5 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

3 minutes read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.