0

I tried moving files from one directory to another and didn't work. so I searched and found an answer that fits what I want. When I run it, modified to my directory, it gives

What is want to do:

${filename,,*}: bad substitution!

This is what I used:

for filename in *; do
  case "${filename,,*}" in
    b01.nii*)    mv "$filename" "$/Users/dave/Desktop/test/untitled_folder_*/str" ;;
    vol_01.nii*) mv "$filename" "$/Users/dave/Desktop/test/untitled_folder_*/rs" ;;
    *)           echo "don't know where to put $filename";;
  esac
done

Thank you

tsumaranaina
  • 185
  • 1
  • 11
  • 1
    What language is this? – Lightness Races in Orbit Feb 24 '16 at 10:20
  • 1
    Shell (bash) in Mac terminal! – tsumaranaina Feb 24 '16 at 10:22
  • Ok, thanks. Ill try! – tsumaranaina Feb 24 '16 at 10:45
  • Same.. ${filename,,}: bad substitution – tsumaranaina Feb 24 '16 at 10:46
  • Have you put a `$` in `$/Users/dave..` by mistake, was that suppose to be a variable? .. Also your `$filename` could have some trouble chars, try using 2 dashes like this `mv -- "$filename" ..` – riteshtch Feb 24 '16 at 10:56
  • I just realised my mistake but i am still getting the same error! Ok, so i have a file called test. Inside it there are several files called untitled_folder_000 to untitled_folder_999. Inside each there are 2 folders and 2 files. The folders are 'str' and 'rs' and the files are b01.nii and vol_01.nii. I want to put b01.nii inside str and vol_01 inside rs in all files! Thats it! – tsumaranaina Feb 24 '16 at 11:23
  • Are you sure this is being run by bash? Is there a #!/bin/bash line at the top? – William Hay Feb 24 '16 at 11:45
  • the main problem is the `for` command, the variable `$filename` contains `*` and not the real file name. you should use`for filename in $(find . -name "*");do` – ClaudioM Feb 24 '16 at 11:47
  • It might be because you're using an old version of Bash (bash<4). The expansion `${var,,}` appeared in Bash 4. – gniourf_gniourf Feb 25 '16 at 10:16
  • Ok, i gave up, i really have been trying many different version of this code. Any other way around this? – tsumaranaina Feb 26 '16 at 04:00
  • So, i did this now for file in `ls test` do mv /Users/dave/Desktop/test/untitled_folder****/t1.nii /Users/dave/Desktop/test/untitled_folder****/str the reason why i have ** in my code is because i have a lot of files to transfer (1500~). It didn't work! :( – tsumaranaina Feb 26 '16 at 04:34

1 Answers1

0

you two errors:

the main problem is the for command, the variable $filename contains * and not the real file name. you should usefor filename in $(find . -name "*");do

the second one is the case:

you have to be sure about $filename variable's value i.e. in my bash shell the filename output is

./b01.nii 
./vol_01.nii 

it's better to use the following syntax in the case ./b01.nii* )

I've replaced your mv with echo just to test the script:

for filename in $(find . -name "*"); do

  case ${filename} in
    ./b01.nii* )    
                echo "$filename ${filename}/Users/dave/Desktop/test/untitled_folder_*/str" ;;
    ./vol_01.nii* ) 
                echo "$filename ${filename}/Users/dave/Desktop/test/untitled_folder_*/rs" ;;
    *)          
                echo "don't know where to put $filename";;
  esac
done

my output is

sh-4.3$ bash -f main.sh                                                                                                                                                                                                                                 
don't know where to put .                                                                                                                                                                                                                               
don't know where to put ./main.sh                                                                                                                                                                                                                       
don't know where to put ./.cg_conf                                                                                                                                                                                                                      
don't know where to put ./myfile_12345                                                                                                                                                                                                                  
don't know where to put ./myfile_17676                                                                                                                                                                                                                  
don't know where to put ./myfile_9898                                                                                                                                                                                                                   
don't know where to put ./Newfile.sh                                                                                                                                                                                                                    
./b01.nii ./b01.nii/Users/dave/Desktop/test/untitled_folder_*/str                                                                                                                                                                                       
./vol_01.nii ./vol_01.nii/Users/dave/Desktop/test/untitled_folder_*/rs 

Regards

Claudio

ClaudioM
  • 1,418
  • 2
  • 16
  • 42
  • Don't parse the output of `find`! it's broken if there are filenames containing spaces or glob characters. `for filename in *` is the proper way to loop through files in Bash. – gniourf_gniourf Feb 25 '16 at 10:12