![](/img/trans.png)
[英]touch command create multiple files (different names) under one directory
[英]Create directory and files with one command
我想对mkdir
创建一个命令/函数并快速touch
我的目录和文件。
终端: cd PROJECT
:
PROJECT
终端: quickcreate home index.js style.css...
树看起来像:
PROJECT __ home __ index.html
\_ style.css
\_ ...
手动执行:
mkdir home
touch home/index.html
touch home/style.css
touch home/...
我想写一个这样的命令:
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...?
}
您可以使用shift
将位置 arguments 一个一个删除。
不要忘记将目录名和文件名双引号,以便脚本也适用于包含空格的名称。
mkdir "$1"
dir=$1
shift
while (( $# )) ; do
touch "$dir/$1"
shift
done
我推荐-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
这是您可以根据需要使用的另一种方法,即使用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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.