Tarun
Tarun

Reputation: 1898

Create multiple files in a directory without change current directory

I'm using command below in my mac machine:

mkdir views && touch views/layout.erb views/home.erb views/about.erb views/contact.erb

It's working as expected. But i'm looking for something, so that i don't have to use directory name(views) to create/touch each file.

Upvotes: 0

Views: 258

Answers (3)

Alok Singhal
Alok Singhal

Reputation: 96131

You can do something like:

mkdir views && touch views/{layout,home,about,contact}.erb

Or you can change the directory in a sub-shell, so your current shell's working directory is unchanged:

mkdir views && (cd views && touch layout.erb home.erb about.erb contact.erb)

Another option is to use a loop:

mkdir views && for f in layout home about contact ; do touch views/${f}.erb ; done

Upvotes: 2

Thomas
Thomas

Reputation: 181745

mkdir views && pushd views && touch layout.erb home.erb about.erb contact.erb && popd

Or:

mkdir views && (cd views && touch layout.erb home.erb about.erb contact.erb)

Upvotes: 1

Rob M.
Rob M.

Reputation: 36511

You can use bash expansion to make this more concise:

mkdir views && touch views/{layout,home,contact}.erb

Upvotes: 4

Related Questions