Results 1 to 5 of 5

Thread: mv command skips .hidden .files

  1. #1
    Join Date
    May 2008
    Location
    Brooklyn
    Beans
    160
    Distro
    Kubuntu 13.04 Raring Ringtail

    mv command skips .hidden .files

    Greetings.

    I need some help with the mv command or an alternative thereto. It's
    really something I ought to know but I have usually used the mv command
    for a small number of files at a time and, amazingly, never for
    "hidden" files (with names beginning with .), symbolic links and any
    lingering hard links.

    It is my intention to move whole directory trees from one file system
    to another, including hidden files.

    Consider the following directories in /tmp: /tmp/A and /tmp/B, noting
    their contents:
    Code:
    $ ls -lA /tmp/[AB]
    /tmp/A:
    total 8
    -rw-r--r-- 1 rasputin rasputin 74 2010-12-31 00:11 file1
    -rw-r--r-- 1 rasputin rasputin 74 2010-12-31 00:11 .file2
    
    /tmp/B:
    total 0
    That is, subdir A contains two files, one of which would not turn up in
    a simple ls listing because its name starts with a dot.

    Now the mv command (with verbose option), followed by the same ls
    command to view my accomplishment:
    Code:
    $ mv -v /tmp/A/* /tmp/B
    `/tmp/A/file1' -> `/tmp/B/file1'
    
    $ ls -lA /tmp/[AB]
    /tmp/A:
    total 4
    -rw-r--r-- 1 rasputin rasputin 74 2010-12-31 00:11 .file2
    
    /tmp/B:
    total 4
    -rw-r--r-- 1 rasputin rasputin 74 2010-12-31 00:11 file1
    Notice, the mv skipped moving .file2.

    I may have missed something in the mv man page but I don't see an
    option for including such hidden files in the move. I also don't see
    an option in rsync for accomplishing the same function, though it's
    really easy to miss something in the haystack of the
    rsync man page.

    So, my question is (as usual, multi-part. Sighhhh ):
    • Is there indeed an option to mv that forces it to include .files in
      the move?
    • I am assuming that mv will recursively move all subdirectories,
      similar to the -pR option of the cp command. Is this a correct
      assumption?
    • Is there an option to rsync that accomplishes what I'm up to? (I
      just found the --remove-source-files
      option.)
    • Is there yet another program I can use to accomplish this move?

    Note: I do have an awkward workaround: I could use cp -pR to copy the directory tree and then
    rm -rf /source-path/* to empty out
    the original directory. I'd rather not use it because if something went
    was missed in the cp, I would lose the original in the rm -rf step.

    I am also worried about the godzillion options in rsync. I want to
    preserve symbolic links but they may require interpretation when the
    symlink itself is moved to another file system - the target of the
    symlink may or may not remain in the same relative path as it
    originally was. (Y'all may surmise that I have not used rsync much.)
    I will be experimenting with mv and rsync for symlinks and hard links
    but I might get my answers more quickly if someone already has the
    knowledge to share.

    Advice, anyone?

    Thanks much.
    -- Rasputin Paskudniak II (In pursuit of undomesticated, semi-aquatic avians)

  2. #2
    Join Date
    Sep 2006
    Location
    Dublin IE
    Beans
    3,275
    Distro
    Ubuntu

    Re: mv command skips .hidden .files

    Hi there,

    Just a bit of background ... bear with me ...

    There isn't really any such thing as a hidden file. Choosing not to display certain files by default is just a commonly observed convention, for the sake of neatness. Examples include files beginning with '.' (settings/preferences), and those ending with '~' (backups).

    Commands like mv don't care about filenames. They will process any file you tell them to, however the '*' wildcard does not expand to include filenames starting with dots, so they are not processed. mv -v /tmp/A/* /tmp/B does not move ".file2" for the same reason that you'd expect mv -v /tmp/A/g* /tmp/B not to move "file1".

    For example, compare ...
    Code:
    ls -d /tmp/A/*             [.files explicitly excluded]
    ls -d /tmp/A/* /tmp/A/.*   [.files included]
    ls /tmp/A/* /tmp/A/.[^.]*  [".." and "." excluded]
    find /tmp/A -type f        [find does not observe the '.' convention]

    Quote Originally Posted by rpaskudniak View Post
    Is there indeed an option to mv that forces it to include .files in the move?
    No, but such an option is not necessary. Any file selected by your arguments will be moved, whether it starts with a '.' or not.

    Quote Originally Posted by rpaskudniak View Post
    I am assuming that mv will recursively move all subdirectories, similar to the -pR option of the cp command. Is this a correct assumption?
    Strictly speaking, mv doesn't act recursively. Think of moving a directory as detaching it from one part of your filesystem and re-attaching it to another. On most filesystem formats, an entire directory structure can be moved instantly ... it's not a recursive operation. Copying, on the other hand, involves duplicating data, which is quite a different thing.

    Quote Originally Posted by rpaskudniak View Post
    Is there an option to rsync that accomplishes what I'm up to?
    Rsync should not be used to simply move files around. It's designed to help you mirror changes in file structures, typically between two different machines (eg a development & deployment environment).

    Quote Originally Posted by rpaskudniak View Post
    Is there yet another program I can use to accomplish this move?
    There are lots of ways of moving things, but a straightforward 'mv' command is the one least likely to do something unexpected, and the one that will perform most efficiently.

    I hope that helps.

  3. #3
    Join Date
    Sep 2007
    Location
    Key Largo, FL
    Beans
    75
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: mv command skips .hidden .files

    Quote Originally Posted by kidders View Post
    There are lots of ways of moving things, but a straightforward 'mv' command is the one least likely to do something unexpected, and the one that will perform most efficiently.

    I hope that helps.
    So, what is the best way to move EVERYTHING, including the . files? Answering that question would truly "help". I find that I run the mv command twice one with a * and the second with a .*. In fact, I usually use my arrow to bring up the previous command and add the ".".
    Pete "NetDoc" Murray
    NetDoc@ScubaBoard.com
    www.ScubaBoard.com
    The World's LARGEST Dive Community

  4. #4
    Join Date
    Jul 2011
    Beans
    1

    Re: mv command skips .hidden .files

    Quote Originally Posted by NetDoc View Post
    So, what is the best way to move EVERYTHING, including the . files? Answering that question would truly "help". I find that I run the mv command twice one with a * and the second with a .*. In fact, I usually use my arrow to bring up the previous command and add the ".".
    By `best' do you mean simpliest, fastest, most portable, etc.?


    A simple way:

    Code:
    mv src/{*,.*} dest/
    Remember, the `mv' command can move multiple sources to the destination.



    A simple bash specific way:

    Code:
    shopt -s dotglob
    mv src/* dest/
    The `shopt -s dotglob' makes `*' include dotfiles. To turn it off use `shopt -u dotglob'.
    Last edited by cmthornton; July 9th, 2011 at 04:57 AM.

  5. #5
    Join Date
    Sep 2007
    Location
    Key Largo, FL
    Beans
    75
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: mv command skips .hidden .files

    Quote Originally Posted by cmthornton View Post
    By `best' do you mean simpliest, fastest, most portable, etc.?


    A simple way:

    Code:
    mv src/{*,.*} dest/
    Remember, the `mv' command can move multiple sources to the destination.
    That works great. Thanks!

    Quote Originally Posted by cmthornton View Post
    A simple bash specific way:

    Code:
    shopt -s dotglob
    mv src/* dest/
    The `shopt -s dotglob' makes `*' include dotfiles. To turn it off use `shopt -u dotglob'.
    Explain a bit more about shopt please.
    Pete "NetDoc" Murray
    NetDoc@ScubaBoard.com
    www.ScubaBoard.com
    The World's LARGEST Dive Community

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •