I needed to find all the files that matched a regular expression. In this case all the files that had either
400x220 and where either
You can do that pretty easily in BASH using
find but the more filetypes and conditions the longer the command.
For example the following would be used to find all png and jpg files in /tmp folder:
find /tmp -name '*.png' -or -name '*.jpg'
Enter regular expressions 🙂
findcommand in MacOS and in GNU/Linux (Ubuntu, Debian, etc..) are slightly different and not all syntax can be used between systems.
It’s a really useful magic but really hard to learn.
What is a regular expression?
A regular expression is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as
*.txtto find all text files in a file manager. The regex equivalent is
find -E . -regex '.*\.(jpg|png)'
find ./ -regextype posix-extended -regex '.*(jpg|png)$'
Find all the jpg and png files which have 300×200 or 400×220 in their filenames.
find -E . -regex '.*(300x200|400x220)\.(jpg|png)'
find ./ -regextype posix-extended -regex '.*(300x200|400x220)\.(jpg|png)$'
Those commands will find all the files of the type something-300×200.jpg or somethingelse400x220.png
So now you see a pattern. You can use the same regular expression on MacOS and Linux (the part between
' ) if you change the sintax and add an
$ at the end in the case of Linux.
Here are some useful regular expression you may want to know.
Find all the png and jpg files with
SAMsomewhere in the filename
This would find, among others, the following files
find -E . -regex '.*(SAM).*\.(jpg|png)'
find ./ -regextype posix-extended -regex '.*(SAM).*\.(jpg|png)$'
Now let’s learn
xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command. –Wikipedia
It’s great to be able to find all those files, but you should be able to do something with them once you have found them, right?
xargs comes in. Let’s say you would like to remove all the jpg and png files that have
SAM in the filename.
find -E . -regex '.*(SAM).*\.(jpg|png)' | xargs rm
find ./ -regextype posix-extended -regex '.*(SAM).*\.(jpg|png)$' | xargs rm
That was easy, right? And it’s really handy to be able to delete some files from all folders. For example all the automatically-made thumbnails in WordPress and other systems. Let’s say you have to clean a folder with several sub-folders and only leave the original files. On a Mac you can do this (add or remove other resolutions) and remove all the files with a resolution in their name (for example
find -E . -regex '.*(1024x576|278x380|506x380|1024x576|508x380|1024x681|550x366|550x309|350x220|380x380|768x768|285x380|768x1024|1024x1024|213x380|576x1024|550x367|253x380|681x1024|1024x768)\.(jpg|png)' | xargs rm