I have file1.zip, file2.zip, etc. in folder called folder1. have similar folders folder2, folder3, etc.

I need to add folder name to each .zip file inside these folders.

So, files inside folder1 will be folder1_file1.zip, folder1_file2.zip, folder1_file3.zip, etc.

Similarly, folder2 files will be folder2_file1.zip, folder2_file2.zip, folder2_file2.zip, etc.

Thanks a lot!


So, running this script inside the directory that has folder1, folder2, folder3, etc etc, it will look inside each folder and rename each of the files files present to <directory_<filename>.

I believe this is what you were looking for, let me know if it is not what you were looking for.


for x in $(ls `pwd`); do
        if [ -d $x ]; then
                cd $x
                for f in *; do mv $f ${f/$f/$(basename `pwd`)_$f}; done
                cd ..

It's important to note that this is indiscriminant as far as file names go, if there are files inside folder# that you DONT want to rename, you can change the line

for f in *; do mv $f ${f/$f/$(basename `pwd`)_$f}; done


for f in *pattern_you_are_lookingfor*; do mv $f ${f/$f/$(basename `pwd`)_$f}; done
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.