-1

I'm writing a bash script that dumps a specified directory into a destination directory (or the current directory if no destination is specified.) The script works fine if the destination directory is specified, but when I try to use the current directory (via $PWD) I get the following error.

mv: `test' and `/home/user/programming/bash/test' are the same file

The current working directory is just /home/user/programming/bash. For some reason the directory I want to dump is being appended onto the current directory path. The function I've created for the dumping is the following.

dump_dir()
{
    echo "Dumping \"$1\" into \"$2\"..."
    mv -i $1 $2
}

How would I do this using mv? Is there a better command to do this?

EDIT:

Running the command dump_dir test $PWD gives the output

Dumping "test" into "/home/user/programming/bash"...

Then followed by the error which I stated above. For some reason the mv command takes $PWD and appends "test" to the end of it.

EDIT:

To replicate the error open a terminal and type

mv some_directory $PWD

Obviously replace some_directory with an actual directory ;)

EDIT: Again...sorry :)

I'm call the function as follows when the user supplies only the directory to dump.

dump_dir $1 $PWD
sotrh
  • 386
  • 5
  • 13
  • 1
    You're just doing a long form of `mv test test`. Where did you expect `test` to be after you ran `dump_dir test $PWD`? – that other guy Jun 01 '14 at 00:52
  • $1 must be an absolute path to a directory, or a relative path to a directory. – Anthony Rutledge Jun 01 '14 at 01:12
  • I edited my question to include the terminal output. I expected the contents of test to be moved into $PWD. I basically tried to rename the directory test using the current working directory. I'm beginning to think that this is a poor aproach... Thanks for the quick response though! – sotrh Jun 01 '14 at 01:14
  • Can you post an example of the function call with arguments? That would help a lot. Thanks. :-) – Anthony Rutledge Jun 01 '14 at 01:28
  • Basic usage of `mv` is not a programming question. – Ben Voigt Jun 01 '14 at 01:36
  • @BenVoigt you're probably right. I just posted it here because I was writing a script. Next time if i have a question about mv, or cp, or tar, etc. I'll post it on askubuntu.com or somewhere similar. :) – sotrh Jun 01 '14 at 01:41
  • @sotrh: If you have a question about why `mv` is generating an error, or what the right parameters are, that definitely belongs on askubuntu or unix.SE or superuser. If you've figured out that parameters you want to pass to `mv` and can't figure out how to have the script supply the right parameters, that could go here, but askubuntu and unix.SE are also valid. – Ben Voigt Jun 01 '14 at 01:44
  • Stack Overflow should create a transfer question button! :-) – Anthony Rutledge Jun 01 '14 at 01:46

1 Answers1

1

All these answers are way off base. You're trying to do something that makes no sense - mv test test. It doesn't matter how you specify the source and destination, that's what it works out to - trying to "move" a directory to where it already is.

mv test test is a no-op, regardless of whether test is a file or directory or symbolic link or device or whatever. The command never even "looks inside" the thing you're moving to see what it is.

If you want to move the contents of the test directory into your current directory, that's different - that'd be something like mv test/* ., although that won't work with nested directories that have the same name as existing subdirectories.

If you want to move an entire directory tree around the filesystem, use tar or rsync or something.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • The mv test/* did the trick! Thanks Mark! You were right, I did want to move the contents of the folder! Probably should have specified that... :/ – sotrh Jun 01 '14 at 01:38
  • Yeah, because the question was not specific. He was talking about moving a directory, not the contents. – Anthony Rutledge Jun 01 '14 at 01:44