0

I need to use *.* at the end of a Path that is stored in a variable. Also just one "*" should be OK as well.

finally I want to move files in folders with same name. With a static Path like /share/test/temp/* the for loop worked. Now I have different usernames and the search folder for the for loop changes. Therefore I need a varible there. This doesnt work:

    destDir= $Startdir/$username/
a=$destDir/*.*

for file in $a  ; do
  tmp=${file:0:4};
  filenew=${file%.*}"($tmp)";
  mkdir -- "$filenew";
  mv -- "$file" "$filenew";
done

I have tried to escape, single and double marks and backticks.

a=$destDir/*.*      # not working
a="$destDir/\*"     # not working
a= $destDir'/\*'    # not working
a="$destDir/'\*.\*" # not working

How can I make a pure sting with asterix in it, that will be substitued in the for loop? Or is there any other option to pass a variable to a for loop that stores a path and for knows that he has to search very file in that path?

I would perfer a for loop than find $destDir -name ..

5
  • 3
    Don't put spaces after (or before) the = in an assignment -- that changes their meaning completely. Apr 17, 2023 at 17:32
  • Please edit your question and explain what your code is supposed to do. I suggest to show an example directory structure and file list and the expected result. What about destDir="$Startdir/$username" for file in "$destDir"/*.* ; do ...
    – Bodo
    Apr 17, 2023 at 17:38
  • big sorry, I just found the mistake by myself. I have tu use dobble quotes for every file. The problem was not in the for loop directory, but it was in the commands between do and done. This works: for file in $destDir/*.*; do tmp=$"{file:0:4}"; filenew="${file%.*}""($tmp)"; mkdir -- "$filenew"; mv -- "$file" "$filenew"; done
    – Donaufisch
    Apr 17, 2023 at 17:58
  • this is just a warkaround for my questen. I still cannot use asterix in variables. In a test script I have tried something like this, that doesnt work. destDir= 'share/temp/test' ; destDir=$Startdir/$username/ ; a="$destDir/*.*" ; echo $a ; How Can I use Asterix or "." in bash in a variable?
    – Donaufisch
    Apr 17, 2023 at 18:04
  • Please edit your question and explain what you are doing. As Gordon pointed out, you cannot use var= something, the space breaks the syntax, so your example doesn't make sense. And we need to know if you want * (all files) or *.* (all files whose name contains a .).
    – terdon
    Apr 17, 2023 at 18:16

1 Answer 1

0

There are several stumbling blocks in your script - some of them come into mind immediately:

  1. in your script line: "for file in $a; do" *s in $a are not interpreted by the shell as you expected, instead they are taken literally.

  2. Your *.* and * are not equal: *.* requires a dot in the file names while * does not.

Check with a simplified script like this:

destDir=$HOME
a=$destDir/*
for file in $a;  do echo $file;  done

Warning: if a file name contains whitespace, the for loop will split it into two (or more) file names.

If /share/test/temp/* works for you (which means you have no whitespace in your file names), why not just use:

for file in $destdir/*;  do ...yourstuff...; done

If you have whitespace, you might try:

find $destDir -type f | while read file;  do ...yourstuff...;  done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .