How to Create New Files on Linux Using touch
Every now and then, Linux users feel the need to create a new file on their system. Whether it be for taking notes, writing some code, or simply for file validation during programming, the touch command is the only file creation utility you need.
Creating files and managing timestamps on Linux is a snap with the touch command. Here in this article, we will discuss the touch command in detail, along with the various functions that can be performed using the tool.
What Is the touch Command?
The primary function of the touch command is to update and manage file timestamps. If you have a bit of experience working on Linux, you might already know that on Linux distributions, every file has specific timestamps associated with them.
Timestamps are responsible for storing file-related information such as when the file was last modified, accessed, or changed. These timestamps are mtime, atime, and ctime. All this information can be modified with ease using the touch command.
How To Use the Touch Command
The most basic use of the touch command is to create new empty files. Unlike the cat command, which prompts you to add content to your file at the time of creation, the touch command creates an empty file without such prompts.
This is beneficial for software developers who have to constantly create new files, either for writing code or for validating the existence of a specific file.
Basic Syntax
The basic syntax of the touch command is:
touch [options] [filename]
You can utilize the functionalities of the touch command by passing various arguments and flags in place of options, whereas filename is the name of the file that you want to create.
Create New Files With touch
To create an empty file using touch, type touch followed by the filename.
touch newemptyfile
The aforementioned command will create a new file named newemptyfile in the current working directory. You can verify that the file has been created using the ls command.
Similarly, you can create multiple files altogether by passing the filenames separated by the space character.
touch fileone filetwo filethree
Change File Timestamps
As mentioned above, there are three timestamps associated with every file on your storage.
- Access time (atime)
- Modified time (mtime)
- Change time (ctime)
You can change the access and change time of any file on your system using the touch command.
To update the atime and ctime of a file, use the -a flag with the default touch command.
touch -a textfile
The above command will replace the access and change time of the file with the current time. If the file does not exist, touch will create a new file and assign the timestamps to it.
You can change the modification time (mtime) of a file using the -m flag with touch as well.
touch -m textfile
You can check whether the timestamps were changed by issuing the stat command with the filename as an argument.
stat textfile
Output:
File: `textfile'
Size: 13 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 327688 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ ubuntu) Gid: ( 1000/ ubuntu)
Access: 2021-04-12 16:59:45.000000000 +0000
Modify: 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 displays the atime, mtime, and ctime of the specified file.
Using the -c flag with the touch command doesn't create a new file if it doesn't exist. Instead, it is only used to assign a new timestamp to already existing files.
touch -c existfile
Add Custom Timestamps To a File
For those who want to set a custom modification timestamps for their file, the -c and -t options might be of use. Use the following format to do the same.
touch -c -t YYDDHHMM filename
...where YYDDHHMM is the date and time that you want to set and filename is the name of the file that you want to modify.
To change the timestamp of the file in a more user-friendly way, use the -d flag with the touch command. You will have to specify the time that 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 in accordance with the old one.
touch -d "$(date -r filename) - 5 hours" existfile
If the timestamp of the file is 2:00pm, then executing the aforementioned command will set 9:00am as the new mtime for the file.
You can also set custom modification timestamp for files at the time of creation. The -t flag allows you to do the same.
touch -t YYMMDDHHMM.SS filename
For example, to create a new file with 12 December 2020, 09:00:33pm as the timestamp, use the following command.
touch -t 202012120900.33 newfile
If you can't figure out the path to the file that you want to modify, use the find command to search for files with a similar name.
You can also set the mtime and atime of the file individually during creation. Use the -a and -m flags with the command.
To assign only the access time to a new file at the time of creation:
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
To copy the timestamp of any other file, use the -r flag with the touch command. The default syntax of the command is:
touch -r originalfile copiedfile
...where the timestamps of the originalfile are getting copied to the copiedfile.
Changing File Information in Linux
Managing timestamps of a file has never been easier with the touch command. If you are looking to create a new file on Linux, there are multiple options such as touch, cat, etc. But these choices are viable for only those who have decent experience working with any Linux-based operating system.
For those who are not comfortable with the command line, several file managers are available that allow you to create new files in a graphical manner. And if you want to navigate through your system storage without bombarding your brain with scary commands, these file managers will be the perfect choice for you.
source https://www.makeuseof.com/how-to-create-new-files-on-linux-using-touch/
Post a Comment for "How to Create New Files on Linux Using touch"
Comment when there are difficulties