

regextype posix-extended -regex '.*(SAM).*\.(jpg|png)$' | xargs rm Let’s say you would like to remove all the jpg and png files that have SAM in the filename. 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? It converts input from standard input into arguments to a command. Xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. regextype posix-extended -regex '.*(SAM).*\.(jpg|png)$' Now let’s learn xargs Result of running the find command with ‘. This would find, among others, the following files Find all the png and jpg files with SAMsomewhere in the filename '.*(SAM).*\.(jpg|png)' Here are some useful regular expression you may want to know. 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. Those commands will find all the files of the type something-300×200.jpg or somethingelse400x220.png regextype posix-extended -regex '.*(jpg|png)$' Find all the jpg and png files which have 300×200 or 400×220 in their filenames. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager. You can think of regular expressions as wildcards on steroids. What is a regular expression?Ī regular expression is a special text string for describing a search pattern. It’s a really useful magic but really hard to learn.

Important: the find command in MacOS and in GNU/Linux (Ubuntu, Debian, etc.) are slightly different and not all syntax can be used between systems. You can do that pretty easily in BASH using find but the more filetypes and conditions the longer the command.įor example the following would be used to find all png and jpg files in /tmp folder: find /tmp -name '*.png' -or -name '*.jpg' In this case all the files that had either 300x200 or 400x220 and where either png or jpg files. I needed to find all the files that matched a regular expression.
