9

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.

10

With 's rename :

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 code like I does in a substitution.

You can remove the -n (dry-run mode switch) when your tests become valids.

warning 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 .pdf from the file name. – William Everett Oct 29 '13 at 16:39
  • Post edited accordingly. – Gilles Quenot Oct 29 '13 at 16:40
4

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 from util-linux, and one from perl with different syntax. The one you're refering to looks like the one from util-linux that is sometimes called rename.ul so as not to be confused with the rename from perl, while on some other systems, rename is the one from util-linux and the one from perl is called prename. – Stéphane Chazelas Oct 29 '13 at 15:36
  • 1
    Mine is the one from util-linux. I mistakenly thought it was part of GNU coreutils. – wingedsubmariner Oct 29 '13 at 15:40
  • The default Debian version of rename would be something like this rename 's/PMC// *.txt and rename s/\.pdf// *.txt – William Everett Oct 29 '13 at 16:05
4

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 .pdf from the filename. – William Everett Oct 29 '13 at 16:22
  • have you tried it? because for me it does. Shown in the edit. – hmontoliu Oct 29 '13 at 16:41
  • My bad. I thought it would just convert capital letters. Upvoted. – William Everett Oct 29 '13 at 16:44
0

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.

0

If you're using zsh, that's a typical job for zmv:

zmv 'PMC(*).pdf.txt' '$1.txt'

(zmv handles potential conflicts gracefully).

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.