1

I have some images named with generated like this: a.jpg a_3558.jpg a_3560.jpg a_3561.jpg, now i want to find out a.jpg , just it, and move it to another folder, how to write the command.

3 Answers 3

1

Try the below find command which uses sed regex on the directory where the files you want to move are being stored.

find . -regextype sed -regex ".*/[a-zA-Z0-9]\+\.jpg" -exec mv "{}" /path/to/destination \;

OR

find . -regextype sed -regex ".*/[^._]\+\.jpg" -exec mv "{}" /path/to/destination \;

Update:

mv $(find . -name "*.jpg" -type f | grep -P '^(?!.*_\d+\.jpg).*') /path/to/destination
Sign up to request clarification or add additional context in comments.

5 Comments

all right, I did not describe well. there are lots of files such as b.jpg b_258.jpg b_6693.jpg, 789c.jpg 789c_77885.jpg 789c_14.jpg, what is mean is only find a.jpg b.jpg, 789c.jpg ... without any"_{NO.}", and move them
add it to the question.
,thanks for your help. I try, but seems it dose not work. my question is assume in folder temp, there are files "a.jpg,a_3558.jpg,a_3560.jpg a_3561.jpg,b.jpg,b_258.jpg,b_6693.jpg,789c.jpg,789c_77885.jpg,789c_14.jpg" I want to find out files a.jpg,b.jpg,789c.jpg, means file names without any "_(here are numbers).jpg"
It works for me.What's the output of find . -regextype sed -regex ".*/[a-zA-Z0-9]\+\.jpg" command?
thanks it works for "a.jpg,a_3558.jpg,a_3560.jpg a_3561.jpg,b.jpg,b_258.jpg,b_6693.jpg,789c.jpg,789c_77885.jpg,789c_14.jpg", but it can not work for, for example "Stian_Lou_Altada_140mm_Pub.jpg,Stian_Lou_Altada_140mm_Pub_3529.jpg,Stian_Lou_Altada_140mm_Pub_3530.jpg,Stian_Lou_Altada_140mm_Pub_3531.jpg", I want to find out Stian_Lou_Altada_140mm_Pub.jpg, this does not work
1

Only if you do not know where a.jpg is, and only if you know there is only one a.jpg

find . -type f -name "a.jpg" -exec mv "{}" /anotherfolder \;

Comments

0

To do this with a regex, you would do something like this:

find /basefoldertosearch -regex '.*/a\.jpg' -exec mv "{}" /anotherfolder \;

Notes: the pattern matches the entire path, not just the filename. The first dot, without the "\" before it, will match anything (and, because of the star, any number of). The "." in the filename, because it has a "\" before it, will match a dot. So, this pattern will match any path+filename that ends in "/a.jpg". Since this includes '*', it MUST be quoted.

Alternatively, since you know the exact filename, you can search for a specific filename.

find /basefoldertosearch -name a.jpg -exec mv "{}" /anotherfolder \;

Note that this one does NOT include the path. It is also not a regular expression; it searches for a filename that is exactly what is specified. In this case, since it does not have a '' (and a '' would not, in fact, work), it does not need to be quoted.

Note that, if either of these finds more than one match, it will copy all of them, one by one, to the same destination; each will overwrite the previous one, and you will be left with the LAST file it found.

ADDITIONAL: After reading the comments below, perhaps this is what you are looking for:

    find /basefoldertosearch ! -regex '.*[0-9].*' -type f -exec mv "{}" /anotherfolder \;

This will find everything that does NOT (because of the !) match the regex: any characters .*, followed by a digit [0-9] followed by any other characters .*. Anything that is not a normal file (e.g. a director) is then removed from the list (otherwise you will move /basefoldertosearch, and find would have nothing left to do!)

2 Comments

all right, I did not describe well. there are lots of files such as b.jpg b_258.jpg b_6693.jpg, 789c.jpg 789c_77885.jpg 789c_14.jpg, what is mean is only find a.jpg b.jpg, 789c.jpg ... without any"_{NO.}", and move them
my english is poor, sorry, maybe i did not describe clearly, find files without any suffix "_Numbers.jpg"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.