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
apt-cache show mmv
: mmv is a program to move/copy/append/link multiple files according to a set of wildcard patterns. This multiple action is performed safely, i.e. without any unexpected deletion of files due to collisions of target names with existing filenames or with other target names.
– user1717828
Aug 07 '17 at 14:58
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
rename.ul
which comes with util-linux
, does not give enough flexibility to me while the prename
(perl
script) let me do all the perl
capabilities. So prename
is my way to deal with these stuffs.
– heemayl
Aug 07 '17 at 13:12
echo abc > abc.pdf; echo abc_0 > abc_0.pdf; for f in *.pdf; do pre="${f%.pdf}"; echo mv -- "$f" "${pre}_0.pdf"; done
(The rename
version will skip instead of overwriting.)
– Denis de Bernardy
Aug 07 '17 at 14:11
Do you want to rename or copy?
To rename, you can use emacs:
M-x wdired-change-to-wdired-mode
M-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.
abc_0.pdf
with the content of abc.pdf
, but only after moving/copying abc_0.pdf
to abc_0_0.pdf
first, because abc_0.pdf
comes before abc.pdf
for the bash (with my localisation). When in doubt, just check the file order first with for i in *.pdf; do echo $i; done
.
– dessert
Aug 09 '17 at 11:30
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
rename
command, I got an error: syntax error at (eval 1) line 1, near "."
– wjandrea
Aug 08 '17 at 00:22
rename
reporting the syntax error, not the shell.
– wjandrea
Aug 08 '17 at 04:27
rename
syntax. Try this instead: rename 's/.pdf/_0.pdf/' *.pdf
– wjandrea
Aug 08 '17 at 04:29
rename
from util-linux
, not perl-rename
: http://manpages.ubuntu.com/manpages/trusty/man1/rename.ul.1.html
– muru
Aug 08 '17 at 05:36
abc.pdf
andabc_0.pdf
then the renamedabc.pdf
may overwriteabc_0.pdf
before the latter gets renamed. – Denis de Bernardy Aug 07 '17 at 14:05