What touch command in linux

touch command in Linux with examples

The touch command in Linux is used to alter the access and modification times for a given file or set of files. It’s also a super handy way to create an empty file very quickly, if you are running the touch command and specify a file that doesn’t already exist.

Many users will find themselves using the touch command every once in a while, or maybe even quite often. When working with a Linux operating system, sometimes a need arises to create an empty file. Some service, which an administrator is configuring, requires a certain file to be created before the service can start and, sometimes, this file may need to have certain access time stamp.

Other scenarios where touch proves useful is in applications that sort files according to their modification time. If you want to manipulate this sorting, without actually editing any files, touch can be used to update the modification time with any date and time that you supply. In this guide, you’ll learn how to use the touch command in Linux through examples. Follow along below to see learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the touch command on Linux

touch command in Linux with examples

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software touch
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The touch command has a simple syntax and a very simple purpose, so it doesn’t take long to learn. The following examples will teach you everything you need to know about the touch command, and the advanced section will show you some lesser used options that may come in handy every once in a while.

Examples of the touch command in Linux

touch command in Linux Basic Examples

  1. As mentioned earlier, one of the primary uses of touch is to create new, empty files. This is probably the quickest and easiest way to generate a new file on Linux, and one that system administrators will find themselves using often. The syntax is very simple – just specify the file name you wish to create.
$ stat example.txt Access: 2021-08-10 00:08:47.660120649 -0400 Modify: 2021-08-10 00:08:43.936129005 -0400 Change: 2021-08-10 00:08:43.936129005 -0400

We have cut some of the excess output here. But as you can see, the current access time of the file is 2021-08-10 00:08:47 and the modification time is 2021-08-10 00:08:43. Now let’s run the touch command and then check again.

$ touch example.txt $ stat example.txt Access: 2021-08-16 22:59:27.315203013 -0400 Modify: 2021-08-16 22:59:27.315203013 -0400 Change: 2021-08-16 22:59:27.315203013 -0400
$ touch -t 201901150325 example.txt
$ stat example.txt Access: 2019-01-15 03:25:00.000000000 -0500 Modify: 2019-01-15 03:25:00.000000000 -0500

NOTE
You can always use the man command to read more about the touch command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Читайте также:  Astra linux техподдержка телефон

Advanced Usage

The touch command is pretty simple, but it comes packed with a lot of options. Many of these options fly under the radar, and even some seasoned system administrators may not know of them. However, they can definitely come in handy in various situations. In this section of the guide, we’ll show you a few of the lesser known options of the touch command that we think are useful.

touch command in Linux Advanced Examples

  1. Use the -r option with the touch command to specify a reference file. What this does is change a file’s modification time to match the modification time of a different file that you specify. After executing the command, both files will have the same modification time.
$ touch -c file1 file2 file3
$ touch --date="Sun, 29 Feb 2004 16:21:42 -0800" example.txt OR $ touch --date="2019-01-15 03:25:00" example.txt OR $ touch --date="next Friday" example.txt

Closing Thoughts

In this guide, we learned all about the touch command on Linux. The touch command proves very useful when you need to create a new, empty file or set files, and it’s the easiest way to modify the modification time or access time of any file. The command may not make it into your daily repertoire, but you’ll find that it comes in handy more often than you would think.

Источник

9 Useful Examples of Touch Command in Linux

Learn to use touch command in Linux with these useful and practical examples.

Touch command in Linux

Touch command in Linux is used for changing file timestamps however one of the most common usages of touch command includes creating a new empty file.

With the touch command, you can change access, modify and change time of files and folders in Linux. You can update the timestamps or modify them to a date in the past.

The syntax for touch command is quite simple:

Touch command syntax

What are file timestamps in Linux, again?

I have written about timestamps in Linux in detail in an earlier article. I would recommend reading it for a better and clearer understanding. For the sake of a quick recall, I’ll list the timestamps here:

  • access time – last time when a file was accessed
  • modify time – last time when a file was modified
  • change time – last time when file metadata (file permission, ownership etc) was changed
Читайте также:  Диагностика жестких дисков linux

You can see the timestamps of a file using the stat command in the following manner:

stat abhi.txt File: abhi.txt Size: 10 Blocks: 8 IO Block: 4096 regular file Device: 10305h/66309d Inode: 11940163 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/abhishek) Gid: ( 1000/abhishek) Access: 2018-09-02 14:12:24.476483367 +0530 Modify: 2018-09-02 14:12:24.476483367 +0530 Change: 2018-09-02 14:12:24.476483367 +0530 Birth: -

Stat command even shows the inode number of the file.

9 Practical examples of touch command in Linux

Now let’s see how to use the touch command with some simple but useful examples.

1. Create an empty file

As I mentioned earlier, this is the most common use of touch command. All you have to do is to use touch with the file name.

This will create an empty file if the file doesn’t exist.

touch empty_file ls -l empty_file -rw-r--r-- 1 abhishek abhishek 0 Sep 2 14:37 empty_file

But what if the file already exists? In that case, it will update all three timestamps of the file to the current time.

2. Create multiple empty files

You can use touch to create more than one empty file as well. Just provide the names of the files you want to create.

If you think it’s tiring to write all filenames, you can auto-generate filenames in this way:

This will create new-file-1.txt, new-file-2.txt upto new-file-10.txt.

3. Avoid creating a file with touch if it doesn’t exist

Touch will update the timestamps of input file if it exists and will create an empty file if the input file does not exist.

But what if you don’t want touch to create a new empty file? You want it to update the timestamps of the file but if the file doesn’t exist, it should not be created.

You can use the touch command with -c option in such cases:

Remember: Touch will create a new empty file if it doesn’t exist else it will modify the timestamps of the existing file. You can stop the creation of a new file with the -c option.

4. Change all timestamps of a file

If you use touch on an existing file, it will change access, modify and change time of that file.

For example, I have this file named sherlock.txt with the following timestamps:

stat sherlock.txt File: sherlock.txt Size: 356 Blocks: 8 IO Block: 4096 regular file Device: 10305h/66309d Inode: 11928277 Links: 1 Access: (0777/-rwxrwxrwx) Uid: ( 1000/abhishek) Gid: ( 1000/abhishek) Access: 2018-08-25 09:44:56.092937000 +0530 Modify: 2018-08-09 09:41:05.028309000 +0530 Change: 2018-08-25 09:44:56.096937182 +0530 

If I use touch on this command, all timestamps will be changed to the current timestamps.

touch sherlock.txt stat sherlock.txt File: sherlock.txt Size: 356 Blocks: 8 IO Block: 4096 regular file Device: 10305h/66309d Inode: 11928277 Links: 1 Access: (0777/-rwxrwxrwx) Uid: ( 1000/abhishek) Gid: ( 1000/abhishek) Access: 2018-09-02 15:22:47.017037942 +0530 Modify: 2018-09-02 15:22:47.017037942 +0530 Change: 2018-09-02 15:22:47.017037942 +0530 Birth: - 

Note: You should not be bothered with ctime (change time). It’s a system property and cannot/shouldn’t be controlled by the user. Your focus should be on access and modify time.

5. Update only access time of file

You may not always want to change all the timestamps of a file. If you just want to change the access time of a file, you can use the -a option with touch.

touch -a sherlock.txt stat sherlock.txt File: sherlock.txt Size: 356 Blocks: 8 IO Block: 4096 regular file Device: 10305h/66309d Inode: 11928277 Links: 1 Access: (0777/-rwxrwxrwx) Uid: ( 1000/abhishek) Gid: ( 1000/abhishek) Access: 2018-09-02 15:29:08.796926093 +0530 Modify: 2018-09-02 15:22:47.017037942 +0530 Change: 2018-09-02 15:29:08.796926093 +0530 Birth: -

6. Update only modify time of file

If you just want to update the modify time of a file to the current timestamp, use the -m option of touch command.

touch -m sherlock.txt stat sherlock.txt File: sherlock.txt Size: 356 Blocks: 8 IO Block: 4096 regular file Device: 10305h/66309d Inode: 11928277 Links: 1 Access: (0777/-rwxrwxrwx) Uid: ( 1000/abhishek) Gid: ( 1000/abhishek) Access: 2018-09-02 15:29:08.796926093 +0530 Modify: 2018-09-02 15:31:25.770866881 +0530 Change: 2018-09-02 15:31:25.770866881 +0530 Birth: -

7. Use timestamps of another file

You can also use the timestamps of another file as a reference with the -r option in the following manner:

Читайте также:  Linux duplicate file search

This will set the access and modify time of the target file same as the access and modify time of the source file.

8. Set specific access and modification time

You might have noticed that in almost all the cases (except the reference file one), the timestamps are changed to the current timestamp.

But you are not bound with that. Touch allows you to set access and modification time to a past or future date. You can use the -t option and a timestamp in the following format:

  • CC – First two digits of a year
  • YY – Second two digits of a year
  • MM – Month of the year (01-12)
  • DD – Day of the month (01-31)
  • hh – Hour of the day (00-23)
  • mm – Minute of the hour (00-59)
  • ss – Seconds (00-59)

In the above case, CC is optional. In fact, CCYY is optional as well, it will take the current year in that case. Similarly, seconds are optional as well, it defaults to 00.

Let me show you an example by changing the timestamp to 12021301 i.e., 12th month, second day, 13th hour and first minute of the current year:

touch -t 12021301 agatha.txt stat agatha.txt File: agatha.txt Size: 457 Blocks: 8 IO Block: 4096 regular file Device: 10305h/66309d Inode: 11928279 Links: 1 Access: (0777/-rwxrwxrwx) Uid: ( 1000/abhishek) Gid: ( 1000/abhishek) Access: 2018-12-02 13:01:00.000000000 +0530 Modify: 2018-12-02 13:01:00.000000000 +0530 Change: 2018-09-02 15:59:47.588680901 +0530 Birth: -

If you try to enter an invalid date, you’ll see an error. You’ll also notice that change time is using the current timestamp, not the same as access and modify. It’s because it’s system property.

You can also use touch command with symbolic links. You just have to use the -h option while dealing with symbolic links. The rest stays the same as the regular files.

I hope you find these touch command examples in Linux helpful. If you have any questions or suggestions, do let me know.

Источник

Оцените статью
Adblock
detector