[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Touch command in unix

Posted on 2011-09-27
8
Medium Priority
?
361 Views
Last Modified: 2012-05-12
Hi

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.
0
Comment
Question by:new_perl_user
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Accepted Solution

by:
_-W-_ earned 2000 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
0
 

Author Comment

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

Expert Comment

by:woolmilkporc
ID: 36712987
FILE=/path/to/testfile
DATE="20110909"
touch -t ${DATE}$(echo $(stat -c "%z" $FILE) | awk -F":| |\\\." '{print $2 $3 "." $4}') $FILE
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 21

Expert Comment

by:Papertrip
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


-t STAMP
              use [[CC]YY]MMDDhhmm[.ss] instead of current time
0
 
LVL 21

Expert Comment

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

Expert Comment

by:woolmilkporc
ID: 36713011
OK, for the 100 files

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

DATE="20110909"

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

Expert Comment

by:woolmilkporc
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.

wmp
0
 
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' /
:x
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.)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Fine Tune your automatic Updates for Ubuntu / Debian
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

650 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