Home Man
Search
Today's Posts
Register

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?

Tags
moving file, renaming a file

Login to Reply

 
Thread Tools Search this Thread
# 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 01: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
The Following User Says Thank You to bakunin For This Useful Post:
scrutinizerix (02-28-2016)
# 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?-test1-png  
What's the reason behind having -n option for mv command?-test2_1-png  

# 7  
Old 02-29-2016
And what is "overwrite2 not overwritten" (which i would guess is printed to stderr)?
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Fsck command without any option monojcool Solaris 1 11-25-2012 11:22 PM
How to use a variable as a command option? quinestor Shell Programming and Scripting 2 03-01-2012 06:59 AM
oracle processes with SOCKT in glance command....reason? kunwar HP-UX 1 05-12-2011 07:23 AM
-n option with grep command milink Shell Programming and Scripting 1 09-30-2010 05:01 AM
who command option not working KMRWHUNTER HP-UX 11 12-09-2009 10:59 AM
Why does the 'ps' command with -u option not working? ElCaito Solaris 4 02-26-2006 06:14 PM
option for ls command eloquent99 UNIX for Dummies Questions & Answers 1 02-07-2003 05:27 PM
tar command with compress option... tomapam UNIX for Dummies Questions & Answers 3 10-11-2002 12:56 PM
-c option in ping command Deepa Shell Programming and Scripting 3 05-31-2002 06:05 AM


All times are GMT -4. The time now is 08:49 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password