How to move from one filesystem to another without changing directory structure?

6,754

Solution 1

Do not use cp to copy large amounts of data.
Use rsync as well, as it can be restarted if it is interrupted. Use the following:

rsync -az -H /path/to/source  /path/to/destination

-a : Archive mode (i.e. recurse into directories, and preserve symlinks, file permissions, file modification times, file group, file owner, device files & special files) <br>
-z : Compress file data during the transfer <br>
-H : Preserve hard links (i.e. copy hard links as hard links)

Edited as pointed by the commenters, I did not see this is your / dir.
If using bash or zsh as shell add:

--exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"}

Now it becomes

rsync -az -H --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /path/to/source  /path/to/destination 

Otherwise if using different shell, you should do a --exclude statement for every different dir.

Solution 2

In most cases, you wouldn't move the files from one filesystem to another. If you're enlarging the disk in a virtual machine or grabbing more space from the same disk, you'd enlarge the partition containing the filesystem then enlarge the filesystem to fill the partition (which commands to use depends on the partition type and filesystem type). If you're moving to a new disk, it's usually faster and easier to copy the partition as a whole and then enlarge it as above.

In your case, since this is a virtual machine, you'll need to first give it a bigger disk. As noted by Mark Plotnick in a comment, check the AWS guide; there's a web interface to attach a new volume, and there's also a web interface to perform the copy (by creating and the restoring a snapshot).

Note that /dev is not a storage filesystem, it's a filesystem in RAM. Its 30GB free are the maximum amount of data that can be stored there, but that takes a chunk from your RAM, and is lost when you reboot. Once again, do not move any files to /dev, they would be deleted when you reboot.

Share:
6,754

Related videos on Youtube

rafaelcosman
Author by

rafaelcosman

Stanford CS grad interested in ML.

Updated on September 18, 2022

Comments

  • rafaelcosman
    rafaelcosman over 1 year

    /dev/xvda1 is full so I want to move its contents onto my 30GB udev filesystem.

    Filesystem      Size  Used Avail Use% Mounted on
    udev             30G  4.0K   30G   1% /dev
    tmpfs           5.9G  768K  5.9G   1% /run
    /dev/xvda1      7.8G  7.4G     0 100% /
    none            4.0K     0  4.0K   0% /sys/fs/cgroup
    none            5.0M     0  5.0M   0% /run/lock
    none             30G     0   30G   0% /run/shm
    none            100M     0  100M   0% /run/user
    

    How can I do that?

    Stack: EC2 Ubuntu machine.

    • dhag
      dhag almost 8 years
      The /dev filesystem, of type udev, is not backed by permanent storage. You almost certainly do not want to move / there.
    • Mark Plotnick
      Mark Plotnick almost 8 years
      Is this a local Xen system or are you using a cloud provider?
    • rafaelcosman
      rafaelcosman almost 8 years
      Thanks @MarkPlotnick, I've edited my post to point out that it's on an EC2 machine.
    • Mark Plotnick
      Mark Plotnick almost 8 years
      You need a larger permanent disk volume. docs.aws.amazon.com/AWSEC2/latest/UserGuide/… . Create a new, larger volume based on a snapshot of this volume, detach the old volume, attach the new volume, use resize2fs.
  • roaima
    roaima almost 8 years
    No point using -z for a local copy.
  • Jeff Schaller
    Jeff Schaller almost 8 years
    I hope you're not (indirectly) suggesting that they rsync files from / to /dev ? See dhag's comment above
  • Hristo Mohamed
    Hristo Mohamed almost 8 years
    I did not see this is his root, I have editted my post
  • rafaelcosman
    rafaelcosman almost 8 years
    Thanks for the quick reply. At the end of the day, I want my directory structure to be unchanged. As in for each file that was formerly at /, I want it to still be at /. Will this get me that?
  • Hristo Mohamed
    Hristo Mohamed almost 8 years
    The contents of /dev, /proc, /sys, /tmp and /run were excluded because they are populated at boot (while the folders themselves are not created), /lost+found is filesystem-specific. If there are files you want to keep at /mnt or /media you can remove them from the list.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 8 years
    There's nothing wrong with using cp -ax to copy a whole filesystem. With GNU coreutils, that's the command that's most likely to preserve all “special cases” (permissions, extended attributes, exotic file types, etc.).
  • Hristo Mohamed
    Hristo Mohamed almost 8 years
    Cp does not allow you to restart the copy if something goes wrong. Plus in general not everyone has access to the GNU version!