ichimok
ichimok

Reputation: 3067

How to create files with folder names recursively using script?

I have a folder structure that can have x subfolders, like so:

I want to create an empty file that has the name of the sub-folders (with .file appended) in their parent folders, as presented here:

Some folders might have spaces in their names, so I've tried the following but it's not working even though echoing $(dirname $dir)/$dir seems to yield the expected result:

#!/bin/bash

find . -mindepth 1 -type d | while read dir
do
    touch $(dirname $dir)/$dir.file
done

What would be the best way to achieve this? Thank you so much in advance!

post updated to try to be clearer

Upvotes: 0

Views: 423

Answers (2)

phranz
phranz

Reputation: 69

perhaps you could try:

find . -type d | while IFS= read -r d
do 
    ( cd "$d" && cd .. && touch "$(basename "$d").file" ) 
done

Upvotes: 1

Shigufta Yasmin
Shigufta Yasmin

Reputation: 1

I have tried with awk, check this if its useful for you. I am giving .file for file name

 realpath Fol*/* | awk '{print "touch "$1".file"}' | sh

Upvotes: 0

Related Questions