Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a requirement of selecting xml files from a folder of same ID's. E.g. There are files with name

  1. S-000001-0-0.xml
  2. S-000001-0-1.xml
  3. S-000001-0-2.xml
  4. S-000001-0-3.xml
  5. S-000001-0-4.xml
  6. S-000002-0-0.xml
  7. S-000003-0-0.xml

From 1 to 5 Xml belongs to one ID and 6,7 belongs to different ID. My requirement is to select files of type 1 to 5 together and move to a new folder.

share|improve this question

You can use a glob like so:

mv S-000001-*.xml dir-to-move-to
share|improve this answer
Hi Chris,Thanks For the quick reply.Yes your suggestion will work but only for one ID.There are thousand of different ID's.I need to move all the files.If I will use the above method it will only move one similar ID not all the ID's – user32885 Feb 22 '13 at 6:40
@user32885 - Oh, I see -- your ID is the second part of the filename; that didn't seem clear from your question. See my edit, it should do what you want. – Chris Down Feb 22 '13 at 6:42
No Chris,ID is the first part of the filename.This is customer ID for a bank employee.We receive file by the name S-083344-0-0.xml,if this customer has a joint account then we will receive files by name S-083344-0-0.xml,S-083344-0-0.xml,S-083344-0-2.xml.Hope now You have got my point.So i have to move files of joint account as well as single ID account.Single ID will not have 2nd part as -1,-2,-3,it will be simply S-083884-0-0.xml.The ID has fixed 6 characters after S- – user32885 Feb 22 '13 at 6:50
@user32885 I'm confused. You say that the "ID has fixed 6 characters after S-", but you also say that the ID isn't the second group... when the group after S- is the second group. – Chris Down Feb 22 '13 at 7:10
for x in `ls S*.xml | egrep -o '[0-9]{6}' | sort | uniq`; do mkdir -p /tmp/$x; cp S-$x-* /tmp/$x/.; done

This will create a folder ID (for each ID) in /tmp and then will copy there all files with the same ID.

You can replace cp with mv if tests are ok.

I assumed that ID has 6 digits and all other numbers are smaller.

share|improve this answer
Don't parse ls. – Chris Down Feb 22 '13 at 9:43
egrep is throwing error,egrep :illegal option --0 – user32885 Feb 22 '13 at 11:23
please give a real file name example, for those above it works. – Laurentiu Roescu Feb 22 '13 at 12:05
find /path/to/old/directory -name 'S-??????-?-?.xml' -type f -print0 | \
    xargs -r0 mv -vt /path/to/new/directory
share|improve this answer
As far as I can tell the second portion of the filename is the ID, so this will move too much. – Chris Down Feb 22 '13 at 7:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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