0

With the mkdir command if I want this location to exist ~/test/panda/fat when only the ~ part of it exists so far I have to do something like this:

mkdir ~/test
mkdir ~/test/panda
mkdir ~/test/panda/fat

Though it would be much more useful if I could just run something like mkdir ~/test/panda/fat and rather than just getting this error:

mkdir: cannot create directory ‘/home/$USER/test/panda/fat’: No such file or directory

Actually have it create the directory test in ~, then the directory panda in ~/test, and then finally the directory fat in that sub-directory. Am I missing something? Is there a way of doing this as it would be very useful? What would in fact be even more useful in certain situations is for me also to be able to also automatically create a file at the end so that it would create all those directories and then the file bamboo.txt in fat and for all this to be in the same command (obviously not requiring there to be a file at the end though).

If there is not already a command which can do this (which I would be surprised if there isn't), then how can I make a script to make this so?

I am running Ubuntu GNOME 15.10 with GNOME 3.18.

8

Use the -p option of mkdir:

mkdir -p ~/test/panda/fat

From man mkdir:

-p, --parents
              no error if existing, make parent directories as needed

Example:

% mkdir /tmp/foo/bar
mkdir: cannot create directory ‘/tmp/foo/bar’: No such file or directory

% mkdir -p /tmp/foo/bar    ## Done

Now about a file creation after automatically creating the non-existing directories in between, simply put- you can't do this in one go.

At most, you can add the file generation command after the directory generation one:

mkdir -p ~/test/panda/fat && touch ~/test/panda/fat/bamboo.txt

You can use history expansion for touch command to make it shorter or create a shell function for convenience.


Here is a shell function that will do the job:

create_file_dir() {
    [ "$#" -ne 2 ] && echo 'Insufficient Arguments' ||

        case "$1" in -d)    mkdir -p "$2" && echo "Directory '"$2"' Created" ;;

                     -f)    mkdir -p "${2%/*}" && touch "$2" \
                              && echo "File '"$2"' Created" ;;

                      *)    echo "Invalid Argument"
        esac
}

Put the snippet in your ~/.bashrc, do source ~/.bashrc and use it as:

create_file_dir -d /directories/to/create  ##For only directories
create_file_dir -f /path/to/file  ## For file with intermediate dirs

Caveat: If a file already exists, touch will change the timestamp.

Example:

$ create_file_dir -d /tmp/spam/egg/
Directory '/tmp/spam/egg/' Created

$ create_file_dir -f /tmp/spam/egg/foobar
File '/tmp/foo/bar/foobar' Created

$ create_file_dir /spam/egg
Insufficient Arguments

$ create_file_dir -m /spam/egg
Invalid Argument
  • And what about what I said with also making a file at the end? – user364819 Jan 26 '16 at 16:12
  • @ParanoidPanda You can't do that in one operation.. You can add the file generation command after the directory generation one I.e. mkdir -p ~/test/panda/fat && touch ~/test/panda/fat/foobar – heemayl Jan 26 '16 at 16:17
  • Well, you could probably make a simple script actually which if only -p is used and there is no file extension given at the end (if there is then it will give an error) does as mkdir -p does, however if another option is used instead, for instance, -f then it runs the code with mkdir -p with the touch command and the file name at the end. That shouldn't be too hard to do. – user364819 Jan 26 '16 at 16:35
  • @ParanoidPanda Added.. – heemayl Jan 27 '16 at 2:02
2

Try using the -p argument to create the parent directories as needed.

   -p, --parents
          no error if existing, make parent directories as needed
1

And if you don't want to use the -p option, you could type:

mkdir  ~/test  ~/test/panda  ~/test/panda/fat

Your Answer

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