PerseP
PerseP

Reputation: 1197

List files that only have number in names

I have a directory with these files:

1.html 22.html 333.html zxc.html

I want to get a list of the html files that only have digits in their name:

1.html 22.html 333.html

I thought this would work

find . -regex '^[0-9]+\.html'

or

ls -al | grep -E '^[0-9]+\.html$'

But I get nothing. My idea is to get the html files with only digits in their names and pass them to sed to do a substitution.I'm using linux and bash

Upvotes: 5

Views: 15617

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74595

Use an extended glob:

$ shopt -s extglob
$ echo +([0-9]).html
1.html 22.html 333.html

With extglob enabled, +(pattern) matches one or more of pattern. Note that I am just using echo to show which files match - how you use the glob depends on what you want to do with it.

To print each file on a separate line, you can use:

printf '%s\n' +([0-9]).html

Each file matching the pattern is passed as a separate argument to printf so you don't have to worry about things like spaces or other interesting characters in filenames.

To iterate over these files, it's as simple as:

for file in +([0-9]).html; do 
    echo "$file"
done

Again, the shell takes care of any potential problems with interesting filenames, so you don't have to worry about it.

Upvotes: 5

Biffen
Biffen

Reputation: 6355

find's -regex matches against the whole path, not just the filename (I myself seem to forget this once for every time I use it).

Thus, you can use:

find . -regex '.*/[0-9]+\.html'

(^ and $ aren't necessary since it always tests against the whole path.)

Using find also has advantages when you want to do something with the files, e.g. using the built-in -exec, -print0 and pipe to xargs -0 or even (using Bash):

while IFS='' read -r -d '' file
do
  # ...
done < <(find . -regex '.*/[0-9]+\.html' -print0)

echo with a glob, ls|grep, etc. tend to stop working when filenames contain spaces (or even newlines) (which I realise won't happen in this case; it's more of a matter of future-proofing and making good habits).

Upvotes: 11

Related Questions