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...?
}
You can use shift
to remove positional arguments one by one.
Don't forget to double quote the directory and file names so the script works for names containing whitespace, too.
mkdir "$1"
dir=$1
shift
while (( $# )) ; do
touch "$dir/$1"
shift
done
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
This is another method you can use for what you want, that is using arrays :
function quickcreate {
if [ $# -eq 0 ]; then
echo "No arg supplied!"
return 0
else
dir="${@::1}"
files=(${@:1})
mkdir "$dir"
for file in "${files[@]}"; do
touch $dir/$file
done
}
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.