Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH redirect create folder

Tags:

How can I have the following command

echo "something" > "$f" 

where $f will be something like folder/file.txt create the folder folder if does not exist?

If I can't do that, how can I have a script duplicate all folders (without contents) in directory 'a' to directory 'b'?

e.g if I have

a/f1/
a/f2/
a/f3/

I want to have

b/f1/
b/f2/
b/f3/

like image 386
Baruch Avatar asked Jan 26 '12 17:01

Baruch


1 Answers

The other answers here are using the external command dirname. This can be done without calling an external utility.

mkdir -p "${f%/*}" 

You can also check if the directory already exists, but this not really required with mkdir -p:

mydir="${f%/*}" [[ -d $mydir ]] || mkdir -p "$mydir" 
like image 78
jordanm Avatar answered Nov 01 '22 22:11

jordanm