×

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

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute 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.

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.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 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.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute 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.

rm Command in Linux/Unix with Examples

The "rm" command in Linux (and other Unix-like operating systems) is used to remove files and directories. The "rm" command stands for "remove" and it is used to delete files...

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 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.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 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.

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.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

4 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.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.