What's the reason behind having -n option for mv command?


 
Thread Tools Search this Thread
Operating Systems OS X (Apple) What's the reason behind having -n option for mv command?
# 1  
Old 02-28-2016
What's the reason behind having -n option for mv command?

Sorry for a question that may seem dumb but learning UNIX basics I still can not grasp benefits of using
Code:
mv -n source file target file

I can understand the need for
Code:
 cp -n source file target file

when you get a copy with contents untouched but the former baffles me.
I know that this about preventing from overwriting a target file with contents of a source file (not in opposite order, right?) but why rename at all if you don't want to annihilate the target file? What's the point?

Would be glad to feel enlightened on the subject.

Last edited by scrutinizerix; 02-29-2016 at 02:03 PM..
# 2  
Old 02-28-2016
Did you think of moving to a different directory?
# 3  
Old 02-28-2016
Quote:
Originally Posted by scrutinizerix
I still can not grasp benefits of using
Code:
mv -n source file target file

What's the point?
According to the man page of OS X i found in the net "-n" prevents the mv command to overwrite already existing files. That means: if you have files A, B and C and you do a

Code:
mv A D

you will end up with three files again, B, C and D (the former A). Whereas, when you issue:

Code:
mv A B

you will end up with only two files: B (the former A) and C. The original B will be wiped out. This may sometimes be the desired result, but sometimes it may not. To guard against accidentally wiping out a file you might want to keep is the reason "-n" exists.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 02-28-2016
Yeah, understand that. But my point and question was that if you pass -n option so that your command is designed as

Code:
mv -n A B

, then nothing happens. Both A and B are untouched so B IS NOT the former A. Right?
# 5  
Old 02-28-2016
Quote:
Originally Posted by scrutinizerix
Code:
mv -n A B

, then nothing happens.
Not quite, see below.

Quote:
Originally Posted by scrutinizerix
Both A and B are untouched so B IS NOT the former A. Right?
Yes. This is what i'd expect.

I'd also expect some diagnostic message to be issued (i don't have OS X but i suppose it would be something along the lines of "Error: cannot ....") and the return-code to be non-zero. This is a common trait of UNIX-commands: whenenver they cannot do what is asked they will return a non-zero error code. Try the following:

Code:
ls -l /etc/hosts
echo RC is $?
ls -l /some/nonexisting/file
echo RC is $?

/etc/hosts is a file supposed to exist, so the return code should be zero, the second file does not exist, so the error code will be presumably 2 (if OS X-ls works the same way as my ls on AIX, the value might differ but it should be non-zero in any case).

I hope this helps.

bakunin
# 6  
Old 02-29-2016
Below are the screenshots of my actions in OS X Terminal. As you see there're no alert messages. I have cat'ed both files beforehand so that their contents became evident. If I pass -n option then nothing happens - both are kept in the state they used to prior running the command (as evidenced in the portion of their directory listing I invoked through ls -lahG command).
What's the reason behind having -n option for mv command?-test1png
What's the reason behind having -n option for mv command?-test2_1png
# 7  
Old 02-29-2016
And what is "overwrite2 not overwritten" (which i would guess is printed to stderr)?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command option substitution?

Hi folks, I totally dislike asking questions in forums but this one eats up to much of my time I need to spend on other topics. I have a shell-script in which I call a terminal. I want to invoke bash inside the terminal and print a message inside bash with aid of a here document. See... (7 Replies)
Discussion started by: bluntroller
7 Replies

2. Solaris

Fsck command without any option

Dear all, I want to execute fsck command,can i execute fsck command without any option asking for more confidence. Thanks and Regards Monoj Das (1 Reply)
Discussion started by: monojcool
1 Replies

3. Shell Programming and Scripting

How to use a variable as a command option?

I am just learning shell scripting and already I found out I have the bad habit of thinking that it is similar to php or c. I learned some basics and now encountered this problem: On shell it is possible to type: $ date --date="2009-10-10 09:08:34" Sat Oct 10 09:08:34 CEST 2009 ... (2 Replies)
Discussion started by: quinestor
2 Replies

4. HP-UX

oracle processes with SOCKT in glance command....reason?

Hi Friends, I could see following oracle process in the glance command. i see nothing running in the database although. I tried google it but no success. Another team ,which needs all the processes on the server is complaining. Can someone help me what exactly are these sessions/ how to ... (1 Reply)
Discussion started by: kunwar
1 Replies

5. Shell Programming and Scripting

-n option with grep command

Hi, what is the meaning of -n option before the grep command ? grep command searches for the specified string in the file tmp_crontab.txt but what does -n mean ? With Regards (1 Reply)
Discussion started by: milink
1 Replies

6. HP-UX

who command option not working

Running HP 11.31 on a HP3600. But when I log in as a user the who command works but if I use an option like "who -m" I get nothing. Any thoughts on what is causing this problem. (11 Replies)
Discussion started by: KMRWHUNTER
11 Replies

7. Solaris

Why does the 'ps' command with -u option not working?

How can I use the 'ps' command to view current sessions but only for a given process/user, with the -u parm? In older versions of Unix, this used to work, but not in Sun Solaris. Thanks (4 Replies)
Discussion started by: ElCaito
4 Replies

8. UNIX for Dummies Questions & Answers

option for ls command

i'm using SunOS 5.7 and I know theres a ls option for seeing what kind of files are in a directory. I was wondering if there was a ls option that could see if the files are txt or files that can be opened in vi (1 Reply)
Discussion started by: eloquent99
1 Replies

9. Shell Programming and Scripting

-c option in ping command

What does '-c' mean in ping command? Is this option specific to bash shell? Deepa (3 Replies)
Discussion started by: Deepa
3 Replies
Login or Register to Ask a Question