Digger
Digger

Reputation: 17

Create a file in a directory shell

I have a directory named "dir" in which there is an html file and folders named 01 02 03 ... 10. I want to create from the folder "dir", in each folder a file page01.html for the folder 01 and page02.html for the dir 02 etc ..

I have tried:

for i in `ls -a * /`; do
touch page$i.html
done

But it does not create me files in directories.

thank you

Upvotes: 0

Views: 45

Answers (3)

nrb
nrb

Reputation: 312

/tmp/dir> ls -d *
01  02  03  04  05  06  07  08  09  10
/tmp/dir> for d in `ls -d *`; do f=$d/page$d.html ; touch $f; ls $f; done
01/page01.html
02/page02.html
03/page03.html
04/page04.html
05/page05.html
06/page06.html
07/page07.html
08/page08.html
09/page09.html
10/page10.html
/tmp/dir> 

Upvotes: 0

anubhava
anubhava

Reputation: 786136

You can use:

for d in {01..10}; do
   touch "$d/page$d.html"
done

Upvotes: 1

that other guy
that other guy

Reputation: 123690

To create a file pageX.html for each directory X, you can use:

for dir in */
do
  touch "$dir/page$dir.html"
done

This is the safer and less redundant version of for dir in `ls -d */`, which should never be used as it has problems with various types of special characters.

If you have other directories, you can limit it to ones with two digits as the name:

for dir in [0-9][0-9]/
do
  touch "$dir/page$dir.html"
done

Upvotes: 0

Related Questions