[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

General method for copying a partition



On Wed, 17 Feb 2010 11:26:43 -0600, Boyd Stephen Smith Jr. wrote:
> On Wednesday 17 February 2010 11:18:38 Stephen Powell wrote:
>> Then the files
>> are copied between partitions by means of
>> 
>>    cp -a /media/* /mnt
>> 
>> This seems to work OK except if there are "dot files" (files with names
>> beginning with a period) in the top directory of the source partition.
> 
> Use this instead:
> cp -a /media/. /mnt
> OR
> cp -a /media/{.[!.],}* /mnt

I replied to this post earlier, but this is a follow-up because I have
discovered a flaw in the logic of the second method.  I earlier pointed
out that the second method doesn't work in ash, a Bourne shell clone
used by the Debian installer,
because ash doesn't support brace expansion.  But that is easily solved
by separating them, as you pointed out.  For example,

   cp -a /media/.[!.]* /media/* /mnt

There is of course the problem of "What happens if the pattern matches 0 files"?
But there is also another problem.  A file such as "..test" will not be copied.
The first pattern requires that the second character must exist and must
not be a period.  This eliminates "." and "..", which is desired, but it
also eliminates any file which starts with "..", such as "..test".  I was
trying to apply this technique to the problem of removing everything from a
directory, including all subdirectories, but not removing the directory itself.
The shell is assumed to be one that does not support the GLOBIGNORE shell
variable or brace expansion, such as ash.  What I eventually came up with was

   rm -r /x/y/* /x/y/.[!.]* /x/y/..?*

This does not solve the problem of patterns which match zero files,
which can be solved in bash by "shopt -s nullglob".  There doesn't appear
to be any equivalent for "shopt -s nullglob" in ash.  But in this context,
it doesn't matter.  An error message will be issued for a "file not found"
condition if one of the patterns does not match any files, but all files
in the directory will be erased.  I suppose something like

   if [ -f /x/y/* ];then rm -r /x/y/*;fi
   if [ -f /x/y/.[!.]* ];then rm -r /x/y/.[!.]*;fi
   if [ -f /x/y/..?* ];then rm -r /x/y/..?*;fi

would have to be used if one wanted to clean out the directory without
removing it and not produce an error message.  Or maybe

   rm -r /x/y/* /x/y/.* >/dev/null 2>/dev/null

would be simpler.  (This will produce error message about "you can't
remove . or ..", but they will be redirected to /dev/null.)  Anyway, the point
is that the pattern .[!.]* will not match all dot files except "." and "..".


Reply to: