Touch command in unix

Posted on 2011-09-27
Last Modified: 2012-05-12

I want to change the file's date from 9/27/2011 to 9/9/2011. How can I do this in unix using touch.

It should change only the date.
Question by:new_perl_user

Accepted Solution

_-W-_ earned 500 total points
ID: 36712970
Here are three ways:
 # touch -t 200701310846.26 index.html
 # touch -d '2007-01-31 8:46:26' index.html
 # touch -d 'Jan 31 2007 8:46:26' index.html

Author Comment

ID: 36712985
I mean there are around 100 files with date as 9/27/2011 . I need to change them to 9/9/2011
LVL 68

Expert Comment

ID: 36712987
touch -t ${DATE}$(echo $(stat -c "%z" $FILE) | awk -F":| |\\\." '{print $2 $3 "." $4}') $FILE
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

LVL 21

Expert Comment

ID: 36713005
touch -t 201109090000 filename

Open in new window

or, since the year is the same,
touch -t 09090000 filename

Open in new window

              use [[CC]YY]MMDDhhmm[.ss] instead of current time
LVL 21

Expert Comment

ID: 36713010
Woops didn't see the other answers, walked away for a few while writing my answer :p
LVL 68

Expert Comment

ID: 36713011
OK, for the 100 files

touch -t 201127090000 /tmp/from
touch -t 201127092359 /tmp/to


find /dir -type f -newer /tmp/from ! -newer /tmp/to |while read FILE
   touch -t ${DATE}$(echo $(stat -c "%z" $FILE) | awk -F":| |\\\." '{print $2 $3 "." $4}') $FILE
LVL 68

Expert Comment

ID: 36713059
You can also use

... stat -c "%y" ... ...

to keep the time of last modification, or

... stat -c "%x" ... ...

to keep the time of last access, instead of my

.. stat -c "%z" ... ...

which will keep the time of last change.

LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36713121
Is this something you want to do just once or several times?  

If several times, accept one of the fine answers above.

If just once, and you are good with vi, you can do something like thsi

ls  > foo
vi foo
1,$s/^/touch -d '2007-01-31 8:46:26' /
sh foo

Credit goes to -W- for providing the touch command.

I don't want any points for this because this is a scripting thread and I gave a vi answer and the other answers are good and earlier.  (It's just quicker for me, sometimes, when I just have to do whatever the task is once.)

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best way to virtualise a remote Linux server 2 69
php ssh2_scp_send 1 61
CentOS 7 Installation 7 74
Linux Ubuntu - Volume "boot" has only 0 bytes disk space remainig 6 64
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question