-3

I am trying to group all the subdirectories with the same names to a new directory (create if not present)using awk

I appreciate any help! Thanks

sdu
  • 25
  • 6
  • You said `I have a Directory with folder names like below`NOTE here word folder terminology is used in Windows o.s and directory is *NIX o.s ones. So what you are trying to convey here is confusing, could you please do edit your Question with more details and let us know then? – RavinderSingh13 Nov 01 '19 at 16:36
  • What are the preconditions? Are all those target folders have `L0..` suffix? – RomanPerekhrest Nov 01 '19 at 16:36
  • Sorry, I am a novice Linux user. I am not good with the terminology. Yes, all the folder names have suffix L001,L002 and so on. I am trying to ignore everything after first "_" and create new directory like A1, B1. – sdu Nov 01 '19 at 16:40
  • But why do you want to use awk? Awk doesn't have a reliable builtin for moving files, creating directories etc. – oguz ismail Nov 01 '19 at 16:48
  • Sorry, I was looking at some examples on grouping files with awk on stack overflows. So I was trying some awk commands. The solution need not be an awk command. @oguz ismail – sdu Nov 01 '19 at 16:53
  • Almost identical... https://stackoverflow.com/a/58379836/2836621 – Mark Setchell Nov 01 '19 at 16:55
  • @MarkSetchell Almost, but I don't think that that question has a good answer – oguz ismail Nov 01 '19 at 16:58
  • @oguzismail In what way, please? It has a very robust, succinct, accepted answer - by me. – Mark Setchell Nov 01 '19 at 17:00
  • 3
    @oguzismail Wow! Let's agree to differ. Your *"non-standard"* tool is my extremely powerful *"tried-and-tested"* tool that has been available for all Unixes/Linuxes and macOS since the year dot. The fact it is written in Perl which is maybe difficult to read does not deter me any more from using it than the fact that I don't understand the microcode in my car's Engine Management Unit deters me from driving my car. Anyone else's mileage may vary – Mark Setchell Nov 01 '19 at 17:12
  • Doesn't matter. It's not standard – oguz ismail Nov 01 '19 at 17:19
  • 3
    @oguzismail How is it "not standard"? [Linux Standard Base](http://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Languages/LSB-Languages.html) requires Perl... – Benjamin W. Nov 01 '19 at 17:32
  • @BenjaminW. I was talking about POSIX – oguz ismail Nov 01 '19 at 17:33

1 Answers1

1

I'd use a simple for loop like this:

for dir in ./*_*/; do
  echo mkdir -p "${dir%%_*}"   &&
  echo mv "$dir"* "${dir%%_*}" &&
  echo rm -r "$dir"
done

If its output looks good, remove echoes.

oguz ismail
  • 1
  • 16
  • 47
  • 69