How to change time stamp with touch command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change time stamp with touch command?
# 1  
Old 01-09-2013
How to change time stamp with touch command?

Hi,

I wish to change time stamp of a directory with all its subdirectories and files on server.
I am able to find following two ways but want to know which will be the better one. I have not tried anyone of them because I am not sure if it can effect my data:

Code:
find * -type d -exec touch '{}/.gitkeep' \;

Code:
$ bash recursive_touch.sh MyProject .gitkeep
 
SOURCE_DIR=$1
FILE=$2
 
recursive_touch() {
  for dir in *
  do
    if [ -d $dir ]
    then
      cd $dir; recursive_touch
    fi
    touch $FILE       
  done
}
 
cd $SOURCE_DIR; recursive_touch

Thanks
# 2  
Old 01-09-2013
The "find" version is preferable because it uses less system resources to execute and is IMHO a lot easier to read and maintain.

Note that "touch" will create a file if it isn't already there, therefore your version will not only update existing files ".gitkeep", but also create one in every directory if it ins't already there. If you only want to touch already existing files you will want to change your "find" command:

Code:
find * -type f -name ".gitkeep" -exec touch '{}' \;

"touch" will only change the content of the i-node of a file (the modification timestamp), but not change the contents of the file itself.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 01-09-2013
Thanks

Code:
Note that "touch" will create a file if it isn't already there, therefore your version will not only update existing files ".gitkeep", but also create one in every directory if it ins't already there.

If a file is not there, how its possible to create any random file. I am little confused with it.


So if the name of my directory is Practical and I want to change time stamp of it with all its subdirectories and files, I can use this command.

Code:
find * -type f -Practical ".gitkeep" -exec touch '{}' \;

Thanks
# 4  
Old 01-09-2013
Quote:
Originally Posted by bioinfo
If a file is not there, how its possible to create any random file. I am little confused with it.
"touch" will create an empty file (not a random one!). Try it out. Create and change to a temporary directory, and issue "touch file" there, then issue "ls". You will see a file named "file" with a content of 0 bytes. You can remove this directory along with the file by going one level higher and issue "rm -rf <directory>"

Code:
cd /tmp
mkdir tempdir
cd tempdir
touch file
ls -l
cd ..
rm -rf tempdir

Quote:
So if the name of my directory is Practical and I want to change time stamp of it with all its subdirectories and files, I can use this command.
No, not quite. Do it this way:

Code:
find /some/where/Practical -type f -name ".gitkeep" -exec touch '{}' \;  # changes existing ".gitkeep" files
find /some/where/Practical -type d -exec touch '{}' \;                   # changes existing directories

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 01-09-2013
Can you please explain regarding gitkeep. Smilie
# 6  
Old 01-09-2013
Quote:
Originally Posted by bioinfo
Can you please explain regarding gitkeep. Smilie
I don't quite understand: "touch" changes the time stamp of an existing file or directory. If the filename given does not exist it will create the file with the respective time stamp.

The first command searches for all files of the name ".gitkeep" in a directory structure given and uses "touch" on these files. The second one does the same with the directories in this tree.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change date time stamp of existing file

I have a file hello.txt which was created today (today's date timestamp) I wish to change its date timestamp (access, modified, created) to 1 week old i.e one week from now. uname -a SunOS mymac 5.11 11.2 sun4v sparc sun4v Can you please suggest a easy way to do that ? (12 Replies)
Discussion started by: mohtashims
12 Replies

2. Shell Programming and Scripting

Logs between two time stamp

I am creating log monitoring script and stuck up to get the logs between two time stamp. can you please help me to create the script to get the logs between two time stamp, for example, I need the complete logs between # Time: 150328 1:30:10 and # Time: 150328 19:10:57 OS : Cent OS 6.x... (8 Replies)
Discussion started by: zenkarthi
8 Replies

3. Solaris

Find command output gives one day before time stamp

Hi All, I am listing the files which are 4 hours older. For this first I have creted a dummy file with the 4 hours before timestamp, then I am using the below find command, find /path/ -type f ! -newer 4_hours_oledr_file -exec ls -lrt {} \; I am getting the files which are older than... (13 Replies)
Discussion started by: velava
13 Replies

4. Solaris

System time and Cron time stamp not matching

On Solaris 10 server the system date won't match with the timestamp on files created by a cron jobs, Please help here is what i get when i check for system date infodba-ie10ux014:/tcpdv1_ie10/tcadmin/bin\n\r-> date Tue Apr 24 15:27:43 GMT 2012at same time i executed a cron job, and checked... (4 Replies)
Discussion started by: karghum
4 Replies

5. Shell Programming and Scripting

change and replace time stamp

Hi, I want to convert normal time stamp to unix time stamp to a filename. coz our ssytem will pick depends on unix timestamp format . the filenames are as shown below fie names are stored in file say temp.txt. MLFG2_cDomHTTPstats_SAB15-1_1318482447.dat... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

6. Shell Programming and Scripting

How to get time duration between two human readable time stamp in Unix?

Here is two time I have: Jul 12 16:02:01 Jul 13 01:02:01 and how can I do a simple match to get difference between two time which is 09:00:00 Thanks in advance. (3 Replies)
Discussion started by: ford99
3 Replies

7. Shell Programming and Scripting

regarding time stamp

hi everyone i am facing a strange problem here suppose content of my file is a=1,2,3 b=2,3,4 c=4,5,6 time= now the problem is i want to add value in front of time variable and the value should be i format only "HHMMSS" so it should be like this a=1,2,3 b=2,3,4 c=4,5,6... (3 Replies)
Discussion started by: aishsimplesweet
3 Replies

8. Shell Programming and Scripting

Change time stamp of a file

Hi, As i know , we can change the time stamp of a file by touch command, i did change in a file and it is looking as given # ls -l abcd -rw-r--r-- 1 batsoqa sicusers 0 Feb 17 2010 abcd actually i want to see the output like this -rw-r--r-- 1 batsoqa sicusers ... (3 Replies)
Discussion started by: apskaushik
3 Replies

9. Shell Programming and Scripting

how to touch a file with prev time stamp

i want to find the files which are modified in last 30 to 120 minutes i am using "find . -mmin +30 -mmin -120 " it is giving me the error find: bad option -mmin find: path-list predicate-list can somebody help me out . Thank you (5 Replies)
Discussion started by: Prat007
5 Replies

10. Shell Programming and Scripting

change the time stamp of file

can we change the timestamp of a file to old date. -rwxrwxrwx 1 root other 330 Jul 1 16:03 abc.txt it shows creation time is 16.03 can i change it to previous time :) (2 Replies)
Discussion started by: anish19
2 Replies
Login or Register to Ask a Question