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
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
5 Comments
find . -regextype sed -regex ".*/[a-zA-Z0-9]\+\.jpg" command?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!)