Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 591
PerseP Avatar asked May 29 '15 10:05

PerseP


People also ask

Can file names have numbers?

The file directory displays file names in alphanumeric order. To maintain the numeric order when file names include numbers it is important to include the zero for numbers 0-9.

How do I list only text files?

List only the . txt files in the directory: ls *. txt. List by file size: ls -s.

How do I list only file names in Linux?

Another new method to list the files in a terminal is by using the “find” command. Our first method will be using the “find” keyword along with the “maxdepth flag keyword within the command. The keyword “-maxdepth” along with the number “1” means we will be looking for the files only in the current directory.

How do I list only file names in Windows?

/W - Displays only filenames and directory names (without the added information about each file) in a five-wide display format. dir c:*. This form of the DIR command will also display directories. They can be identified by the DIR label that follows the directory name.


1 Answers

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).

like image 138
Biffen Avatar answered Oct 10 '22 03:10

Biffen