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
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.
List only the . txt files in the directory: ls *. txt. List by file size: ls -s.
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.
/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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With