0

I have a large number of files in directories of the format */*/*/*/*.txt and I would like to copy them into a different place while replacing the forward-slashes in the path with underscores. For example, if a file is located at A/B/C/D/E.txt, I'd like to copy it to dest/ so that its path after copying is dest/A_B_C_D_E.txt. Is this possible?

1 Answers1

0

You can use script like this:

for i in `find . -type f -name "*.txt"`
do
newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
mv "$i" "dest/$newfile"
done

If the number of files if very big you can try with while instead of for

while read i
do 
    newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
    mv "$i" "dest/$newfile"
done < (find . -type f -name "*.txt")

P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers

Romeo Ninov
  • 17,484