2

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!

1 Answer 1

1

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.

#!/bin/bash


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 ..
        fi
done

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

to:

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

You must log in to answer this question.

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