Skip to content
Advertisement

Create files in subdirectories with the same name [closed]

Reading a list with A and B data in 2 columns, iterating row by row, I was createding a series of direcoties with the following logic. read first row, data A and B, create folder A and subfolder B ( mkdir -p A/B ) and so on. Well, now, I need to create a corresponding file B.txt ( i.e ) inside the proper subfolder B.

By example, y made some proofs with a single file with the next content.

col1  col2
  a     b
  c     d

I created the foler sructure with the next line:

    awk 'NR>1{system("mkdir -p "$1"/"$2)} my_file.txt 

The result was:

$ tree
a
 |_b
c
 |_d

Now I would like to invoke that directory path, to move or create for example a file named b.txt to the directory with the same name, and so be able to do it with the whole tree, but I could not do it yet

My spected result.

$ tree
a
 |_b
   |_b.txt
c
 |_d
   |_d.txt

Any idia?

Thanks

Advertisement

Answer

perhaps better to do in bash directly since there is nothing requires awk

$ while read -r a b; 
  do mkdir -p "$a/$b"; touch "$a/$b/$b.txt"; 
  done < <(sed 1d file)


$ tree
.
├── a
│   └── b
│       └── b.txt
├── c
│   └── d
│       └── d.txt
└── file

4 directories, 3 files
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement