Here's a little command-line puzzle. Suppose you want to move all files from directory A to directory B. You might think that mv A/* B would do it. But no -- that doesn't move the dotfiles. So how about mv A/* A/.* B No -- that tries to move "." and "..". So how about mv A/* A/.?* B That still tries to move "..", but not ".". Finally, how about mv A/* A/.[^.]* B That actually does it, I think -- but it's very inelegant. Does anyone have an elegant solution (that works for other commands also)? Paul
Paul, On Thursday 10 March 2005 15:31, Paul W. Abrahams wrote:
...
... Finally, how about
mv A/* A/.[^.]* B
That actually does it, I think -- but it's very inelegant. Does anyone have an elegant solution (that works for other commands also)?
If it's something you do frequently, create a script (call it "mvall" or "reloc" or some such) and hide the ugliness there.
Paul
Randall Schulz
On Friday 11 March 2005 00:31, Paul W. Abrahams wrote:
Here's a little command-line puzzle. Suppose you want to move all files from directory A to directory B. You might think that
mv A/* B
would do it. But no -- that doesn't move the dotfiles. So how about
mv A/* A/.* B
No -- that tries to move "." and "..". So how about mv A/* A/.?* B
That still tries to move "..", but not ".". Finally, how about
mv A/* A/.[^.]* B
That actually does it, I think -- but it's very inelegant. Does anyone have an elegant solution (that works for other commands also)?
shopt -s dotglob mv A/* B/ Have a look at "man bash" for a description of shopt, dotglob and the other possibilities available
On Thursday 10 March 2005 6:45 pm, Anders Johansson wrote:
On Friday 11 March 2005 00:31, Paul W. Abrahams wrote:
Here's a little command-line puzzle. Suppose you want to move all files from directory A to directory B.
shopt -s dotglob mv A/* B/
Have a look at "man bash" for a description of shopt, dotglob and the other possibilities available
Very nice, Anders -- but to really do it right we might want: (shopt -s dotglob; mv A/* B/) which does the deed in a subshell and avoids leaving any footprints. Of course, if we want the dotglob behavior in general your original solution is optimal. Another bash goodie I didn't know was there. Paul
On Friday 11 March 2005 00:31, Paul W. Abrahams wrote:
Here's a little command-line puzzle. Suppose you want to move all files from directory A to directory B. You might think that
mv A/* B
would do it. But no -- that doesn't move the dotfiles. So how about
mv A/* A/.* B
No -- that tries to move "." and "..". So how about mv A/* A/.?* B
That still tries to move "..", but not ".". Finally, how about
mv A/* A/.[^.]* B
That actually does it, I think -- but it's very inelegant. Does anyone have an elegant solution (that works for other commands also)?
A/{,.[^.]}* "expands" to A/* A/.[^.]* so mv A/{,.[^.]}* B is also a solution. However mv will bark if there is no dot-file. find A -mindepth 1 -maxdepth 1 -exec mv {} B \; moves dot-{fil,directori}es as well, and does not bark if it can't find a file. Cheers, Leen
On Thu, 2005-03-10 at 18:31, Paul W. Abrahams wrote:
Here's a little command-line puzzle. Suppose you want to move all files from directory A to directory B. You might think that
mv A/* B
would do it. But no -- that doesn't move the dotfiles. So how about
mv A/* A/.* B
No -- that tries to move "." and "..". So how about mv A/* A/.?* B
That still tries to move "..", but not ".". Finally, how about
mv A/* A/.[^.]* B
That actually does it, I think -- but it's very inelegant. Does anyone have an elegant solution (that works for other commands also)?
Paul
If A and B are on the same filesystem simply: mv A B; mkdir A would rename A to B and recreate an empty A If on different mounted filesystems: (cd [source dir parent];tar cf - ./source)|(cd [dest dir parent];tar xvf -) source and dest without the brackets of course. And if on different machines just create the tar file, copy it to the remote PC and untar the contents. -- Ken Schneider UNIX since 1989, linux since 1994, SuSE since 1998 * Only reply to the list please* "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners." -Ernst Jan Plugge
On Thursday 10 March 2005 6:59 pm, Ken Schneider wrote:
On Thu, 2005-03-10 at 18:31, Paul W. Abrahams wrote:
Here's a little command-line puzzle. Suppose you want to move all files from directory A to directory B.
If A and B are on the same filesystem simply:
mv A B; mkdir A
would rename A to B and recreate an empty A
The trouble with that is that you're assuming B is initially empty. What if it isn't? And how would this generalize to other commands (chmod, for instance)? Paul
On Thu, 2005-03-10 at 19:44, Paul W. Abrahams wrote:
On Thursday 10 March 2005 6:59 pm, Ken Schneider wrote:
On Thu, 2005-03-10 at 18:31, Paul W. Abrahams wrote:
Here's a little command-line puzzle. Suppose you want to move all files from directory A to directory B.
If A and B are on the same filesystem simply:
mv A B; mkdir A
would rename A to B and recreate an empty A
The trouble with that is that you're assuming B is initially empty. What if it isn't? And how would this generalize to other commands (chmod, for instance)?
Yes that was a faulty assumption on my part. -- Ken Schneider UNIX since 1989, linux since 1994, SuSE since 1998 * Only reply to the list please* "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners." -Ernst Jan Plugge
participants (5)
-
Anders Johansson
-
Ken Schneider
-
Leendert Meyer
-
Paul W. Abrahams
-
Randall R Schulz