【发布时间】:2022-01-16 00:50:57
【问题描述】:
我的 .zshrc 中定义了以下两个函数
newdir(){ # make a new dir and cd into it
if [ $# != 1 ]; then
printf "\nUsage: newdir <dir> \n"
else
/bin/mkdir -p $1 && cd $1
fi
}
newfile() { # make a new file, open it for editing, here specified where
if [ -z "$1" ]; then
printf "\nUsage: newfile FILENAME \n"
printf "touches a new file in the current working directory and opens with nano to edit \n\n"
printf "Alternate usage: newfile /path/to/file FILENAME \n"
printf "touches a new file in the specified directory, creating the diretory if needed, and opens to edit with nano \n"
elif [ -n "$2" ]; then
FILENAME="$2"
DIRNAME="$1"
if [ -d "$DIRNAME" ]; then
cd $DIRNAME
else
newdir $DIRNAME
fi
else
FILENAME="$1"
fi
touch ./"$FILENAME"
nano ./"$FILENAME"
}
但我想知道,是否有一种类似于 mkdir -p 的触摸版本,因为它可以根据需要在一行/命令中创建父目录?
【问题讨论】:
-
为什么不使用
mkdir -p? -
@TedLyngmo 我不明白你的问题。函数 newdir 调用 mkdir -p。如果条件满足,函数newfile调用函数newdir,函数调用mkdir -p。
-
您要求类似于
mkdir -p的东西,所以我只是想知道为什么要使用类似的东西而不是mkdir -p(如下面的Léa Gris 所示)
标签: linux bash touch zsh mkdir