Unix/Linux Go Back    


OS X (Apple) OS X is a line of Unix-based graphical operating systems developed, marketed, and sold by Apple.

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

OS X (Apple)


Tags
moving file, renaming a file

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-28-2016   -   Original Discussion by scrutinizerix
scrutinizerix's Unix or Linux Image
scrutinizerix scrutinizerix is offline
Registered User
 
Join Date: Sep 2015
Last Activity: 17 March 2018, 12:38 PM EDT
Posts: 26
Thanks: 7
Thanked 1 Time in 1 Post
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 01:03 PM..
Sponsored Links
    #2  
Old Unix and Linux 02-28-2016   -   Original Discussion by scrutinizerix
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 June 2018, 8:24 AM EDT
Location: Aachen, Germany
Posts: 12,917
Thanks: 429
Thanked 3,967 Times in 3,648 Posts
Did you think of moving to a different directory?
Sponsored Links
    #3  
Old Unix and Linux 02-28-2016   -   Original Discussion by scrutinizerix
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 22 June 2018, 7:13 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,780
Thanks: 112
Thanked 1,698 Times in 1,248 Posts
Quote:
Originally Posted by scrutinizerix View Post
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
The Following User Says Thank You to bakunin For This Useful Post:
scrutinizerix (02-28-2016)
    #4  
Old Unix and Linux 02-28-2016   -   Original Discussion by scrutinizerix
scrutinizerix's Unix or Linux Image
scrutinizerix scrutinizerix is offline
Registered User
 
Join Date: Sep 2015
Last Activity: 17 March 2018, 12:38 PM EDT
Posts: 26
Thanks: 7
Thanked 1 Time in 1 Post
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?
Sponsored Links
    #5  
Old Unix and Linux 02-28-2016   -   Original Discussion by scrutinizerix
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 22 June 2018, 7:13 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,780
Thanks: 112
Thanked 1,698 Times in 1,248 Posts
Quote:
Originally Posted by scrutinizerix View Post


Code:
mv -n A B

, then nothing happens.
Not quite, see below.

Quote:
Originally Posted by scrutinizerix View Post
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
Sponsored Links
    #6  
Old Unix and Linux 02-29-2016   -   Original Discussion by scrutinizerix
scrutinizerix's Unix or Linux Image
scrutinizerix scrutinizerix is offline
Registered User
 
Join Date: Sep 2015
Last Activity: 17 March 2018, 12:38 PM EDT
Posts: 26
Thanks: 7
Thanked 1 Time in 1 Post
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).
Attached Thumbnails
What's the reason behind having -n option for mv command?-test1-png   What's the reason behind having -n option for mv command?-test2_1-png  
Sponsored Links
    #7  
Old Unix and Linux 02-29-2016   -   Original Discussion by scrutinizerix
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 June 2018, 8:24 AM EDT
Location: Aachen, Germany
Posts: 12,917
Thanks: 429
Thanked 3,967 Times in 3,648 Posts
And what is "overwrite2 not overwritten" (which i would guess is printed to stderr)?
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
oracle processes with SOCKT in glance command....reason? kunwar HP-UX 1 05-12-2011 07:23 AM
who command option not working KMRWHUNTER HP-UX 11 12-09-2009 10:59 AM
option for ls command eloquent99 UNIX for Dummies Questions & Answers 1 02-07-2003 05:27 PM



All times are GMT -4. The time now is 10:53 AM.