The touch command in Linux and other Unix-like operating systems is a utility for creating empty files and modifying the timestamps of existing files. In its simplest form, it allows you to create a new file without any content. When applied to an existing file, it updates the file's access and modification timestamps to the current system time.
Syntax:
touch [OPTION]... FILE...
Here, [OPTION] represents optional flags that you can use to modify the behavior of the command, and FILE represents one or more filenames that you want to create or update.
Options
The commonly used options for the touch command:
-a
: Only change the access time (atime).-c
,--no-create
: Do not create any new files.-m
: Only change the modification time (mtime).-r
,--reference=FILE
: Use FILE's times instead of the current time.-d
,--date=STRING
: Parse STRING and use it instead of the current time.-t TIMESTAMP
: Use [[CC]YY]MMDDhhmm[.ss] instead of the current time.--time=WORD
: Change the specified time; WORD can be access, atime, use, modify, or mtime.-h
,--no-dereference
: Affect the symbolic link itself instead of the file to which it points.
Basic Usage
One of the most basic and common usages is file creation - with empty content.
To create a new, empty file called samplefile.txt, you would use the following command:
touch samplefile.txt
This command creates a new file named samplefile.txt in the current directory. If the file already exists, its timestamps will be updated to the current system time. Don't worry it won't overwrite the file content.
if you don't want to create a new file use the -c or --no-create option. This will only update the timestamps of files that already exist; no new files will be created.
You can verify that the file was created by listing the files in the directory using the ls command:
ls
or
ls -l
You may also create multiple empty files
touch file1.txt file2.txt file3.txt
In this example, three new files - file1.txt, file2.txt, and file3.txt - will be created in the current directory.
You can also use brace expansion to create multiple files in a more efficient manner. Example touch filename{1..10}.
To create an empty file in a directory:
touch dir1/samplefile.txt
While touch doesn't create directories, but often used alongside mkdir and other utilities in scripts to set up complex file and directory hierarchies.
Using Touch to Modify File Timestamps
The touch command is commonly used to create empty files, but it also has the important function of modifying file timestamps. Let's look into it.
By default when you touch an existing file To update the access and modification timestamps to the current system time for a file named existingfile.txt, you simply run:
touch existingfile.txt
This will set both the atime and mtime to the current date and time. You can verify using the stat command.
If you want to change access time (atime) to the current time, you can use the -a flag:
touch -a myfile.txt
Same way to change modification time (mtime) to the current time, you can use the -m
flag:
touch -m myfile.txt
You can also change the timestamp based on a string that represents that date and time using -d option.
Examples:
touch -d '2023-06-31 12:00:00' samplefile.txt
touch -d 'June 31 12:00 2023' samplefile.txt
touch -d '12:00 01/06/2023' samplefile.txt
This sets both the access and modification timestamps of the file named samplefile.txt to June 31, 2023, at 12:00.
The touch command can be particularly useful for developers working with Makefiles. In a typical build system using Makefiles, a decision about whether to recompile a source file is made based on the comparison of timestamps between the source files (*.c
, *.cpp, etc.) and the corresponding object files (*.o).
Creating Files with Specific Timestamps
The touch command also allows you to set specific timestamps using the -t
option.
Syntax:
touch -t [CCYYMMDDhhmm.ss] filename
Here, [CCYYMMDDhhmm.ss]
is the timestamp, and its components are:
CCYY
: 4-digit yearMM
: 2-digit monthDD
: 2-digit dayhh
: 2-digit hourmm
: 2-digit minutess
: 2-digit second (this is optional)
Suppose you want to set both the access and modification times for a file called samplefile.txt to August 1, 2023, at 12:30:06. You would use:
touch -t 202308011230.06 samplefile.txt
You can combine with -m or -a option to explicitly set only for modification and access time. You also reference another file to copy its timestamp using -r option.
Comments