1

In Linux there are some Folders.

I want to process each of this Folders with the program myprogramm.

I get a list with ls -1 or much better, sure only Folders with:

find . -maxdepth 1 -type d -printf '%f\n' | tr -d '.' | sed '/^[[:space:]]*$/d'

I Can pipe this to xargs.

xargs -I {} -n1 -P1 $(cat $TEMPDIR)/myprogramm -switch1 -switch2 -someoptions {}/ -o {}.info

In one line :

find . -maxdepth 1 -type d -printf '%f\n' | tr -d '.' | sed '/^[[:space:]]*$/d' | xargs -I {} -n1 -P1 $(cat $TEMPDIR/pwd)/myprogramm -switch1 -switch2 -someoptions {}/ -o {}.info

That works so far.

But i am looking for a way to bring a little bit info into it.

Everytime if myprogramm ends, it would be nice to have an output like: Folder 1 of 22 finished

Count how many Folders exist can i do with:

find . -maxdepth 1 -type d -printf '%f\n' | tr -d '.' | sed '/^[[:space:]]*$/d' | wc -l

But how to bring this into the line they process each Folders? And how to count after every one is finished one up?

Next thing, how to bring a random sleep value after every finished myprogramm run and count them down before to work at the next Folder?

A random sleep can do with sleep $(shuf -i 30-60 -n 1).

But how to bring this into, and how to show and count them down?

1
  • 2
    You could start with simply files=(*/) and run a loop for file in "${files[@]}"; do ... done (bash syntax). Call myprogram and add a counter or a random delay as desired in the loop. Commented Jul 13 at 13:59

1 Answer 1

6

A few notes:

  1. unless you have a good reason not to, you should get into the habit of using the find command's null-delimited output options -print0 or -printf '%f\0' to allow subsequent processing to handle all legal filenames.

  2. piping through tr to remove dots seems like a bad idea - especially with your find pipeline, since it will include hidden (dot) directories by default.

  3. If you are piping through sed to avoid running your program once with empty input then note that (at least for GNU xargs) -I implies -L 1 which in turn implies running over only non-empty lines (and there's a -r or --no-run-if-empty if you're not using -I or -L).

  4. OTOH if you are piping through tr then sed simply to exclude the current directory then I'd suggest adding -mindepth 1 (GNU find) or excluding the current directory by pattern i.e. ! -name . instead.

Having said that, if you want a null-delimited list of subdirectories you can use the shell's built-in printf with a simple shell glob, printf '%s\0' */ which avoids the intricacies of find altogether. Set the dotglob shell option if you want to mimic find's default inclusion of hidden files.

  1. bash has a built-in RANDOM so you can replace the external shuf with something like $((30 + RANDOM%30)) .

  2. unless the location of your program really is dynamic, read it once before the start of processing.

So you could do something like

shopt -s nullglob # set dotglob as well if you want to process hidden dirs progdir=$(<$TMPDIR/pwd) printf '%s\0' */ | while IFS= read -r -d '' dir; do echo "$progdir/myprogramm" -switch1 -switch2 -someoptions "$dir" -o "${dir%/}.info" sleep $((30 + RANDOM%30)) done 

However if you're going to use a shell loop anyway, you may as well omit find and store the directory names in an array; a side benefit is that it allows you to access the count before you start and hence easily count down (and avoid a last unnecessary sleep):

shopt -s nullglob # set dotglob as well if you want to process hidden dirs dirs=( */ ) progdir=$(<$TMPDIR/pwd) n=${#dirs[@]} for dir in "${dirs[@]}"; do echo "$progdir/myprogramm" -switch1 -switch2 -someoptions "$dir" -o "${dir%/}.info" printf 'Directories remaining: %d\n' $((--n)) ((n == 0)) || sleep $((30 + RANDOM%30)) done 

In either case, remove the echo once you have checked that it is doing what you expect.

2
  • Is There a way to have a visible timer for the sleep and waiting time they count down the random time? Commented Jul 14 at 0:57
  • 1
    @Banana see How to add a progress bar to a shell script? for dozens of examples of doing progress bars in shell scripts. pick something you like that you feel capable of implementing and combine it with steeldriver's second (array-based) solution above. Commented Jul 14 at 12:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.