I want to copy all files *.pdf to *_0.pdf
How can I do it?
I want to copy all files *.pdf to *_0.pdf
How can I do it?
A simple way would be to use the mmv command:
mmv '*.pdf' '#1_0.pdf'
You might need to install it first (available in the Universe repository):
sudo apt-get install mmv
With rename (prename):
rename -n 's/\.pdf$/_0$&/' *.pdf
\.pdf$ matches .pdf at the end of the filename_0: _0$&-n for actual actionWith bash parameter expansion:
for f in *.pdf; do pre="${f%.pdf}"; echo mv -- "$f" "${pre}_0.pdf"; done
pre="${f%.pdf}" saves the portion of the filename before .pdf as variable pre
while mv-ing _0.pdf is appended to $pre: ${pre}_0.pdf
drop echo for actual action
Example:
% rename -n 's/\.pdf$/_0$&/' *.pdf
rename(egg.pdf, egg_0.pdf)
rename(spam.pdf, spam_0.pdf)
% for f in *.pdf; do pre="${f%.pdf}"; echo mv -- "$f" "${pre}_0.pdf"; done
mv -- egg.pdf egg_0.pdf
mv -- spam.pdf spam_0.pdf
Do you want to rename or copy?
To rename, you can use emacs:
M-x wdired-change-to-wdired-modeM-x query-replace to replace '.pdf' with '_0.pdf'C-x C-s to save the bufferDo you want to rename or copy the files?
For both, you can simply use a for loop and mv (move, also renames) or cp (copy):
for i in *.pdf; do mv "$i" "${i/%.pdf/_0.pdf}"; done
or rather
for i in *.pdf; do cp "$i" "${i/%.pdf/_0.pdf}"; done
The quotation marks are only needed if (one of) your files contains spaces.
Quick explanation: ${i/%.pdf/_0.pdf} takes variable i and substitutes “.pdf” by “_0.pdf” if it is found at the end of the string (hence %). Read more about bash's amazing superpowers here.
On sh shell, rename will help you:
rename .pdf _0.pdf *.pdf
Execute the comand on directory with the pdf files.
If you have the files inside directories and want to find all of then and replace:
find . -iname "*.pdf" | fgrep -v _0.pdf | xargs -n1 echo rename .pdf _0.pdf