我在 .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 的觸摸版本,因為它可以根據需要在一行/命令中創建父目錄?
uj5u.com熱心網友回復:
沒有觸摸可以創建父目錄路徑,所以用標準的 POSIX-shell 語法撰寫你自己的,也可以與 zsh 一起使用:
#!/usr/bin/env sh
touchp() {
for arg
do
# Creates leading directories if not exists
mkdir -p "${arg%/*}"
# Touch file in-place without cd into dir
touch "$arg"
done
}
uj5u.com熱心網友回復:
與zsh您一起可以:
mkdir -p -- $@:h && : >>| $@
mkdir被賦予每個引數的“頭”來制作目錄(man zshexpn說:h擴展修飾符就像dirname工具一樣作業)。然后,假設您沒有取消設定 MUTLIOS 選項,:(一個不產生輸出的命令)的輸出將附加到檔案中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/412225.html
標籤:
