Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List only numeric file names in directory

Tags:

linux

bash

I have a list of files with numeric file names (e.g. #.php, ##.php or ###.php) that I'd like to copy/move in one fell swoop.

Does anyone know of an ls or grep combo command to accomplish this objective?

I do have this much:

ls -al | grep "[0-9].php"
like image 461
gurun8 Avatar asked Jun 30 '10 16:06

gurun8


2 Answers

Amend it like this:

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

-E activates the extended regular expressions.

+ requires that at least one occurrence of the preceding group must appear.

\. escape dot otherwise it means "any character."

^ and $ to match the entire filename and not only a part.

Single quotes to prevent variable expansion (it would complain because of the $).

like image 104
UncleZeiv Avatar answered Sep 19 '22 16:09

UncleZeiv


In Bash, you can use extended pattern matching:

shopt -s extglob
ls -l +([0-9]).php

which will find files such as:

123.php
9.php

but not

a.php
2b.php
c3.php
like image 28
Dennis Williamson Avatar answered Sep 19 '22 16:09

Dennis Williamson