MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
January
Sun Mon Tue Wed Thu Fri Sat
         
25
           
2016
Months
Jan


Mon, Jan 25, 2016 10:47 pm

Updating a file's time stamp and creating multiple files with touch

The touch command is a standard command available on Unix/Linux systems. It can be used to create new, empty files or change the timestamp on existing files. If there is an existing file named test.txt created on January 22, 2016 at 10:13 PM, i.e. 22:13 in the 24-hour time format, aka "military time", I can change the date to Decembe 25, 2015 and the time to 5:13 PM by using the command shown below.
$ touch -t 201512251713 test.txt
$ ls -l test.txt
-rw-rw-r-- 1 joe joe 0 Dec 25 17:13 test.txt

The -t option indicates that I wish to change the time stamp. It is followed by the date and time in the format YYYYMMDDHHMM where YYYY represents the year, MM the month, DD the day, HH the hour and MM represents minutes.

Using the --date argument to the command, you can even specify a time as YYYY-MM-DD HH:MM. Note: use the --time-style=long-iso or --time-style=full-iso options for the ls -l command to show the full timestamp.

$ touch --date="2013-01-25 09:00" example.txt
$ ls -l --time-style=long-iso example.txt
-rw-rw-r-- 1 joe joe 0 2013-01-25 09:00 example.txt

With the --date option, you can even specify a date in a format such as "next Friday" or "last Friday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day.

E.g., suppose, today is Saturday January 23, but I want to create two new files, one with a date of the prior Friday and one with a date of next Friday. I could use the commands shown below.

$ touch --date="last Friday" oldsample.txt
$ touch --date="next Friday" newsample.txt
$ ls -l *sample.txt
-rw-rw-r-- 1 joe joe 0 Jan 29  2016 newsample.txt
-rw-rw-r-- 1 joe joe 0 Jan 22 00:00 oldsample.txt

You can also specify the hours, minutes, and seconds using such a format, e.g., suppose I already have the file newsample.txt, but want to change the date and time for the existing file to be this coming Sunday at 11:00 PM. I could use the touch command below.

$ touch --date="Sunday 23:11:05" newsample.txt
$ ls -l --time-style="long-iso" newsample.txt
-rw-rw-r-- 1 joe joe 0 2016-01-24 23:11 newsample.txt
$ ls -l --time-style="full-iso" newsample.txt
-rw-rw-r-- 1 joe joe 0 2016-01-24 23:11:05.000000000 +0000 newsample.txt

You can even specify the time down to fractions of a section by putting a period after the seconds value, which appears as HH:MM:SS. E.g.:

$ touch --date="Sunday 23:11:05.01234" newsample.txt
$ ls -l --time-style="full-iso" newsample.txt
-rw-rw-r-- 1 joe joe 0 2016-01-24 23:11:05.012340000 +0000 newsample.txt
If you wish to create multiple empty files at once e.g., for test purposes, you can use a command such as touch file{n1..n2} where file is the first part of the file name, n1 is the starting number you wish to add to the end of the file name, and n2 is the ending number to be appended to the file name. The touch command will then create ten files starting with file1 through file10.
$ ls
index.html
$ touch file{1..10}.html
$ ls
file10.html  file2.html  file4.html  file6.html  file8.html  index.html
file1.html   file3.html  file5.html  file7.html  file9.html

[/os/unix/commands] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo