Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

So i am writing this bash script to do all sorts of things but the last step is to move the contents of four different directories into one spot so i can run a shred command or remove or something like that (not sure what yet). this is what i have so far

for dir in $dir1 $dir2 $dir3 $dir4; do
     mv -vi ./* $CYCL
done

would this loop remove all files and folders from those directories at once? or would i have to change it?

edit: changed code . there was a function in there that i replaced with the original code

share|improve this question
1  
I think mv $dir/* $CYCL, but you do not really need a loop, mv $dir1/* $dir2/* $dir3/* $dir4/* $CYCL and you really should use the full path in scripts, ./* is ambiguous can will yield unpredictable results. I would trust $dir1 and $CYCL are full paths to some location. –  bodhi.zazen Nov 19 '13 at 21:33
1  
You set variable dir but then not use it? –  rickhg12hs Nov 19 '13 at 21:34
2  
Also, if your directories or files have spaces, you will have problems. You need to quote your variables. –  bodhi.zazen Nov 19 '13 at 21:35
    
And if you decide that shred should be used, if your previous mv was from a separate partition, there may still be remnants. –  rickhg12hs Nov 19 '13 at 21:37

2 Answers 2

up vote 0 down vote accepted
  • The concise way:

    mv -vi {$dir1,$dir2,$dir3,$dir4}/* $CYCL
    

    I don't know why you're using the -i option for files you want to delete anyway. You don't care if anything is overwritten, you're just adding more manual steps.

  • Using your loop:

    for dir in $dir1 $dir2 $dir3 $dir4; do
       mv -vi $dir/* $CYCL
    done
    

The for var in list; do ; done syntax will sequentially assign the value of each element in the list to the variable X and then run whatever is in the do block. If you don't mention the variable you used nothing is done to it. For example, this will simply print "hello" 3 times:

for i in Tom Dick Harry; do echo hello; done
hello
hello
hello

If you want to actually do something to the elements of your list, you will need to refer to the variable you assigned them to:

$ for i in Tom Dick Harry; do echo hello $i; done
hello Tom
hello Dick
hello Harry

So, your loop was running mv ./* $CYCL which would have moved the contents of your current directory into whatever is the value of $CYCL. It would not have touched the contents of dirs 1-4 in any way.

share|improve this answer
    
ok. i thought the the loop would cycle through the directories so that the current directory would change each time. Didn't know that i had to use the variable –  user6 Nov 21 '13 at 18:52

Your code doesn't look complete. Please post the whole script. To answer your question, the "mv" command should move everything recursively. Although it would appear to be all at once, the nature of a loop means each command should be executed one after another in order.

share|improve this answer
    
Welcome to Ask Ubuntu! This is really a comment, not an answer. With a bit more rep, you will be able to post comments. –  terdon Nov 20 '13 at 0:27

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.