简体   繁体   English

Linux中的自定义命令来移动文件

[英]Custom command in linux to move files

I'm trying to create a command that allows me move a file to another directory. 我正在尝试创建一个命令,该命令允许我将文件移动到另一个目录。 For example when I enter "move file1" in the command line, it should move the file "file1" to another directory. 例如,当我在命令行中输入“ move file1”时,它将把文件“ file1”移动到另一个目录。 I know it can simply done as mv file1 /path/to/destination, But I want to create a new command. 我知道它可以简单地作为mv file1 / path / to / destination完成,但是我想创建一个新命令。 I'm kind of new Linux user, please help me. 我是Linux的新用户,请帮助我。

This is what I tried: Created an alias for move='/home/bin/move.sh' So, now when I type move in the command line, it goes and execute move.sh script 这就是我尝试的方法:为move ='/ home / bin / move.sh'创建了一个别名,因此,现在,当我在命令行中键入move时,它将去执行move.sh脚本

Started writing a shell script move.sh as : 开始将shell脚本move.sh编写为:

#!/bin/bash

mv "$2" "/path/to/destination"

I'm not knowing how to proceed. 我不知道该如何进行。 The whole process might be wrong too. 整个过程也可能是错误的。 Please help me solve this. 请帮我解决这个问题。

Thanks in advance 提前致谢

Create a function: 创建一个函数:

move () { mv -t /path/to/destination "$@" ;}

put it in ~/.bashrc to make it permanent. 将其放在~/.bashrc以使其永久。

Now run it as : 现在将其运行为:

move /source /files 

From here you can read on how to select arguments. 这里您可以阅读如何选择参数。

From here you can read more on how to check for number of arguments. 这里您可以阅读有关如何检查参数数量的更多信息。

move.sh move.sh

#!/bin/bash
if (( $# < 2 )); then
    # TODO: print usage
    exit 1
fi
mv "$1" "$2"

Then you will need to make it executable. 然后,您需要使其可执行。

chmod u+x move.sh

You can remove the .sh part. 您可以删除.sh部分。 It wont change anything. 它不会改变任何东西。

mv move.sh move

And then you should be able to call the file 然后您应该可以调用该文件

move asd /home/

Just make sure that the alias calls the correct file. 只要确保别名调用正确的文件即可。

If you want to make life easier delete the alias and place the file in the /bin/ directory 如果您想使生活更轻松,请删除别名并将文件放置在/ bin /目录中

cp move /bin/

Good luck. 祝好运。

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

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