From time to time Linux users feel the need to create a new file on their system. Whether you're taking notes, writing code, or just want to review the file while programming, the touch command is the only file creation utility you'll need.
Creating files and managing timestamps on Linux is a breeze with the touch command. Here in this article we are going to explain in detail the touch command along with the various functions that can be performed with the tool.
What is the touch command?
The main function of the touch command is to update and manage file timestamps. If you are familiar with Linux, you may already know that each file on Linux distributions has specific timestamps associated with it.
Timestamps are responsible for storing file-related information, e.g. B. When the file was last modified, accessed or changed. These timestamps are mtime, atime, and ctime. All of this information can easily be changed with the touch command.
How to use the touch command
The most basic use of the touch command is to create new blank files. Unlike the cat command, which prompts you to add content to your file at creation time, the touch command creates a blank file without such prompts.
This is beneficial for software developers who have to keep creating new files, either to write code or to check the existence of a particular file.
The basic syntax of the touch command is:
Touch (Options) (File Name)
You can use the functions of the touch command by passing various arguments and flags instead of Options, whereas Filename is the name of the file you want to create.
Create new files with touch
Enter to create a blank file using touch touch followed by the filename.
The above command creates a new file named newemptyfile in the current working directory. You can verify that the file was created using the ls command.
Similarly, you can create multiple files together by passing the file names separated by place Character.
Touch fileone filetwo filethree
Change file timestamp
As mentioned above, there are three timestamps associated with every file in your storage.
Access time (atime)
Changed time (mtime)
Change time (ctime)
You can change the access and time of any file on your system with the touch command.
Use the to update the time and time of a file -a Flag with the standard touch command.
Touch a text file
The above command replaces the access and modification time of the file with the current time. If the file doesn't exist, touch creates a new file and assigns the timestamps to it.
You can change the modification time (mtime) of a file with the option -m Flag with touch too.
Touch -m text file
You can check to see if the timestamps have changed by printing that stat Command with the filename as an argument.
stat text file
File: `textfile & # 39;
Size: 13 Blocks: 8 I / O Block: 4096 regular file
Device: 801h / 2049d Inode: 327688 Left: 1
Access: (0644 / -rw-r – r–) Uid: (1000 / Ubuntu) Gid: (1000 / Ubuntu)
Access: 2021-04-12 16:59: 45.000000000 +0000
Change: 2021-04-12 16:57: 59.000000000 +0000
Change: 2021-04-12 17:02: 43.000000000 +0000
In the snippet above, you can see that the output shows the time, time, and time of the specified file.
Use of -c Flag with the touch command won't create a new file if it doesn't exist. Instead, it is only used to assign a new timestamp to existing files.
Touch -c existfile
Add custom timestamps to a file
For those who want to set custom modification timestamps for their file, the -c and -t Options could be useful. Use the following format to do this.
Touch -c -t YYDDHHMM-Filename
…Where YYDDHHMM is the date and time you want to set and Filename is the name of the file you want to change.
Use the button to change the timestamp of the file to be more user-friendly -d Flag with the touch command. You need to specify the time you want to set in simple language.
touch -d "5 hours ago" newfile
By combining the date command with touch, you can add a new modification timestamp to match the old one.
Touch -d "$ (date -r filename) – 5 hours"
If the timestamp of the file is 2:00 PM, executing the above command will set 9:00 AM as the new time for the file.
You can also set a custom modification timestamp for files at creation time. The -t You can do the same with flag.
Touch -t YYMMDDHHMM.SS Filename
For example, use the following command to create a new file with a timestamp of December 12, 2020 at 9:00:33 PM.
Touch -t 202012120900.33 newfile
If you cannot determine the path to the file you want to change, use the find command to find files with a similar name.
You can also set the time and time of the file individually during creation. Use the -a and -m Flags with the command.
To assign only access time to a new file at creation time:
Touch -a -t 202012120900.33 newfile
The following command assigns the specified modification time to the newly created file.
Touch -m -t 202012120900.33 newfile
Copy timestamps from other files
Use the key to copy the timestamp of another file -r Flag with the touch command. The standard syntax of the command is:
Touch -r original file copied file
… where the timestamps of the Original file are copied to the copied file.
Changing file information on Linux
Managing timestamps on a file has never been easier with the touch command. If you want to create a new file on Linux there are several options like Touch, Cat, etc. However, these options are only for those who have sufficient experience working with a Linux based operating system.
For those unfamiliar with the command line, there are several file managers available that you can use to graphically create new files. And if you want to navigate your system memory without bombarding your brain with scary commands, these file managers are the perfect choice for you.
10 best file managers for Linux power users
A reliable file manager makes file organization easier. Here is a list of the best file managers for Linux.
About the author
(39 articles published)
Deepesh is the junior editor for Linux at MUO. He has been writing informational content on the Internet for over 3 years. In his spare time he enjoys writing, listening to music and playing the guitar.
From Deepesh Sharma
Subscribe to our newsletter
Sign up for our newsletter to receive tech tips, reviews, free e-books, and exclusive offers!
One more step …!
Please confirm your email address in the email we just sent you.