Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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
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
You set variable dir but then not use it? – rickhg12hs Nov 19 '13 at 21:34
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
up vote 1 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

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

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 Answer


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.