Bash:移动文件/目录并创建它的链接

发布于 2025-01-04 11:20:25 字数 1416 浏览 0 评论 0原文

我正在尝试制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2025-01-11 11:20:25

问题是 $destination_path 引用了一个不存在的目录。像这样的事情:

mv /path/to/file.txt /path/to/non/existent/directory/

返回一个错误,并将

mv /path/to/directory/ /path/to/non/existent/directory/

/path/to/directory/ 重命名为 /path/to/non/existent/directory/ (前提是 /path/to/non/existent/ 是一个存在的目录,只是没有名为 directory 的子文件夹。

如果您期望 $destination_path 尚不存在,那么您可以添加 mkdir 命令:

mkdir "$destination_path"
mv "$source_path" "$destination_path"

如果您期望它可能不存在,那么您可以有条件地添加它:

[[ -d "$destination_path" ]] || mkdir "$destination_path"
mv "$source_path" "$destination_path"

如果您期望它确实存在,那么您需要进行一些调试!

(顺便说一句,根据您的具体情况,您可能会发现 mkdir -p 很有帮助。它会递归地创建一个目录所有必需的父目录,但它不会不介意该目录是否已经存在。)

The problem is that $destination_path refers to a directory that doesn't exist. Something like this:

mv /path/to/file.txt /path/to/non/existent/directory/

returns an error, and

mv /path/to/directory/ /path/to/non/existent/directory/

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 named directory).

If you are expecting that $destination_path doesn't already exist, then you can add a mkdir command:

mkdir "$destination_path"
mv "$source_path" "$destination_path"

if you're expecting that it might not exist, then you can add it conditionally:

[[ -d "$destination_path" ]] || mkdir "$destination_path"
mv "$source_path" "$destination_path"

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文