0

I have a range of numbered files (images) and I want to move them to subdirectory. https://superuser.com/questions/380338/move-a-range-of-numbered-files#= suggests that I use

mv IMG_{001-022}.jpg subdir

The problem is that considering that the number is a time stamp its not consecutive numbers. The above doesn't seem to work in my case.

Question is how would you do to the above in non-consecutive numbers.

Edit: Note in this particular case there are files numbered IMG_045.jpg for example. I only want any file that was between IMG_001-IMG_022

2
  • 2
    What do the file names actually look like?
    – krowe
    Aug 26, 2014 at 0:42
  • added examples in edit
    – sachinruk
    Aug 26, 2014 at 2:32

2 Answers 2

2

To move all files of the form IMG_nnn.jpg where nnn are three digits and not necessarily consecutive, use:

mv IMG_[0-9][0-9][0-9].jpg subdir

To move only files numbered, say, from 001 to 0022, one can use:

mv IMG_{001..022}.jpg subdir

The problem with the above is that it will generate error messages for every file that does not exist. The messages look like:

mv: cannot stat `IMG_014.jpg': No such file or directory

It is simple, though, to ignore those messages. Use:

mv IMG_{001..022}.jpg subdir 2>&1 | grep -v "cannot stat"
2
  • Sorry added slightly more info in edit- I don't want anything above 022.
    – sachinruk
    Aug 26, 2014 at 2:33
  • @Sachin_ruk Does the approach in the updated answer do what you need?
    – John1024
    Aug 26, 2014 at 2:43
0

I would use xargs for this:

ls IMG_{001..022}.jpg 2&> /dev/null|xargs -I {} mv {} subdir/

Also, because I like to first test all of this before I use it, I would start by getting an ls command to print what you want. Once you have that perfect, just add the xargs part. I find this the best way to get it right every time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .