![](/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.