How to loop through directories to touch files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to loop through directories to touch files
# 1  
Old 12-26-2008
Network 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.
# 2  
Old 12-26-2008
Something like the following should work:

Code:
#!/bin/sh

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

Remember to set your variables properly.
# 3  
Old 12-26-2008
Quote:
Something like the following should work:
Code:
#!/bin/sh
for $i in {`ls $DIR`}
do
  touch $WHATEVER
done

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" .
# 4  
Old 12-26-2008
Hi,

glen your script cannot work for two reasons:

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

Try instead:

Code:
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
# 5  
Old 12-26-2008
Quote:
Originally Posted by sussane
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.
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

Last edited by drugplant; 12-26-2008 at 06:01 PM.. Reason: more precise...
# 6  
Old 12-27-2008
Question

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
# 7  
Old 12-27-2008
Quote:
Originally Posted by sussane
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.
[/code]

What do you expect a failure of the touch command to tell you?
Quote:
[code]
With touch i just wanted to the file to reflect the current sysdate and time.

i was thinking something like this

Please put code inside [code] tags.
Quote:
Code:
base_dir=/app/data
var1=/eng
var2=/med
var3=/bsc

for <list of direcotries> in <parentdir>


Don't you mean:

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

Quote:
Code:
do
  for i in *.20081230*   // searchin each of directories /data ,/med, /bsc for files to touch
  do
     touch *


Why touch everything on every iteration of the loop?

If you do that, you will have no idea which file failed to be touched.
Quote:
Code:
     if [ -ne 0 ]


Syntax error. That should be:

Code:
if [ $? -ne 0 ]

Quote:
Code:
       then
         echo "failed to touch `$1` "


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

Code:
echo "failed to touch '$1' "

Quote:
Code:
     fi
   done


Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Move several files into specific directories with a loop

Hello, I'm a first time poster looking for help in scripting a task in my daily routine. I am new in unix but i am attracted to its use as a mac user. Bear with me... I have several files (20) that I manually drag via the mouse into several named directories over a network. I've used rsync... (14 Replies)
Discussion started by: SonnyClark
14 Replies

2. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

3. UNIX for Dummies Questions & Answers

Loop over certain user directories and find files

Hello I have user directories that contain /temp directory. Example folders: /user1/temp/ /user2/temp/ /user3/temp/ How can i loop over all user directories and find all files only in their /temp folder? Thanks a lot for help! (3 Replies)
Discussion started by: flavius42
3 Replies

4. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

5. Shell Programming and Scripting

loop directories mv files to target in range

Hello, Currently I have a painstaking process that I use to move file for a monthly archive. I have to run the same two commands for 24 different directories. I wish to have a script with a for loop automate this and I have not been able to succeed. Here is what I do 24 times. I know this is... (5 Replies)
Discussion started by: jaysunn
5 Replies

6. UNIX for Dummies Questions & Answers

Loop through Sub Directories and search for set of files

I have the below directory in unix environment /home/bkup/daily: ls -lrt drwxrwx--x 2 user user 256 Jan 12 18:21 20110112/ drwxrwx--x 2 user user 256 Jan 13 17:06 20110113/ drwxrwx--x 2 user user 256 Jan 14 16:44 20110114/ drwxrwx--x 2 user user ... (2 Replies)
Discussion started by: prasannarajesh
2 Replies

7. Shell Programming and Scripting

Loop to move files in different directories

Hi, I have various log files in different paths. e.g. a/b/c/d/e/server.log a/b/c/d/f/server.log a/b/c/d/g/server.log a/b/c/h/e/server.log a/b/c/h/f/server.log a/b/c/h/g/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log and above these have an archive folder... (6 Replies)
Discussion started by: acc01
6 Replies

8. UNIX for Dummies Questions & Answers

Touch all files and subdirectories (recursive touch)

I have a folder with many subdirectories and i need to set the modified date to today for everything in it. Please help, thanks! I tried something i found online, find . -print0 | xargs -r0 touch but I got the error: xargs: illegal option -- r (5 Replies)
Discussion started by: glev2005
5 Replies

9. Shell Programming and Scripting

Script to loop through all files and directories.

I'm trying to write a script that will loop through all files and directories down from a path I give it, and change the permissions and ACL. I was able to do the obvious way and change the files and folders on the same level as teh path...but I need it to continue on deeper into the file... (2 Replies)
Discussion started by: cheetobandito
2 Replies
Login or Register to Ask a Question