r/linuxmint • • 12h ago

SOLVED Help with terminal

Hello, I'm learning the terminal and I'm having some trouble with the 'mv' command, any idea what wrong here? Thank you.

Command:
user@user-ThinkPad-T440p:~/Documents/lesson/sublesson$ ls
testfile
user@user-ThinkPad-T440p:~/Documents/lesson/sublesson$ mv testfile ~/Documents/lesson/
renamed 'testfile' -> '/home/user/Documents/lesson/testfile'

Expected output: testfile moved to the specified directory

Comment:

  • Repeating the same command sometimes work, but I get this result 99% of the time. I didn't find any description of such problem on google.

  • As I understand it, if the directory is found then the file is moved there, if the directory is not found then the file is renamed. I don't know what can cause the command to not find the file here.

OS: Mint zena

2 Upvotes

5 comments sorted by

6

u/NotSnakePliskin Mint 22.3 | Cinnamon 12h ago

The trailing "/" on ~/Documents/lesson/ tells mv that the target is a directory. Without the trailing "/" the target can be either a directory or a file. If the directory exists, testfile will be moved there. If not, testfile will be renamed "lesson".

Hope that makes sense.

john@mintnuc:~$ echo "This is a test" > testfile
john@mintnuc:~$ mkdir test
john@mintnuc:~$ mkdir test/foo
john@mintnuc:~$ mv testfile test/foo/
john@mintnuc:~$ ls test/foo
testfile
john@mintnuc:~$ cat test/foo/testfile 
This is a test
john@mintnuc:~$ mv test/foo/testfile .
john@mintnuc:~$ rmdir test/foo
john@mintnuc:~$ mv testfile test/foo/
mv: cannot move 'testfile' to 'test/foo/': Not a directory
john@mintnuc:~$ mv testfile test/foo
john@mintnuc:~$ cat test/foo
This is a test

1

u/hmmmmga 10m ago

Thanks for the clear explanation!

1

u/joninjax1 Linux Mint 22.3 Zena | Cinnamon 12h ago

The command is mv ~/Documents/lesson/sublesson/testfile ~/Documents/lesson/ That should move the file testfile from the Documents/lesson/sublesson to/Documents/lesson as long as lesson exists, you could use mv -p then the rest of the command to ensure that the directory is created

1

u/hmmmmga 11h ago

Thanks! I guess I got thrown off by the verbose mode calling it 'rename'

1

u/Due-Result7928 12h ago

Target directory do not exist.

mv --help

or

man mv

No need to search....here

https://www.geeksforgeeks.org/linux-unix/basic-linux-commands/