0

The situation:
I want to copy all odt files with size greater than 0 from one folder into another. Filenames will contain the following characters: + % & [ ( and spaces.

Here is my attempt:

#!/bin/bash

mvfunc ()
{
        if [ -s "$1" ]; then
                cp -n "$1" /home/telas/zztmp/currentfiles/
                /usr/bin/truncate -s 0 "$1"
                return 0
        fi
}

fileslist=$(ls --quoting-style=shell-always /home/telas/zzz/workingfiles/*.odt)

for filename in "$fileslist" ; do
        mvfunc "$filename"
done

All help is very much appreciated!!

techraf
  • 5,941

1 Answers1

1

You should rather use pathname expansion (globbing) in Bash than rely on external commands like ls.

If a for loop contains wildcards, then Bash uses globbing by default, so special characters won't mess like they would if interpreting the parameters given as a text string resulting from command substitution.

And the code will be cleaner and more portable, because --quoting-style= option is specific to GNU ls:

#!/bin/bash

mvfunc ()
{
        if [ -s "$1" ]; then
                cp -n "$1" currentfiles
                /usr/bin/truncate -s 0 "$1"
                return 0
        fi
}

for filename in /home/telas/zzz/workingfiles/*.odt; do
        mvfunc "$filename"
done
techraf
  • 5,941