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?
Asked
Active
Viewed 140 times
0
Danica Scott
- 143
1 Answers
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
./in the path, so this results in filenames starting with._, which isn't ideal – Danica Scott Jan 17 '19 at 02:09._problem by adding to thenewfileline:newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)– Danica Scott Jan 17 '19 at 02:20