BASH: Find highest numbered filename in a directory where names start with digits (ls, sed)

Posted by Jake on Stack Overflow See other posts from Stack Overflow or by Jake
Published on 2009-10-19T05:09:04Z Indexed on 2010/03/17 9:01 UTC
Read the original article Hit count: 438

Filed under:
|
|
|
|

I have a directory with files that look like this:

001_something.php  002_something_else.php
004_xyz.php        005_do_good_to_others.php

I ultimately want to create a new, empty PHP file whose name starts with the next number in the series.

LIST=`exec ls $MY_DIR | sed 's/\([0-9]\+\).*/\1/g' | tr '\n' ' '`

The preceding code gives me a string like this:

LIST='001 002 004 005 '

I want to grab that 005, increment by one, and then use that number to generate the new filename. How do I do that in BASH?

© Stack Overflow or respective owner

Related posts about sed

Related posts about tr