We help IT Professionals succeed at work.

fastest way to find files with number range in it.

enthuguy
enthuguy asked
on
Hi Linux experts,
could you help what is the best way to quickly or fastest way to find files with number range please. Basically need to copy files between 4000 and 5000 range.

Since this directory is huge, so my below command takes very long time to execute :)

for i in My_liferay_{4000..4005}*csv; do echo $i; done

Files
My_liferay_4000_1_51.csv
My_liferay_4001_1_52.csv
My_liferay_4002_1_53.csv
.
.
My_liferay_5000_1_60.csv

Open in new window

Comment
Watch Question

Hi ,

if you change to the location where you want your files to be copied in a terminal you can run the script below:

for i in {4000..5000}; do 
   if [ -f  /path/to/My_liferay_${i}*.csv ] ; then 
      cp -p /path/to/My_liferay_${i}*.csv .
   fi 
done

Open in new window

Cheers

Author

Commented:
thanks very much dfke
David FavorFractional CTO
Distinguished Expert 2019

Commented:
One liner to find this type of file...

lxd: net16-david-favor # find . -type f | grep -P '_[45]\d{1,3}_'
./My_liferay_4001_1_52.csv
./My_liferay_4000_1_51.csv
./My_liferay_5000_1_60.csv
./My_liferay_4002_1_53.csv

Open in new window