1

Every now and then I have to kill a for loop that looks through hundreds of directories for something and I have yet to find a way to do it with a single command. Every time I do it just skips that directory and moves onto the next until it's gone through them all. How can I stop the entire thing with just a single command?

Example:

for index in $(ls -1 /path/to/direcorties)
 do
  find $index -name some_file.gz | awk -F/ '{print $2}' >> /other/path/file.txt
 done

If I have to kill this all it does it skip the directory it's in and moves to the next which means I have to kill the new PID for every directory in the list until it's gone through them all.

  • 1
    Proposing a duplicate of unix.stackexchange.com/q/48425/117549 -- cas' answer there covers the more general case (beyond telling the sl command to accept interrupts, for that question's particular command). – Jeff Schaller Apr 3 at 19:12
  • OK so let's add a twist to this and say I am running the exact same code from a script and executing it from within the screen command. The same thing happens, kill the pid and it moves on to the next directory. How would you code that so that killing the pid would cause the entire screen process to die? – J Telep Apr 3 at 20:07
3

Apart from suggesting that you reorganize your "parsing of ls" into a single:

find /path/to/directories -name ...

... which would solve a couple problems (directories with spaces in them for one; letting Control-C kill the main process, for another), my suggestion for killing interactive shell processes like this is to suspend them then kill them, with: Control-Z followed by kill %

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.