Create directory and files with one command

Issue

I want to make a command/function to mkdir and touch my directory and files quickly.


Terminal: cd PROJECT:

PROJECT

Terminal: quickcreate home index.js style.css .... The tree looks like:

PROJECT __ home __ index.html
                \_ style.css
                \_ ...

Do manually:

mkdir home
touch home/index.html
touch home/style.css
touch home/...

I want to write a command like this:

function quickcreate {
if [ $# -eq 0 ]
then
  echo "No arg supplied!"
  return 0
else
  mkdir $1
  # how can I do with S2, S3, .... to touch S1/S2 S1/S3...?
}

Solution

I recommend -p.

qc() { local p="$1";
  if [[ -n "$p" ]];
  then mkdir -p "$p" # can be any full or relative path;
  else echo "Use: qc <dirpath> [f1[..fN]]"; return 1;
  fi;
  shift;
  for f; do touch "$p/$f"; done;
}

$: qc
Use: qc <dirpath> [f1[..fN]]

$: cd /tmp
$: qc a/b/c 5 4 3 2 1    # relative path
$: qc a/b                # no files; dir already exists; no problem
$: qc /tmp/a/b/c/d 3 2 1 # full path that partially exists
$: find a                # all ok
a
a/b
a/b/c
a/b/c/1
a/b/c/2
a/b/c/3
a/b/c/4
a/b/c/5
a/b/c/d
a/b/c/d/1
a/b/c/d/2
a/b/c/d/3

Answered By – Paul Hodges

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published