-1

Trying to find a Unix script that will recursively look for directories/files that contain in their name " backup ", "back" etc. or more than 6 numbers, so it can find those that have a date. Thank you!

Edd
  • 7
  • 1

1 Answers1

4

A command that will look for names in the current directory or below that contains the words back (which includes backup), or that contains at least 6 consecutive digits, and print the pathnames of these.

find . \( -name '*back*' -o -name '*[0-9][0-9][0-9][0-9][0-9][0-9]*' \) -print

To do something with these files or directories, do so with -exec from find:

find . \( -name '*back*' -o -name '*[0-9][0-9][0-9][0-9][0-9][0-9]*' \) -exec sh -c '
    for pathname do
        # code that uses "$pathname"
    done' sh {} +

If you want the names to have six digits anywhere rather than consecutively, change the pattern *[0-9][0-9][0-9][0-9][0-9][0-9]* to *[0-9]*[0-9]*[0-9]*[0-9]*[0-9]*[0-9]*.

See also:

Kusalananda
  • 333,661
  • A quick note for the OP - note that this answer includes spaces after the parentheses and before the escape characters. If you omit those spaces, you'll get an "invalid expression" error. – AJM May 12 '20 at 10:22
  • I have another question: what if I want to find files that have at least 4 consecutive digits in their name? Also, thanks for the great response! – Edd May 12 '20 at 10:27
  • Also, if I want to find files that contain " name_05032020 " or " name.05032020 " does it look like this : -name ' . *[0-9][0-9][0-9[0-9][0-9][0-9][0-9][0-9]]" ? I'm sorry if it's completely wrong, I'm still learning :D – Edd May 12 '20 at 10:34
  • @Edd That should be self evident, use *[0-9][0-9][0-9][0-9]*. Each [0-9] matches exactly one single digit. – Kusalananda May 12 '20 at 10:34
  • @Edd To match a dot or an underscore, use [._]. so [._][0-9] would match a dot or an underscore followed immediately by a digit. – Kusalananda May 12 '20 at 10:35
  • @Edd You may want to look at https://unix.stackexchange.com/help/someone-answers, and also take the tour: https://unix.stackexchange.com/tour (you'll receive a badge when you've done so). – Kusalananda May 12 '20 at 10:37
  • @Kusalananda sorry, I meant that I may not know how many digits there are in the name, so I want the script to find me files with at least 4 digits. Also, sorry if these questions are basic/stupid. – Edd May 12 '20 at 10:39
  • @Edd If you only want names with at least four digits, use find . -name '*[0-9][0-9][0-9][0-9]*' – Kusalananda May 12 '20 at 10:45