I have a list of files in directory They are named:


I'd like to combine all of these in this specific order. When I use

cat *pdb > pdb_all.txt 

it seems it just makes a random combination (or perhaps it starts will all files that begin with the number 1 (no matter if it is 1, 10 or, 1000). Can I make this command combine my files in a specific order, or do I rename all my files to contain 4 digits (e.g. 0002, 0003, 0014 etc). I suspect the latter is the easiest way, but how to I do this with the mv command on a mac?

  • You specifically want to use mv (no python)? renaming can be done "virtually", to skip the actual renaming step. – Jacob Vlijm Mar 9 '15 at 11:59
  • I am all new to bash - so it's just because I've tried to use mv before and I sort of understand it. It's nicest to do something that I understand :) – Ditte Mar 9 '15 at 12:08

The easiest way is as you say to rename your files and add leading zeroes. You can do this in the terminal on your mac like this:

for a in *.pdb; do mv $a `printf %05d.%s ${a%.*} ${a##*.}`; done

In this example i set that each number should contain 5 digits. So 1.pdb will be renamed to 00001.pdb. However you could lower that to 4 or whatever suits your need.

Good luck!


To make it work with pdbid.1 suffix, this is the answer:

for a in *.pdbid.1; do mv $a `printf %05d.%s ${a%.*.*} ${a#*.}`; done
  • I don't know how it went wrong for me. I ended up with all my files in the directory deleted and replaced by one new file named 0000.pdb... :S – Ditte Mar 9 '15 at 12:07
  • Are you sure you copied the command exactly as I described it ? It tested it just now on a machine runnint yosemite - and it works there. – stalet Mar 9 '15 at 12:11
  • I've just tried it twice again. I've copied-pasted it, and now it gives me a very long text in the terminal saying "-bash: printf: 102.pdbid: invalid number -bash: printf: 1033.pdbid: invalid number -bash: printf: 1035.pdbid: invalid number -bash: printf: 1073.pdbid: invalid number -bash: printf: 1121.pdbid: invalid number (continuing...) and ending with 0000.936.pdbid0000.955.pdbid0000.966.pdbid0000.978.pdbid0001.: No such file or directory. There are no new files in my directory (I exchanged pdb with pdbid.1 'cause that's my extension and changed 5 to 4) – Ditte Mar 9 '15 at 12:16
  • are there more than one punctuation in your filenames ? – stalet Mar 9 '15 at 12:19
  • don't think so. they are all e.g. 2.pdbid.1 and 14.pdbid.1 – Ditte Mar 9 '15 at 12:20

I would use a combination of find, sort and xargs to achieve something like this.

find ./ -name "*.pdb" | sort -n | xargs cat > pdb_all.txt

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.