Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

bash - Moving files from one subdirectory to another

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...