Shell script to move files between two dirs Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Shell script to move files between two dirs without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have multiple folders which contain subfolders like JAN/ Jan/ FEB/ Feb/ MAR/ Mar/ and so on. I need to move all files from JAN/* to Jan/ , FEB/* to Feb/ and so on. How do I achieve this with a shell script?

Edit
Thanks to @Costas for pointing me in the right direction. His solution will work with Bash 4 and up. Since I had v3 I ended up using this.

for DIR in [A-Z][A-Z]*/
do
    NEWDIR=`echo "$(echo "$DIR" | sed 's/.*/L&/; s/[a-z]*/u&/g')"`
    mv $DIR/* $NEWDIR
done

sed script taken from here.

Answer

For modern bash (which supports case change):

for dir in [A-Z][a-z]*/
do
    mv -t "$dir" ${dir^^}/*
done

In unsupported versions you free to use tr|sed|… conversion instead.

We are here to answer your question about Shell script to move files between two dirs - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji