I don't think that your assertion above that the second command will fail if the first fails is true. If mv dir1 dir1_backup
fails, then dir1 will still exist, and mv dir2 dir1
will make dir2 a subdirectory of dir1. Also, since you are passing -f
to rm
in the third command, it will silently fail to delete a non-existant directory.
For reference, three bash operators (assuming bash here) and what they do:
- ; simply executes the following command after the first exits, regardless of the exit status.
- && executes the following command only if the previous command exited cleanly (ie, had an exit status of 0.)
- || executes the following command only if the previous command exited uncleanly (ie, had a non-zero exit code.)
If you want each command's execution to be dependent upon the previous command's successful execution, then you might want to consider something like the following:
mv dir1 dir1_backup && mv dir2 dir1 && rm -rf dir1_backup
If the moving of a directory is a failure mode you expect, then it may be reasonable to test the return value of the move or to test for the existence of the expected directory before proceeding to remove the old directory contents. If the test fails, then move the old directory contents back. The overall operation will have failed, but at least you will not be in an invalid state. For example:
mv dir1 dir1_backup && \
mv dir2 dir1 && \
rm -rf dir1_backup || mv dir1_backup dir1
Since we know that &&
executes the following command only if the previous command exits successfully, mv dir2 dir1
will only execute if mv dir1 dir1_backup
succeeds.
Further, rm -rf dir1_backup
will only execute if the move of dir2 to dir1 succeeds. However, if the last exit code (the code returned by mv dir2 dir1
if it has failed) is non-zero, the ||
operator causes mv dir1_backup dir1
to execute.
Hope that helps! Let me know if it needs clarification.
&&
instead of;
to separate your cmds? Unfortunately, while&&
should work, I'm not sure I'd ever want to "bet the store" like that. I would build something that does validations, especially before the finalrm
step. Good luck. – Smv -T point_to_dir2 point_to_dir1
wouldn't work in your edge case. – Thermionic