Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For instance, if there is dir 3:

mkdir 4; cp file 4

The dir could also be called 3 other text. Still no text needs to be in mkdir 4 besides the incremented number. The highest number needs to be detected.

share|improve this question

Lets assume you have sequentially numbered directories like: 'dirnameN',
where 'N' is the number.

You can find the highest number (from the parent directory) by,

find . -type d -name dirname\* | sed 's|dirname||' | sort -n | tail -1

So, you use the pattern in your directory names to filter it out (with 'sed') and leave the number.
Then, you sort numerically to find the last number.

After that, you would add one to that and proceed to make your next directory using the same pattern.

Say, something like,


will give you 'dirname12'.

share|improve this answer

The following should do the thing in one line:

mkdir $(($(find . -type d | sed -e 's/[^0-9]*//g' | sort -n | tail -n 1)+1))

Given the folders:

1 something
3 other text
some 5 thing
2 test

this creates a folder named 6.

If you then want to copy a file named file, just do something like this:

name=$(($(find . -type d | sed -e 's/[^0-9]*//g' | sort -n | tail -n 1)+1)); mkdir $name; cp file $name
share|improve this answer
Thanks, this does exactly what I wanted. Accepted. – nnbc Jun 3 '11 at 17:48
@nnbc Glad it helped. Don't forget to click the checkbox under the score at the left of the post if this answered your question! :) – slhck Jun 3 '11 at 18:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.