1

My script below has two problems:

  • The script stops after first matched directory
  • rm -i is not quite interactive

What are my incorrect assumptions that make it fail? How can I fix it?

ls -1A | xargs -d "\n" \
sh -c '(find "$1" -mindepth 1 -maxdepth 1 \
    -name "*.flac" | read 2>/dev/null) || rm -ri "$1"'
1
  • 1
    Since you say that your script doesn't quite work, please can you state categorically what it is that you want it to do. (Including a definition of your criteria for deletion.) – roaima Mar 29 '15 at 16:15
2

rm's stdin (where it reads the prompt answer from) is /dev/null (set by GNU xargs, some other xargs implementations would keep it as the pipe from ls).

Your sh is getting many arguments at once, but you're only processing one ($1).

Also note that the newline character is as valid as any in a file name which is why you generally can't process the output of ls reliably.

With zsh:

for dir (*(ND/)) {
  flacs=($dir/*.flac(ND.))
  (($#flacs)) || rm -ri -- $dir
}

Otherwise, you could do:

find . ! -name . -prune -type d -exec sh -c '
  for dir do
    find "$dir/." ! -name . -prune -type f -name "*.flac" | read f ||
      rm -ri "$dir"
  done' sh {} +

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.