Sponsored Content
Top Forums Shell Programming and Scripting How to loop through directories to touch files Post 302271657 by cfajohnson on Friday 26th of December 2008 11:52:11 PM
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

 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
All times are GMT -4. The time now is 08:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy