How to rename files in a directory such as the following file: PMC21375.pdf.txt, I need to be renamed to 21375.txt. i.e, I need to remove both PMC and pdf from each file name.
6 Answers
rename 's/(PMC|\.pdf)//g' *pdf.txt
Demo :
$ ls *txt
PMC21375.pdf.txt
$ rename -n 's/(PMC|\.pdf)//g' *txt
PMC21375.pdf.txt -> 21375.txt
from the shell prompt. It's very useful, you can put some perl code like I does in a substitution.
You can remove the -n (dry-run mode switch) when your tests become valids.
There are other tools with the same name which may or may not be able to do this, so be careful.
If you run the following command (linux)
$ file $(readlink -f $(type -p rename))
and you have a result like
.../rename: Perl script, ASCII text executable
then this seems to be the right tool =)
If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :
$ sudo update-alternatives --set rename /path/to/rename
(replace /path/to/rename to the path of your perl's rename command.
Last but not least, this tool was originally written by Larry Wall, the Perl's dad.
-
This will not remove the
.pdffrom the file name.William Everett– William Everett2013-10-29 16:39:23 +00:00Commented Oct 29, 2013 at 16:39 -
Post edited accordingly.Gilles Quénot– Gilles Quénot2013-10-29 16:40:36 +00:00Commented Oct 29, 2013 at 16:40
Besides the rename command you can do it directly in bash (just one way of many):
for file in *pdf.txt; do mv $file ${file//[A-Z.]}.txt ; done
Edited to show Pinyaka how it works:
hmontoliu@ulises2:/tmp/foo$ touch PCM21375.pdf.txt PCM21376.pdf.txt
hmontoliu@ulises2:/tmp/foo$ ls
PCM21375.pdf.txt PCM21376.pdf.txt
hmontoliu@ulises2:/tmp/foo$ for file in *pdf.txt; do mv $file ${file//[A-Z.]}.txt ; done
hmontoliu@ulises2:/tmp/foo$ ls
21375.txt 21376.txt
-
This won't remove the
.pdffrom the filename.William Everett– William Everett2013-10-29 16:22:12 +00:00Commented Oct 29, 2013 at 16:22 -
have you tried it? because for me it does. Shown in the edit.hmontoliu– hmontoliu2013-10-29 16:41:24 +00:00Commented Oct 29, 2013 at 16:41
-
My bad. I thought it would just convert capital letters. Upvoted.William Everett– William Everett2013-10-29 16:44:35 +00:00Commented Oct 29, 2013 at 16:44
There are several utilities for renaming files, but one of the easiest to use is rename. In your case, you can probably do:
rename PMC '' *txt
rename .pdf '' *txt
The first parameter is the part of the filename to replace. The second parameter is the replacement string. Here I am using '' to represent the empty string. All following parameters are files to rename, here I used *txt to limit the replacement to text files.
rename should already be present if you are running Linux. If you are running a *BSD or OS X, you may have to install it first.
-
I don't thing there's a GNU
rename. There's one fromutil-linux, and one fromperlwith different syntax. The one you're refering to looks like the one fromutil-linuxthat is sometimes calledrename.ulso as not to be confused with therenamefromperl, while on some other systems,renameis the one from util-linux and the one fromperlis calledprename.Stéphane Chazelas– Stéphane Chazelas2013-10-29 15:36:02 +00:00Commented Oct 29, 2013 at 15:36 -
1Mine is the one from util-linux. I mistakenly thought it was part of GNU coreutils.wingedsubmariner– wingedsubmariner2013-10-29 15:40:12 +00:00Commented Oct 29, 2013 at 15:40
-
The default Debian version of rename would be something like this
rename 's/PMC// *.txtandrename s/\.pdf// *.txtWilliam Everett– William Everett2013-10-29 16:05:19 +00:00Commented Oct 29, 2013 at 16:05
First make a backup of your files! Then basically you can use a python one-liner in the current directory with the general form:
python -c "import glob,os; [os.rename(fn, fn.replace('PATTERN', 'REPLACE')) for fn in glob.glob('PATH/*')]"
in your specific case, you can use this twice:
python -c "import glob,os; [os.rename(fn, fn.replace('.pdf', '')) for fn in glob.glob('*')]"
to remove .pdf and again:
python -c "import glob,os; [os.rename(fn, fn.replace('PMC', '')) for fn in glob.glob('*')]"
to remove PMC.
Try this:
for i in *.pdf.txt; do j=${i//\.pdf/}; j=${j//PMC/}; mv $i $j; done
The j=${i//\.pdf/} means to substitute .pdf in $i with nothing. Likewise with the second j assignment getting rid of the PMC.
If you're using zsh, that's a typical job for zmv:
zmv 'PMC(*).pdf.txt' '$1.txt'
(zmv handles potential conflicts gracefully).