Saturday, July 13, 2013

bash: Batch File Move Rename with Dirname + Basename as output file name

If you have files named:
book/chapter1/page1.txt
book/chapter1/page2.txt
book/chapter1/page3.txt

book/chapter2/page1.txt
book/chapter2/page2.txt
book/chapter2/page3.txt

book/chapter3/page1.txt
book/chapter3/page2.txt
book/chapter3/page3.txt
And want output like this:
book/chapter1-page1.txt
book/chapter1-page2.txt
book/chapter1-page3.txt
book/chapter2-page1.txt
book/chapter2-page2.txt
book/chapter2-page3.txt
book/chapter3-page1.txt
book/chapter3-page2.txt
book/chapter3-page3.txt
Here is the command
for f in $(find . -type f); do cp $f "$(dirname ${f})-$(basename $f)"; echo $f; done
Change cp to mv to remove original files.

No comments:

Post a Comment