0

I have a directory, that contains hundreds of subdirectories. The structure looks like this:

directory ---> subdirectory_1 ---> file1
          |                  |---> file2
          ---> subdirectory_2 ---> file1
                             |---> file2
          |
          |
          ---> subdirectory_n --> more_files

Now I want to execute a shell command that executes on subdirectories in a continuous manner. Like first on sd1, sd2, sd3 and gives an output. Then on sd2, sd3, sd4 and vice versa.

Now I can do the following to execute a command on all the subdirectories:

'find some_string -maxdepth 1 -type d \( ! -name . \) -exec bash -c "cd \'{}\' && do something" \;'

But I can't seem to find a way to execute them in a continuous manner with a constant interval. Any suggestions?

  • When you say: "Then on sd2, sd3, sd4 and vice versa." , do you mean "Then on sd4, sd5 and sd6, etc..." ? – Gohu Aug 23 '17 at 7:26
  • Yes. So for example I want to list all the files in the subdirectories, then I want sets of those lists in an output text file like: ls of [subdirectory1, subdirectory2, subdirectory3] > output1.txt. Then ls of [subdirectory2, subdirectory3, subdirectory4] > output2.txt and so on till the n-th subdirectory. – DarkMatter Aug 23 '17 at 7:37
  • How can I do subdirectory manipulation? For example, running a command on n-number of sorted subdirectories, where n is an input. Or how can I run a for loop on a range of subdirectories where I can give that range as an input? Like this except how can I define the range here? for d in ["sd1"-"sd2"] do ( cd "$d" && do stuff ) done – DarkMatter Aug 23 '17 at 7:42
1

You can create a loop like this:

while true
do
<your command>
sleep <some time to sleep between executions>
done

But you must set some way to exit the loop, otherwise several instances of this script can overload the machine

  • How can I do subdirectory manipulation? For example, running a command on n-number of sorted subdirectories, where n is an input. Or how can I run a for loop on a range of subdirectories where I can give that range as an input? – DarkMatter Aug 23 '17 at 7:39
  • @0x1, this is another questions. Please ask it separately – Romeo Ninov Aug 23 '17 at 7:47
  • Okay. Added the question here: unix.stackexchange.com/questions/387819/… – DarkMatter Aug 23 '17 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.