Mv doesn't recognize wildcards / variable expansion

I am trying to move all files in a series of directories to a subdirectory of their respective folder. There are many folders that need this, so I put them in a loop. For example, I have reduced the number.

read var1
case ${var1} in a) sub="sub_directory1";; b) sub="sub_directory2";; esac

for ((i=1; i<=5; i++)); do
   case ${i} in
     1) t=a;; 2) t=b;; 3) t=c;; 4) t=d;; 5) t=d;;
   esac
   mv "${location[files]}${t}/*.*" "${location[files]${t}/${sub}
done

      

${location[files]}

, ${t}

and ${sub}

- all directories, so the structure looks something like this:

/files/a/file1.txt
/files/a/file2.txt
/files/a/sub_directory1
/files/a/sub_directory2
/files/b/file33.txt.3824
/files/b/file52f.log.345
/files/b/sub_directory1
/files/b/sub_directory2

      

so on, etc. The idea is that files in /files/a/

will be migrated to files/a/sub_directory1

.

When I run this in a script it seems to expand the variables correctly, but obviously not the correct path for the mv. I get

mv: cannot rename files/a/*.* /files/a/sub_directory1/*.*:
No such file or direbctory

      

When I do the same command manually:

mv /files/a/*.* /files/a/sub_directory1

      

it works as intended.

Is this what wildcards are treated literally?

+3


source to share


1 answer


The double quote prevents word splitting and globes while allowing variable expansion, i.e. "*.*"

is a literal letter *.*

. To make globes by filename, you can:

mv "${location[files]}${t}"/*.* "${location[files]}${t}/${sub}"

      



More information on the Bash manual.

+2


source







All Articles