2

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.

Oliver Salzburg
  • 87,539
  • 63
  • 263
  • 308
nnbc
  • 61

2 Answers2

1

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,

dirnamePattern=dirname
lastDirname=11
newDirname=$((lastDirname+1))
newDirname=${dirnamePattern}${newDirname}

will give you 'dirname12'.

nik
  • 56,306
1

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
slhck
  • 228,104
  • 1
    @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 03 '11 at 18:59