OT: Bash script help needed

Nils Kassube kassube at gmx.net
Thu Dec 20 14:25:02 UTC 2007


Craig Puchta wrote:
> Here is what I have and want to do.
> In one directory I have tens of thousands of .txt files named as such:
>
> rdb-001-001.txt
> rdb-001-002.txt
> rdg-002-001.txt
> rdg-002-002.txt
> and so on.
>
> What I want to do (and not manually, as I haven't even put a dent in it
> yet) is:
>
> mkdir rdb-001
> mv rdb-001-* rdb-001
> mkdir rbg-002
> mv rbg-002-* rbg-002

Depending on the file name pattern, this may be what you are looking for:

find -maxdepth 1 -type f -name \*[0-9][0-9][0-9].txt | while read f;do
  d=${f#./}
  d=${d%-???.txt}
  echo "mkdir -p $d"
  echo "mv $f $d"
done

This only displays what it would do. If that's OK, replace the lines

  echo "mkdir -p $d"
  echo "mv $f $d"

with

  mkdir -p $d
  mv $f $d



Nils




More information about the ubuntu-users mailing list