简体   繁体   中英

Unix command to make file and folders recursively

I know mkdir -p will make directories recursively.

I know touch will create a file recursively.

I know mkdir -p foo/bar; touch foo/bar/baz.txt mkdir -p foo/bar; touch foo/bar/baz.txt will work, but is there a flag or something for touch so I can one-step this?

I'm sure this question has been asked before a million times but for some reason I'm coming up empty.

what about an alias or a function:

function my_touch {
  mkdir -p $(dirname $1) && touch $1
}

my_touch /tmp/a/b/aaa ; ls -l /tmp/a/b/aaa

do one thing and do it well.

if you want to do this one step, just write a bash function or alias.

eg:

function mytouch()
{
    test -z "$1" && exit
    dir=$(dirname $1)
    test -d $dir || mkdir -p $dir
    touch $1
}

The GNU implementation of install can do this:

install -D /dev/null foo/bar/baz.txt
# will create an empty baz.txt file in foo/bar

If you're using OS X without coreutils you have to use functions, like already suggested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM