1

I need to zip some files, move zipped files to some other directory using mv command and then remove all files from original location. But in my case rm command is executing before mv command and failing mv command.

I tried below approaches but it didn't help.

Approach 1 :

$ zip -r foo.zip * &;  wait %1 && mv foo.zip mydir &; wait %1 && rm -rf *

Approach 2 :

[ zip -r foo.zip *] || {mv foo.zip mydir && rm -rf *}
  • 1
    You want to "move zipped files to some other directory using mv command and then remove all files from original location". Why? mv copies and then removes from the source location. You don't need rm anymore. PS : please have a look at the difference between ; and && – schaiba Jan 11 '17 at 18:54
  • @schaiba He's removing the files he zipped. The "zipped files" he's moving are the ones in the archive, I believe. – Kusalananda Jan 11 '17 at 18:59
  • 3
    Why are you running zip and mv in the background??? – Gilles Jan 11 '17 at 23:33
  • @Schaiba : It's correct. This is what i mean , "move zipped files to some other directory using mv command and then remove all files from original location". But somehow rm -rf *; getting executed before mv command. Checking exit status with &&, did not help here. – Vijay Shinde Jan 15 '17 at 11:59
  • The exit status check is done with echo $?. – schaiba Jan 15 '17 at 12:39
5

You're complicating it.

If zip exits with an exit status that reflects success or failure, you can simply do

$ zip -r foo.zip . && mv foo.zip mydir && rm -rf *

However, I really hope mydir is not located in the current directory, or it would get deleted by the rm -rf *. In general when using rm -rf, I'd think both two and three times before I put a * after it!! In fact, I can count the number of times I've done that (rm -rf *) in the last year on the fingers of one hand. I would never put it in a script.

Safer way (assuming mydir is actually a path to somewhere else than in the current directory):

$ zip -r --move --test foo.zip . && mv foo.zip mydir

Or even

$ zip -r --move --test mydir/foo.zip .

Note the use of . (dot) rather than *. With * any file with a - as the first character in its name would be interpreted as a command line switch to zip. Test this by creating a file called --encrypt, for example:

$ touch -- --encrypt
$ zip -r --move --test ../test.zip *
Enter password:

$ rm -- --encrypt
  • 2
    You can aggregate the zip and the rm into zip -r --move --test foo.zip * – xhienne Jan 11 '17 at 19:11
  • @xhienne : mydir is on another file system. I need to clean current directory with rm -rf *; after moving zip to mydir. Not able to see any zip files transferred on my dir as rm is deleting zipped file before mv complete. – Vijay Shinde Jan 15 '17 at 12:21
  • @VijayShinde I don't understand your comment. – Kusalananda Jan 15 '17 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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