Skip to content
Advertisement

Bash script, find command, using wildcards or regex

I am writing a bash script that goes over all files in certain directory and:

  1. Picks the files with names that match a specified pattern
  2. Sorts them by data and time (date and time are part of the filename)
  3. Takes X oldest files
  4. Performs certain operations on them

The pattern used to match the files is passed to the script and looks like:

JavaScript

I tried to implement it as following (fields 6 and 7 at the pattern are assumed to contain date and time):

JavaScript

It doesn’t work. Tried various options with -name and -regex…. Most examples online are for much less complicated patterns. Since there might be hundreds of thousands of files to go through, I am looking for a solution that works efficiently. I would like to avoid using sed for readability reasons.

Advertisement

Answer

Your find regex must match the entire path returned by find. For example if you are searching somedir/ for your files, then your regex must match, e.g.

JavaScript

Complicating the picture, is you have multiple types of regex you can use by changing the -regextype option to find, e.g. emacs (default), posix-awk, posix-basic, posix-egrep, posix-extended. (posix-basic has no alteration capability)

posix-egrep is probably the most transferable between your tools like grep, sed, find, etc.. A posix-egrep regex for your pattern searching for the files in somedir/ would be:

JavaScript

Matching against a test with your filenames (with the ending number ranging 0-3 to show the exclusion of files ending in 0, 1) the following example files were used:

JavaScript

Now matching only files that satisfy your criteria and passing for a general sort would yield:

JavaScript

Since you didn’t provide an example of where the time/date was in the filenames, the sorting by time/date is left to you. Let me know if you have further questions.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement