I know you can do mkdir
to create a directory and touch
to create a file, but is there no way to do both operations in one go?
我知道你可以用mkdir创建一个目录和一个触摸来创建一个文件,但是没有办法一次同时做这两个操作吗?
i.e. if I want to do the below when the folder other
does not exist:
例如,如果我想在其他文件夹不存在时做以下操作:
cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
Error:
错误:
cp: cannot create regular file `/my/other/path/here/cpedthing.txt': No such file or directory
Has anyone come up with a function as a workaround for this?
有人想出一个函数来解决这个问题吗?
11 个解决方案
#1
98
Use &&
to combine two commands in one shell line:
使用&&将两个命令合并到一个shell行中:
COMMAND1 && COMMAND2
mkdir -p /my/other/path/here/ && touch /my/other/path/here/cpedthing.txt
Note: Previously I recommended usage of ;
to separate the two commands but as pointed out by @trysis it's probably better to use &&
in most situations because in case COMMAND1
fails COMMAND2
won't be executed either. (Otherwise this might lead to issues you might not have been expecting.)
注:之前我推荐使用;要分离这两个命令,但是正如@trysis指出的,在大多数情况下最好使用&,因为在COMMAND1失败的情况下,COMMAND2也不会被执行。(否则这可能会导致你可能没有预料到的问题。)
#2
60
You need to make all of the parent directories first.
您需要首先创建所有父目录。
FILE=./base/data/sounds/effects/camera_click.ogg
mkdir -p "$(dirname "$FILE")" && touch "$FILE"
If you want to get creative, you can make a function:
如果你想要有创意,你可以做一个功能:
mktouch() {
if [ $# -lt 1 ]; then
echo "Missing argument";
return 1;
fi
for f in "$@"; do
mkdir -p -- "$(dirname -- "$f")"
touch -- "$f"
done
}
And then use it like any other command:
然后像其他命令一样使用它:
mktouch ./base/data/sounds/effects/camera_click.ogg ./some/other/file
#3
12
#!/bin/sh
for f in "$@"; do mkdir -p "$(dirname "$f")"; done
touch "$@"
#4
12
Do it with /usr/bin/install:
用/usr/bin/install:
install -D /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
when you don't have a source file:
当你没有源文件时:
install -D <(echo 1) /my/other/path/here/cpedthing.txt
#5
11
you can do it in two steps:
你可以分两步完成:
mkdir -p /my/other/path/here/
touch /my/other/path/here/cpedthing.txt
#6
6
This is what I would do:
这就是我要做的:
mkdir -p /my/other/path/here && touch $_/cpredthing.txt
mkdir -p /my/other/path/here & touch $_/cpredthing.txt
Here, the $_
is a variable that represents the last argument to the previous command that we executed in line.
在这里,$_是一个变量,它表示我们在行中执行的前一个命令的最后一个参数。
As always if you want to see what the output might be, you can test it by using the echo
command, like so:
如往常一样,如果您想了解输出是什么,可以使用echo命令进行测试,如下所示:
echo mkdir -p /code/temp/other/path/here && echo touch $_/cpredthing.txt
echo mkdir -p /code/temp/other/path/here & echo touch $_/cpredthing.txt
Which outputs as:
输出为:
mkdir -p /code/temp/other/path/here
touch /code/temp/other/path/here/cpredthing.txt
As a bonus, you could write multiple files at once using brace expansion, for example:
作为奖励,您可以使用大括号展开一次编写多个文件,例如:
mkdir -p /code/temp/other/path/here &&
touch $_/{cpredthing.txt,anotherfile,somescript.sh}
Again, totally testable with echo
:
同样,完全可测试的echo:
mkdir -p /code/temp/other/path/here
touch /code/temp/other/path/here/cpredthing.txt /code/temp/other/path/here/anotherfile /code/temp/other/path/here/somescript.sh
#7
2
if [ ! -d /my/other ]
then
mkdir /my/other/path/here
cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
fi
#8
1
no need for if then
statements... you can do it on a single line usign ;
不需要if then语句…你可以在单行usign上做;
mkdir -p /my/other/path/here;cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
-- or on two lines --
——或者用两条线。
mkdir -p /my/other/path/here
cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
-- the -p
prevents error returns if the directory already exists (which is what I came here looking for :))
——如果目录已经存在,-p可以防止返回错误(这正是我在这里寻找的:)
#9
1
In the special (but not uncommon) case where you are trying to recreate the same directory hierarchy, cp --parents
can be useful.
在特殊(但并不少见)的情况下,您试图重新创建相同的目录层次结构,cp——父母可能是有用的。
For example if /my/long
contains the source files, and my/other
already exists, you can do this:
例如,如果/my/long包含源文件,而my/other已经存在,您可以这样做:
cd /my/long
cp --parents path/here/thing.txt /my/other
#10
0
as I saw and test in a unix forum this solves the problem
正如我在unix论坛上看到和测试的那样,这解决了问题
ptouch() {
for p in "$@"; do
_dir="$(dirname -- "$p")"
[ -d "$_dir" ] || mkdir -p -- "$_dir"
touch -- "$p"
done
}
#11
-3
if you want simple with only 1 param snippet :
如果你只想要一个简单的参数片段:
rm -rf /abs/path/to/file; #prevent cases when old file was a folder
mkdir -p /abs/path/to/file; #make it fist as a dir
rm -rf /abs/path/to/file; #remove the leaf of the dir preserving parents
touch /abs/path/to/file; #create the actual file
#1
98
Use &&
to combine two commands in one shell line:
使用&&将两个命令合并到一个shell行中:
COMMAND1 && COMMAND2
mkdir -p /my/other/path/here/ && touch /my/other/path/here/cpedthing.txt
Note: Previously I recommended usage of ;
to separate the two commands but as pointed out by @trysis it's probably better to use &&
in most situations because in case COMMAND1
fails COMMAND2
won't be executed either. (Otherwise this might lead to issues you might not have been expecting.)
注:之前我推荐使用;要分离这两个命令,但是正如@trysis指出的,在大多数情况下最好使用&,因为在COMMAND1失败的情况下,COMMAND2也不会被执行。(否则这可能会导致你可能没有预料到的问题。)
#2
60
You need to make all of the parent directories first.
您需要首先创建所有父目录。
FILE=./base/data/sounds/effects/camera_click.ogg
mkdir -p "$(dirname "$FILE")" && touch "$FILE"
If you want to get creative, you can make a function:
如果你想要有创意,你可以做一个功能:
mktouch() {
if [ $# -lt 1 ]; then
echo "Missing argument";
return 1;
fi
for f in "$@"; do
mkdir -p -- "$(dirname -- "$f")"
touch -- "$f"
done
}
And then use it like any other command:
然后像其他命令一样使用它:
mktouch ./base/data/sounds/effects/camera_click.ogg ./some/other/file
#3
12
#!/bin/sh
for f in "$@"; do mkdir -p "$(dirname "$f")"; done
touch "$@"
#4
12
Do it with /usr/bin/install:
用/usr/bin/install:
install -D /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
when you don't have a source file:
当你没有源文件时:
install -D <(echo 1) /my/other/path/here/cpedthing.txt
#5
11
you can do it in two steps:
你可以分两步完成:
mkdir -p /my/other/path/here/
touch /my/other/path/here/cpedthing.txt
#6
6
This is what I would do:
这就是我要做的:
mkdir -p /my/other/path/here && touch $_/cpredthing.txt
mkdir -p /my/other/path/here & touch $_/cpredthing.txt
Here, the $_
is a variable that represents the last argument to the previous command that we executed in line.
在这里,$_是一个变量,它表示我们在行中执行的前一个命令的最后一个参数。
As always if you want to see what the output might be, you can test it by using the echo
command, like so:
如往常一样,如果您想了解输出是什么,可以使用echo命令进行测试,如下所示:
echo mkdir -p /code/temp/other/path/here && echo touch $_/cpredthing.txt
echo mkdir -p /code/temp/other/path/here & echo touch $_/cpredthing.txt
Which outputs as:
输出为:
mkdir -p /code/temp/other/path/here
touch /code/temp/other/path/here/cpredthing.txt
As a bonus, you could write multiple files at once using brace expansion, for example:
作为奖励,您可以使用大括号展开一次编写多个文件,例如:
mkdir -p /code/temp/other/path/here &&
touch $_/{cpredthing.txt,anotherfile,somescript.sh}
Again, totally testable with echo
:
同样,完全可测试的echo:
mkdir -p /code/temp/other/path/here
touch /code/temp/other/path/here/cpredthing.txt /code/temp/other/path/here/anotherfile /code/temp/other/path/here/somescript.sh
#7
2
if [ ! -d /my/other ]
then
mkdir /my/other/path/here
cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
fi
#8
1
no need for if then
statements... you can do it on a single line usign ;
不需要if then语句…你可以在单行usign上做;
mkdir -p /my/other/path/here;cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
-- or on two lines --
——或者用两条线。
mkdir -p /my/other/path/here
cp /my/long/path/here/thing.txt /my/other/path/here/cpedthing.txt
-- the -p
prevents error returns if the directory already exists (which is what I came here looking for :))
——如果目录已经存在,-p可以防止返回错误(这正是我在这里寻找的:)
#9
1
In the special (but not uncommon) case where you are trying to recreate the same directory hierarchy, cp --parents
can be useful.
在特殊(但并不少见)的情况下,您试图重新创建相同的目录层次结构,cp——父母可能是有用的。
For example if /my/long
contains the source files, and my/other
already exists, you can do this:
例如,如果/my/long包含源文件,而my/other已经存在,您可以这样做:
cd /my/long
cp --parents path/here/thing.txt /my/other
#10
0
as I saw and test in a unix forum this solves the problem
正如我在unix论坛上看到和测试的那样,这解决了问题
ptouch() {
for p in "$@"; do
_dir="$(dirname -- "$p")"
[ -d "$_dir" ] || mkdir -p -- "$_dir"
touch -- "$p"
done
}
#11
-3
if you want simple with only 1 param snippet :
如果你只想要一个简单的参数片段:
rm -rf /abs/path/to/file; #prevent cases when old file was a folder
mkdir -p /abs/path/to/file; #make it fist as a dir
rm -rf /abs/path/to/file; #remove the leaf of the dir preserving parents
touch /abs/path/to/file; #create the actual file