Linux 有没有办法让 mv 创建要移动到的目录(如果它不存在)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/547719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to make mv create the directory to be moved to if it doesn't exist?
提问by Paul Wicks
So, if I'm in my home directory and I want to move foo.c to ~/bar/baz/foo.c , but those directories don't exist, is there some way to have those directories automatically created, so that you would only have to type
因此,如果我在我的主目录中并且我想将 foo.c 移动到 ~/bar/baz/foo.c ,但这些目录不存在,是否有某种方法可以自动创建这些目录,以便你只需要输入
mv foo.c ~/bar/baz/
and everything would work out? It seems like you could alias mv to a simple bash script that would check if those directories existed and if not would call mkdir and then mv, but I thought I'd check to see if anyone had a better idea.
一切都会好起来的?似乎您可以将 mv 别名为一个简单的 bash 脚本,该脚本将检查这些目录是否存在,如果不存在,则调用 mkdir 然后调用 mv,但我想我会检查是否有人有更好的主意。
采纳答案by KarstenF
How about this one-liner (in bash):
这个单行怎么样(在 bash 中):
mkdir --parents ./some/path/; mv yourfile.txt $_
Breaking that down:
打破它:
mkdir --parents ./some/path
creates the directory (including all intermediate directories), after which:
创建目录(包括所有中间目录),之后:
mv yourfile.txt $_
moves the file to that directory ($_ expands to the last argument passed to the previous shell command, ie: the newly created directory).
将文件移动到该目录($_ 扩展为传递给上一个 shell 命令的最后一个参数,即:新创建的目录)。
I am not sure how far this will work in other shells, but it might give you some ideas about what to look for.
我不确定这在其他 shell 中的作用有多大,但它可能会给你一些关于寻找什么的想法。
Here is an example using this technique:
以下是使用此技术的示例:
$ > ls
$ > touch yourfile.txt
$ > ls
yourfile.txt
$ > mkdir --parents ./some/path/; mv yourfile.txt $_
$ > ls -F
some/
$ > ls some/path/
yourfile.txt
回答by strager
You can use mkdir
:
您可以使用mkdir
:
mkdir -p ~/bar/baz/ && \
mv foo.c ~/bar/baz/
A simple script to do it automatically (untested):
一个自动执行的简单脚本(未经测试):
#!/bin/sh
# Grab the last argument (argument number $#)
eval LAST_ARG=$$#
# Strip the filename (if it exists) from the destination, getting the directory
DIR_NAME=`echo | sed -e 's_/[^/]*$__'`
# Move to the directory, making the directory if necessary
mkdir -p "$DIR_NAME" || exit
mv "$@"
回答by Chris Lutz
The following shell script, perhaps?
也许是下面的shell脚本?
#!/bin/sh
if [[ -e ]]
then
if [[ ! -d ]]
then
mkdir --parents
fi
fi
mv
That's the basic part. You might want to add in a bit to check for arguments, and you may want the behavior to change if the destination exists, or the source directory exists, or doesn't exist (i.e. don't overwrite something that doesn't exist).
这是基本的部分。您可能需要添加一点来检查参数,并且您可能希望在目标存在、源目录存在或不存在时更改行为(即不要覆盖不存在的内容) .
回答by Stefan Gruenwald
You can even use brace extensions:
您甚至可以使用大括号扩展:
mkdir -p directory{1..3}/subdirectory{1..3}/subsubdirectory{1..2}
- which creates 3 directories (directory1, directory2, directory3),
- and in each one of them two subdirectories (subdirectory1, subdirectory2),
- and in each of them two subsubdirectories (subsubdirectory1 and subsubdirectory2).
- and in each one of them two subdirectories (subdirectory1, subdirectory2),
- 它创建了 3 个目录(目录 1、目录 2、目录 3),
- 并且在其中的每一个中都有两个子目录(subdirectory1、subdirectory2),
- 并且在每个子目录中都有两个子目录(subsubdirectory1 和 subsubdirectory2)。
- 并且在其中的每一个中都有两个子目录(subdirectory1、subdirectory2),
You have to use bash 3.0 or newer.
您必须使用 bash 3.0 或更新版本。
回答by Sepero
Save as a script named mv or mv.sh
另存为名为 mv 或 mv.sh 的脚本
#!/bin/bash
# mv.sh
dir=""
tmp=""; tmp="${tmp: -1}"
[ "$tmp" != "/" ] && dir="$(dirname "")"
[ -a "$dir" ] ||
mkdir -p "$dir" &&
mv "$@"
Or put at the end of your ~/.bashrc file as a function that replaces the default mv on every new terminal. Using a function allows bash keep it memory, instead of having to read a script file every time.
或者放在 ~/.bashrc 文件的末尾,作为替换每个新终端上默认 mv 的函数。使用函数可以让 bash 保持内存,而不必每次都读取脚本文件。
function mv ()
{
dir=""
tmp=""; tmp="${tmp: -1}"
[ "$tmp" != "/" ] && dir="$(dirname "")"
[ -a "$dir" ] ||
mkdir -p "$dir" &&
mv "$@"
}
These based on the submission of Chris Lutz.
这些基于 Chris Lutz 的提交。
回答by Billmc
mkdir -p `dirname /destination/moved_file_name.txt`
mv /full/path/the/file.txt /destination/moved_file_name.txt
回答by Blarn Blarnson
Sillier, but working way:
更傻,但工作方式:
mkdir -p
rmdir
mv
Make the directory with mkdir -p including a temporary directory that is shares the destination file name, then remove that file name directory with a simple rmdir, then move your file to its new destination. I think answer using dirname is probably the best though.
使用 mkdir -p 创建目录,包括共享目标文件名的临时目录,然后使用简单的 rmdir 删除该文件名目录,然后将文件移动到新目标。我认为使用 dirname 的答案可能是最好的。
回答by Pat
It sounds like the answer is no :). I don't really want to create an alias or func just to do this, often because it's one-off and I'm already in the middle of typing the mv
command, but I found something that works well for that:
听起来答案是否定的:)。我真的不想创建别名或 func 只是为了做到这一点,通常是因为它是一次性的,而且我已经在输入mv
命令的过程中,但我发现了一些适用于此的东西:
mv *.sh shell_files/also_with_subdir/ || mkdir -p $_
If mv
fails (dir does not exist), it will make the directory (which is the last argument to the previous command, so $_
has it). So just run this command, then upto re-run it, and this time mv
should succeed.
如果mv
失败(dir 不存在),它将创建目录(这是上一个命令的最后一个参数,所以$_
有)。所以只要运行这个命令,然后up重新运行它,这次mv
应该会成功。
回答by cab404
$what=/path/to/file;
$dest=/dest/path;
mkdir -p "$(dirname "$dest")";
mv "$what" "$dest"
回答by Abdoul Seibou
The simpliest way to do that is :
最简单的方法是:
mkdir [directorie name] && mv [filename] $_
假设我下载了位于我的下载目录 ( ~/download ) 中的 pdf 文件,然后我想将所有文件移动到一个不退出的目录中(假设 "my_PDF" “my_PDF”)。
我将输入以下命令(确保我当前的工作目录是 ~/download ):
mkdir my_PDF && mv *.pdf $_
You can add -p option to mkdir if you want to create subdirectories just like this: (supposed i want to create a subdirectorie named "python" ):如果你想像这样创建子目录,你可以向 mkdir 添加 -p 选项:(假设我想创建一个名为“python”的子目录):
mkdir -p my_PDF/python && mv *.pdf $_