Bash:移动文件/目录并创建它的链接
我正在尝试制作一个 bash 脚本,将文件或目录从源目录移动到目标目录,并将其符号链接放入源目录中。
因此,
可以是文件或目录,
是我想要将原始文件移动到的目录。
示例用法:
$ mvln /source_dir/file.txt /destination_dir/
OR
$ mvln /source_dir/dir_I_want_to_move/ /destination_dir/
这是我设法组合起来的,但它不能正常工作。 仅当 source 是目录时它才有效,否则 mv 返回错误:
mv: unable to rename `/source_dir/some_file.txt': Not a directory
并且该目录不会移动到destination_directory,但仅移动其内容。
#!/bin/bash
SCRIPT_NAME='mvln'
USAGE_STRING='usage: '$SCRIPT_NAME' <source_path> <destination_dir_path>'
# Show usage and exit with status
show_usage_and_exit () {
echo $USAGE_STRING
exit 1
}
# ERROR file does not exist
no_file () {
echo $SCRIPT_NAME': '$1': No such file or directory'
exit 2
}
# Check syntax
if [ $# -ne 2 ]; then
show_usage_and_exit
fi
# Check file existence
if [ ! -e "$1" ]; then
no_file $1
fi
# Get paths
source_path=$1
destination_path=$2
# Check that destination ends with a slash
[[ $destination_path != */ ]] && destination_path="$destination_path"/
# Move source
mv "$source_path" "$destination_path"
# Get original path
original_path=$destination_path$(basename $source_path)
# Create symlink in source dir
ln -s "$original_path" "${source_path%/}"
有人可以帮忙吗?
I am trying to make a bash script that moves a file or directory from source directory to destination directory and puts a symlink to it into source directory.
So, <source_path>
can be a file or directory, <destination_dir_path>
is the directory where I want the original moved to.
Sample usage:
$ mvln /source_dir/file.txt /destination_dir/
OR
$ mvln /source_dir/dir_I_want_to_move/ /destination_dir/
This is what I have managed to put together, but it does not work properly.
It works only if source is a directory, otherwise mv returns an error:
mv: unable to rename `/source_dir/some_file.txt': Not a directory
And the directory is not moved into destination_directory but only its contents are moved.
#!/bin/bash
SCRIPT_NAME='mvln'
USAGE_STRING='usage: '$SCRIPT_NAME' <source_path> <destination_dir_path>'
# Show usage and exit with status
show_usage_and_exit () {
echo $USAGE_STRING
exit 1
}
# ERROR file does not exist
no_file () {
echo $SCRIPT_NAME': '$1': No such file or directory'
exit 2
}
# Check syntax
if [ $# -ne 2 ]; then
show_usage_and_exit
fi
# Check file existence
if [ ! -e "$1" ]; then
no_file $1
fi
# Get paths
source_path=$1
destination_path=$2
# Check that destination ends with a slash
[[ $destination_path != */ ]] && destination_path="$destination_path"/
# Move source
mv "$source_path" "$destination_path"
# Get original path
original_path=$destination_path$(basename $source_path)
# Create symlink in source dir
ln -s "$original_path" "${source_path%/}"
Can some one please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
$destination_path
引用了一个不存在的目录。像这样的事情:返回一个错误,并将
/path/to/directory/
重命名为/path/to/non/existent/directory/
(前提是/path/to/non/existent/
是一个存在的目录,只是没有名为directory
的子文件夹。如果您期望
$destination_path
尚不存在,那么您可以添加mkdir
命令:如果您期望它可能不存在,那么您可以有条件地添加它:
如果您期望它确实存在,那么您需要进行一些调试!
(顺便说一句,根据您的具体情况,您可能会发现
mkdir -p
很有帮助。它会递归地创建一个目录和所有必需的父目录,但它不会不介意该目录是否已经存在。)The problem is that
$destination_path
refers to a directory that doesn't exist. Something like this:returns an error, and
will rename
/path/to/directory/
to/path/to/non/existent/directory/
(provided that/path/to/non/existent/
is an existent directory, just without a subfolder nameddirectory
).If you are expecting that
$destination_path
doesn't already exist, then you can add amkdir
command:if you're expecting that it might not exist, then you can add it conditionally:
and if you're expecting that it does exist, then you have some debugging to do!
(By the way, depending on your exact situation, you might find
mkdir -p
to be helpful. It recursively creates a directory and all necessary parent directories, and it doesn't mind if the directory already exists.)