if [[ -e $1 && ! -e $2 ]]; then
mkdir --parents --verbose -- "$(dirname -- "$2")"
fi
mv --verbose -- "$1" "$2"
例:
参数:“d1”“d2 / sub”
mkdir: created directory 'd2'
renamed 'd1' -> 'd2/sub'
12
最简单的方法是:
mkdir [directorie name] && mv [filename] $_
让's suppose I downloaded pdf files located in my download directorie ( ~/download ) and I want to move all of then in a directorie that doesn'退出(让我们说"my_PDF") . 我将键入以下命令(确保我当前的工作主管是〜/下载):
mkdir my_PDF && mv *.pdf $_
You can add -p option to mkdir if you want to create subdirectories just like this: (我想要创建一个名为"python"的子目录):
#!/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 $2 | sed -e 's_/[^/]*$__'`
# Move to the directory, making the directory if necessary
mkdir -p "$DIR_NAME" || exit
mv "$@"
216
可能是以下shell脚本?
#!/bin/sh
if [[ -e $1 ]]
then
if [[ ! -d $2 ]]
then
mkdir --parents $2
fi
fi
mv $1 $2
12 回答
我的一个解决方案:
码:
例:
参数:“d1”“d2 / sub”
最简单的方法是:
让's suppose I downloaded pdf files located in my download directorie ( ~/download ) and I want to move all of then in a directorie that doesn'退出(让我们说"my_PDF") .
我将键入以下命令(确保我当前的工作主管是〜/下载):
You can add -p option to mkdir if you want to create subdirectories just like this: (我想要创建一个名为"python"的子目录):
你甚至可以使用大括号扩展:
创建3个目录(directory1,directory2,directory3),
并且在每个子目录中都有两个子目录(subdirectory1,subdirectory2),
并且在每个子目录中都有两个子子目录(subsubdirectory1和subsubdirectory2) .
你必须使用bash 3.0或更新版本 .
这个单行(在bash中)怎么样:
打破这种情况:
创建目录(包括所有中间目录),之后:
将文件移动到该目录($ _展开到传递给前一个shell命令的最后一个参数,即:新创建的目录) .
我不确定这在其他炮弹中会有多大作用,但它可能会给你一些关于要寻找什么的想法 .
以下是使用此技术的示例:
保存为名为mv或mv.sh的脚本
或者放在〜/ .bashrc文件的末尾,作为替换每个新终端上的默认mv的函数 . 使用函数允许bash保持内存,而不是每次都必须读取脚本文件 .
这些基于Chris Lutz的提交 .
你可以使用
mkdir
:一个简单的脚本自动执行(未经测试):
可能是以下shell脚本?
这是基本的部分 . 您可能希望添加一些内容来检查参数,如果目标存在,或者源目录存在,或者不存在(或者不覆盖不存在的内容),您可能希望更改行为 .
这听起来像答案是没有:) . 我没有't really want to create an alias or func just to do this, often because it'一次性,我已经在键入
mv
命令的中间,但我发现了一些效果很好的东西:如果
mv
失败(dir不存在),它将生成目录(这是上一个命令的最后一个参数,因此$_
有它) . 所以只需运行此命令,然后重新运行它,这次mv
应该成功 .Sillier,但工作方式:
使用mkdir -p创建包含共享目标文件名的临时目录的目录,然后使用简单的rmdir删除该文件名目录,然后将文件移动到新目标 . 我认为使用dirname的答案可能是最好的 .