30

Let's say the directory ~/this_dir doesn't exist.

I need to be able to run touch ~/this_dir/new.txt to create new.txt in ~/this_dir.

Is there a simple way to make touch also create the directory?

Or an alternative command which would achieve the same thing?

Thanks

1
  • There's no single system call that can create directories up to a new file, so generally these are different tasks that you can't do with a single invocation of any "traditional" command like touch, and I don't think cp will do this either. The answer using install is neat, though, if you want efficiency in a script moreso than being obvious or clear to readers. Nov 27, 2019 at 12:18

3 Answers 3

48

There is the command install which will accomplish what you are asking for.

install -Dv /dev/null this_dir/new.txt

(source: Bash command to create a new file and its parent directories if necessary)

Explanation:

  • install is used to copy files and set attributes (see man install)
  • -D tells the command to "create all leading components of DEST except the last, or all components of --target-directory, then copy SOURCE to DEST"
  • -v causes to show every creation step (can be omitted of course)
  • /dev/null is the source, from where to copy
  • this_dir/new.txt is the target of the copy operation.

@rchard2scout has thankfully pointed out that

The install command is part of GNU Coreutils, which has been marked as "Essential". That means it'll basically always be available.

5
  • very nice explanation.. do we need to install the package 0r it out of the box? considering 18.04 19.04 19.10
    – PRATAP
    Nov 26, 2019 at 2:29
  • 11
    Hey! After 30+ years working with UNIX and Linux, I still learn something new! Thanks.
    – j4nd3r53n
    Nov 26, 2019 at 10:38
  • 5
    @PRATAP The install command is part of GNU Coreutils, which has been marked as "Essential". That means it'll basically always be available. Nov 26, 2019 at 11:40
  • @rchard2scout Hi, thanks.. after posting my comment, I have tried it and found it is out of the box in my 18.04 19.04 and 19.10. Thanks for your reply.
    – PRATAP
    Nov 26, 2019 at 11:43
  • This command is very often used in, e.g., spectemplates & other packages scripts. Nov 28, 2019 at 14:02
21

I would recommend use the &&.

Example:

mkdir ~/this_dir && touch ~/this_dir/new.txt

The && deals accepts a new command. So mkdir this_dir, also do the rest.

This is very useful because can be used for everything, not only for new folders.

3
  • 21
    Also there's mkdir -p ~/this_dir/that_dir && touch ~/this_dir/that_dir/new.txt.
    – waltinator
    Nov 26, 2019 at 3:24
  • 7
    I like this solution because it is the most easy solution to understand for someone with just basic nix knowledge. It is also very descriptive of what is happening. :-)
    – whirlwin
    Nov 26, 2019 at 14:21
  • 3
    This fails if ~/this_dir does exist. Use mkdir -p to fix that. Or if that behaviour is on purpose, mention it in the answer. Nov 27, 2019 at 12:15
5

Simple solution, given $file as a file, this should work:

mkdir -p $(dirname $file) && touch $file

or even

# create function
touchfile () {
    local file="$1"
    mkdir -p -- "$(dirname -- "$file")" &&
        touch -- "$file"
}

# then just
touchfile /path/to/file/to/touch/woah
1
  • Alternative (but not exactly via touch, though): mkdir {1..10}_directory && touch {1..10}_directory/FILENAME.txt Nov 29, 2022 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.