files and directories with spaces in bash script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


I am trying to write a script to organize/sort my music collection. All of my music is arranged as Artist/Album folders with tracks within them, and most of these names have spaces (if not other strange characters). The script I am trying to get working needs to get all filenames in a specified directory so that I can process each file at a time. I am trying to do this as:


for directory in "$@"; do
  if [[ -d "$directory" ]]; then
    for f in `find $directory -iname '*mp3' `; do
      echo "found mp3 file: $f"

Obviously, $f is getting word-split at each space encountered in $directory as well as in the filename. How can I set bash to give me an array/variable that I loop over for each file found only, irrespective of spaces without using sed to replace the spaces with _ etc.?

I did think of -print0, but could not get to loop over each file and I prefer not to use ls. What are my options with bash, or is it just better to get this written with Perl?



The only way to keep your health is to eat what you don't want, drink
what you don't like, and do what you'd rather not.  -- Mark Twain
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at

[Site Home]     [Audio]     [Hams]     [Kernel Newbies]     [Memory]     [Security]     [Netfilter]     [Bugtraq]     [Photo]     [Yosemite Photos]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Linux Resources]     [Fedora Users]

  Powered by Linux