LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-02-2004, 09:37 AM   #1
mandavi
LQ Newbie
 
Registered: Nov 2004
Location: berlin
Distribution: ubuntu 6.06
Posts: 21

Rep: Reputation: 15
Question how to use "touch" for a whole file tree


hi,
i have many files in a lot of directories and subdirectories where i want to change the time-stamp of files and folders. it works fine with touch for _one_ file, but takes some time for all. i did not find any recursive function for touch. and i have no idea how to write a script for that.
can anyone help?

Last edited by mandavi; 11-02-2004 at 10:05 AM.
 
Old 11-02-2004, 11:45 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Like you stated, touch doesn't have a recursive option. This is how I should tackle this:

Touch all files in /home/doe (and all subdirs/files:
$ find /home/doe -exec touch {} \ ;

Touch all files in /home/doe (files only, no dirs):
$ find /home/doe -type f -exec touch {} \ ;

And the other way around (touch only dirs):
$ find /home/doe -type d -exec touch {} \ ;

Hope this helps.
 
Old 11-02-2004, 11:57 AM   #3
marcheikens
Member
 
Registered: Aug 2003
Location: Cleveland, OH
Distribution: Ubuntu 6.06
Posts: 66

Rep: Reputation: 15
Code:
#!/bin/sh

for X in *
do
[ -d "$X" ] && cd "$X" && multitoucher && cd ..
touch "$X"
done
This worked for me. It has to be in your path and executable, and named multitoucher to work properly. To make sure, create a test directory and populate it with test files and directories etc. I can't take any credit for any destructive behavior this script exhibits, as I just spent the last half hour figuring this out for the first time.

marc

OH! Make sure you're in the top directory that you want touched, so if you want to touch all the files and folders in /home/jdoe/touchme/ make sure to cd /home/jdoe/touchme/ before running that script, or else all the files and folders in the directory you're in and all below will be touched.

Last edited by marcheikens; 11-02-2004 at 12:03 PM.
 
Old 11-02-2004, 12:17 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Nice recursive code

And it will work in a 'true' linux/unix environment. With that I'm trying to say that if you run it and it encounters names with, for example, spaces in them it will fail........

The for X in * is the base for that problem. This File (one file!!) will be broken up and X will be filled first with This and next with File.

Using find is one way of solving this. There must be others (there's the while construct in the back of my mind).

Quote:
I can't take any credit for any destructive behavior this script exhibits.
Neither can I
 
Old 11-02-2004, 12:27 PM   #5
mandavi
LQ Newbie
 
Registered: Nov 2004
Location: berlin
Distribution: ubuntu 6.06
Posts: 21

Original Poster
Rep: Reputation: 15
i tried the first option and got a

find: missing argument to `-exec'

after find /home/mandavi/data/ -exec touch {}\ -t 06251436;
but i can't find in the find-manual any need for an extra argument for -exec but the arguments for the executed command.
and i have a lot of "non-unix" file and folder names in the tree...
 
Old 11-02-2004, 12:30 PM   #6
marcheikens
Member
 
Registered: Aug 2003
Location: Cleveland, OH
Distribution: Ubuntu 6.06
Posts: 66

Rep: Reputation: 15
Thanks!

Are you sure about the spaces? I thought "$X" took care of spaces, or couldn't you also put ${X} ?
 
Old 11-02-2004, 12:40 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally posted by mandavi
i tried the first option and got a

find: missing argument to `-exec'
I believe I placed a space between the \ and the ;. Sorry about that!

To make sure, checked and changed:

Touch all files in /home/doe (and all subdirs/files:
$ find /home/doe -exec touch {} \;

Touch all files in /home/doe (files only, no dirs):
$ find /home/doe -type f -exec touch {} \;

And the other way around (touch only dirs):
$ find /home/doe -type d -exec touch {} \;
 
Old 11-02-2004, 12:51 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally posted by marcheikens
Thanks!

Are you sure about the spaces? I thought "$X" took care of spaces, or couldn't you also put ${X} ?
${X} will not work, the "$X" or "${X}" will work in some cases (a real space being one of them). But a space is just one of the 'strange' characters you can encounter.
 
Old 11-02-2004, 01:11 PM   #9
marcheikens
Member
 
Registered: Aug 2003
Location: Cleveland, OH
Distribution: Ubuntu 6.06
Posts: 66

Rep: Reputation: 15
Odd, I'm playing with spaces, &, %, even * in file and directory names, and no errors. I don't know why this works for me, as I'm still a novice at bash scripting.
 
Old 11-02-2004, 01:45 PM   #10
mandavi
LQ Newbie
 
Registered: Nov 2004
Location: berlin
Distribution: ubuntu 6.06
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by druuna

To make sure, checked and changed:

Touch all files in /home/doe (and all subdirs/files:
$ find /home/doe -exec touch {} \;
it worked! thanks a lot
 
Old 03-18-2005, 11:32 AM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you have file names that contain white-space or evil characters, place the brackets inside of double quotes.

find /home/doe -exec touch "{}" \;

Last edited by jschiwal; 03-25-2005 at 05:51 AM.
 
Old 07-05-2005, 05:16 AM   #12
ardee
LQ Newbie
 
Registered: Jun 2004
Distribution: Gentoo Linux, Ubuntu, Manderake 10 Official
Posts: 5

Rep: Reputation: 0
find /usr/src/linux -exec touch {} \; does not work for me...

But this one did

find /usr/src/linux * -exec touch {} \;

Note the *.

Without the -exec option, find /usr/src/linux would only output /usr/src/linux. Including the * would display all the files inside /usr/src/linux, which is what you want.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parasite in a file name with touch & ">" orgazmo Programming 25 05-24-2005 03:28 AM
strange group and user set to "573" in linux-2.4.27 tree structure xround Linux - General 2 10-12-2004 11:09 AM
Turn off touch pad "clicking" pgte3 Linux - Newbie 7 08-25-2004 01:59 PM
Can't boot Mandrake 10.0, "Cannot touch, Read-Only File System" RasutoIbuki Mandriva 5 07-23-2004 08:57 PM
Can't locate object method "splitpath" via package "File::Spec" RobJohnston Linux - General 2 06-28-2003 09:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration