Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

if one executes the following two commands in one line, as follows,

rm -rf dir ; cp -r dir2 dir

it may complain that cp can not create directory dir/subdir: File exists

but if these two commands are executed in two lines, no errors will be thrown. I am just wandering what is the difference? and more importantly, how to execute two commands in one line, with the effect exactly the same as by two lines...

EDIT: I change it to cp -r dir2 dir. Besides, what is in dir or dir2 is huge, typically 4gb.

share|improve this question
    
There's nothing wrong with the command line you posted. Check to see if either command is aliased or redefined as a function: type -a rm cp. –  Dennis Williamson Jul 20 '12 at 19:20
    
type -a rm cp shows rm is redirected to rm -i, so if cp –  Richard Jul 20 '12 at 19:25
    
On my system, it gives a different error: cp: omitting directory `dir2' even when rm is not aliased. So, there's definitely something going on, but see solution below. –  CodeGnome Jul 20 '12 at 19:27
    
By "redirected" I assume you mean aliased. The -i option shouldn't cause the problem you're seeing. Is dir2 a symlink? That shouldn't matter, though. –  Dennis Williamson Jul 20 '12 at 19:28
    
Is dir on an NFS mount, by any chance? I'm picturing the NFS client issuing the cp request before the NFS server has finished deleting dir. No idea if that's plausible. –  chepner Jul 20 '12 at 19:43
show 1 more comment

migrated from stackoverflow.com Jul 20 '12 at 22:58

This question came from our site for professional and enthusiast programmers.

2 Answers

If you want to execute command2 after command1 (only if command1 completes successfully), then do:

command1 && command2
share|improve this answer
add comment

Solution Without Explanation

I'm not 100% sure why this happens, although I suspect it has something to do with how or when Bash is expanding the arguments to cp. However, it's easy enough to fix: just add the -a flag. For example:

rm -rf dir; cp -a dir2 dir

This performs properly on my system, and reports no errors. As proof:

$ set -x; rm -rf dir; cp -a dir2 dir; ls
+ rm -rf dir
+ cp -a dir2 dir
dir  dir2
share|improve this answer
    
Do you get an error without -a? What happens if you use -d instead? Is dir2 a symlink? –  Dennis Williamson Jul 20 '12 at 19:26
    
@DennisWilliamson Yes. On my system, it gives a different error: cp: omitting directory `dir2' even when rm is hashed to /bin/rm. Ubuntu 11.10, Bash 4.2.10(1)-release. The -d flag gives the same error, as it should, since I'm testing with real directories. The -a flag definitely fixes it, though. –  CodeGnome Jul 20 '12 at 19:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.