mkdir 목록 내의 콘텐츠로 텍스트 파일 만들기

mkdir 목록 내의 콘텐츠로 텍스트 파일 만들기

mkdir을 사용하여 추가로 10개의 하위 폴더가 있는 상위 폴더를 만들었습니다.mkdir -p 부모\자식{1..10}. 이제 10개의 하위 폴더 각각에 (상위 폴더 이름).txt라는 txt 파일을 생성해야 합니다.

각 텍스트 문서에도 텍스트 줄이 있어야 하므로 터치 명령을 사용할 수 없습니다.

내 원래 아이디어는 지정된 텍스트 줄이 포함된 각 하위 폴더와 txt 파일을 개별적으로 10번 만드는 루프를 만드는 것이었습니다. 나는 이것이 비효율적이라고 생각했습니다.

단일 명령으로 10개의 하위 폴더와 상위 폴더를 모두 생성할 수 있습니다. 텍스트 줄을 사용하여 효율적인 방식으로 텍스트 파일을 만들려면 어떻게 해야 합니까? 아니면 내 원래 아이디어가 최선의 방법이었나요?

답변1

접근 방식 중 하나는 다음과 같습니다.

mkdir -p parent/child{1..10}
for d in parent/child{1..10}; do echo "This file is stored in ${d##*/}" > "$d/parent.txt"; done

결과 보기:

$ head parent/child*/parent.txt
==> parent/child10/parent.txt <==
This file is stored in child10

==> parent/child1/parent.txt <==
This file is stored in child1

==> parent/child2/parent.txt <==
This file is stored in child2

==> parent/child3/parent.txt <==
This file is stored in child3

==> parent/child4/parent.txt <==
This file is stored in child4

==> parent/child5/parent.txt <==
This file is stored in child5

==> parent/child6/parent.txt <==
This file is stored in child6

==> parent/child7/parent.txt <==
This file is stored in child7

==> parent/child8/parent.txt <==
This file is stored in child8

==> parent/child9/parent.txt <==
This file is stored in child9

관련 정보