Anil
Anil

Reputation: 41

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. I know it can simply done as mv file1 /path/to/destination, But I want to create a new command. I'm kind of new Linux user, please help me.

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

Started writing a shell script move.sh as :

#!/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

Upvotes: 1

Views: 865

Answers (2)

user5590296
user5590296

Reputation:

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

#!/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. 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

cp move /bin/

Good luck.

Upvotes: 2

heemayl
heemayl

Reputation: 42127

Create a function:

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

put it in ~/.bashrc to make it permanent.

Now run it as :

move /source /files 

Upvotes: 2

Related Questions