22

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

  • 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. – Peter Cordes Nov 27 '19 at 12:18
41

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.

  • 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 '19 at 2:29
  • 10
    Hey! After 30+ years working with UNIX and Linux, I still learn something new! Thanks. – j4nd3r53n Nov 26 '19 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. – rchard2scout Nov 26 '19 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 '19 at 11:43
  • This command is very often used in, e.g., spectemplates & other packages scripts. – Giacomo Alzetta Nov 28 '19 at 14:02
20

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.

  • 20
    Also there's mkdir -p ~/this_dir/that_dir && touch ~/this_dir/that_dir/new.txt. – waltinator Nov 26 '19 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 '19 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. – Peter Cordes Nov 27 '19 at 12:15
4

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

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

or even

# create function
touchfile ()
{
  # to make directory there
  mkdir -p $(dirname $1)
  # and touch the file
  touch $1
} 

# then just
touchfile /path/to/file/to/touch/woah

EDIT: as pointed out by @wjandrea, there are numerous improvements to be made; look at his gist and use that.

  • 2
    This is great! But there are a few problems: 1) Always quote variables and substitutions. 2) Use -- to protect against paths that look like options. 3) The function runs touch unconditionally. Also it's clearer to use names instead of numbered parameters. So I would write the function like this: gist – wjandrea Nov 28 '19 at 1:17
  • Welcome to Ask Ubuntu btw! Check out the tour. – wjandrea Nov 28 '19 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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