How to loop through directories to touch files

Hi,

Please help me on this.

Suppose i have the following directory structure.
/app/data
/app/data/eng
/app/data/med
/app/data/bsc

each of the directories data,data/eng,data/med,data/bsc holds files with date extension like
a.20081230
b.20081230 and so on

I need a script to loop through the directories and touch the files .

please suggest as i am new to unix.

Something like the following should work:

#!/bin/sh

for $i in {`ls $DIR`}
do
  touch $WHATEVER
done

Remember to set your variables properly.

That's not going to work in more ways than none.

I'm not clear if a loop is needed for any reason, nor if by "touching" the file you want to change the date stamp in the name fo the file or just the atime, but this seems to be better handled by the find command, like "find from /app name *2008* | exec touch the file" .

Hi,

glen your script cannot work for two reasons:

a) first ls is not recursive
b) it should be: for i in ...

Try instead:

for file in app{/,/med/,/bsc/,/data/}*\.bar; do echo $file; done

Will print out a list of all the files ending in ".bar" in the directories
app, app/med, app/bsc, app/data.

You should be able to adopt this to your needs.

HTH Chris

find /app/data -type f -exec touch '{}' \;

will touch every regular file under /app/data

if you want touch only files with given name...
find /app/data -type f -name "*.20081230" -exec touch '{}' \;

or date -- see -atime in man find

Thanks to all for the quick response.

But i was thinking in doing it with for loop so that i could have better chance to check if any of files in these directories misses out the touch command.
With touch i just wanted to the file to reflect the current sysdate and time.

i was thinking something like this

base_dir=/app/data
var1=/eng
var2=/med
var3=/bsc

for <list of direcotries> in <parentdir>
do
for i in *.20081230* // searchin each of directories /data ,/med, /bsc for files to touch
do
touch *
if [ -ne 0 ]
then
echo "failed to touch `$1` "
fi
done

Please suggest

What do you expect a failure of the touch command to tell you?

Don't you mean:

for <directory> in <list of directories in parentdir>

Why touch everything on every iteration of the loop?

If you do that, you will have no idea which file failed to be touched.

Syntax error. That should be:

if [ $? -ne 0 ]

That will try to execute the contents of $1 as a shell command. You mean:

echo "failed to touch '$1' "
for dir in "$base_dir/$var1" "$base_dir/$var2" "$base_dir/$var3"
do
    (
      cd "$dir" || continue
      for i in *.20081230*
      do
           touch "$i"
           if [ $? -ne 0 ]
           then
              echo "failed to touch '$1' "
           fi
      done
    )
done

hi johnson

thanks for the quick response.

with the failure of touch command i just want to know the name of files which are not touched so that i could perform touch manually.

could you please explain me the meaning of || coninue in the below statement.

cd "$dir" || continue

In the above program is there any way we could get all the names of all files touched and not touched as output

I'm new to unix programming and need some inputs on books i could refer.

Please suggest.

What do you expect to gain by touching the files?

Please put code inside

 tags (as I have done for you in this reply).



cd "$dir" || continue

[/quote]

[indent]
If the command on the left fails, the command on the right is executed.

touch "$i" && printf 'Success: %s\n' "$i" || printf 'Failed: %s\n' "$i"

Links to other shell resources

Thanks a lot johnson for your time and suggestion.:wink:

The code which u gave has worked very well. Thank you again for your time.
Frankly speaking u r the best.:b: