简体   繁体   English

Bash:移动文件/目录并创建一个链接

[英]Bash: move file/directory and create a link of it

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. 我正在尝试制作一个bash脚本,该脚本将文件或目录从源目录移动到目标目录,并将符号链接放置到源目录中。

So, <source_path> can be a file or directory, <destination_dir_path> is the directory where I want the original moved to. 因此, <source_path>可以是文件或目录, <destination_dir_path>是我希望原始文件移动到的目录。

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: 它仅在source是目录时才有效,否则mv返回错误:

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. 该目录不会移动到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%/}"

Can some one please help? 有人可以帮忙吗?

The problem is that $destination_path refers to a directory that doesn't exist. 问题在于$destination_path引用的目录不存在。 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 ). /path/to/directory/重命名为/path/to/non/existent/directory/ (前提是/path/to/non/existent/是存在的目录,只是没有名为directory的子文件夹)。

If you are expecting that $destination_path doesn't already exist, then you can add a mkdir command: 如果期望$destination_path不存在,则可以添加mkdir命令:

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.) (顺便说一句,根据您的实际情况,您可能会发现mkdir -p会有所帮助。它以递归方式创建目录所有必需的父目录,并且不介意该目录是否已存在。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM