LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to copy files from a partition to another (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-copy-files-from-a-partition-to-another-849500/)

ldesamero 12-10-2010 10:26 AM

How to copy files from a partition to another
 
Hi All,

Now I have learned creating partition in linux (ubuntu), well that's an achievement for a newbie.

The next thing that I want to know is, how can I copy the contents of a partition to another partition. Like if I want to backup its content to a new partition that Im going to create.


Any ideas please. And Thank you

kirukan 12-10-2010 10:31 AM

If both partitions mount with respective directories just use copy command "cp"

ldesamero 12-10-2010 12:02 PM

Thank you for the reply kirukan, that helped me.

I was just thinking if this scenario is possible:

I have an existing partition sdb1 and I created a new partition sdb2, then copied the contents of sdb1 to sdb2. Then I deleted my sdb1 partition. Now I want my sdb2 to act as my previous sdb1 that I have deleted so that I could mount it again from the previous directory where the old sdb1 is mounted and use all the copied files or maybe also applications from the previous deleted partition; and also renaming sdb2 to sdb1?

Basically the contents of sdb2 will be a total replication of what sdb1 have before, but might have a difference in size. Example: old sdb1 is 10GB, then sdb2 is 8GB. So if you could notice, sdb1 is a copy of sdb2 that is being Shrinked in size


Any ideas from all the members here if this is possible? And if yes how can this be achieved?

For some reason I preferred using native scripts of linux, like fdisk, cp, etc and not
using other apps to achieve this.


THank you very much

darkefge 12-10-2010 01:58 PM

You would probably want to try parted/gparted. Given you are using an extended partition with sdb2 you might have to move the files to another non sdbX then fdisk sdb into one partition and move the files back. Parted does allow for partition resizes though.

http://www.gnu.org/software/parted/m...l#Partitioning

AnanthaP 12-10-2010 08:15 PM

As implied by kirukkan's post, all you need to do is to mount sd2 instead of sd1 into the logical mount point.

So if youe earlier mount points were something like:
sd1 /usr/special
sd2 /usr/special_back

you would probably do:

cp/dd/rsycn sd1 sd2
umount /usr/special
umount /usr/special_back
mount sd2 /usr/special.

Be sure to change the default mount points also by reading `man mount` (how brokebackish).

catkin 12-10-2010 08:51 PM

Quote:

Originally Posted by ldesamero (Post 4187150)
... how can I copy the contents of a partition to another partition.

The dd command for simply copying the contents of one partition to another but dd is seldom the best choice if the partition contains a file system because a) if the old file system is fragmented the fragmentation is also reproduced b) if the new partition is bigger the extra space is unused c) if the new partition is smaller it fails d) if there is an error during copying, the resulting breakage can be extensive.

Is it a file system that you want to copy?

RootAround 01-04-2011 06:05 PM

Quote:

Originally Posted by catkin (Post 4187599)
The dd command for simply copying the contents of one partition to another but dd is seldom the best choice if the partition contains a file system because a) if the old file system is fragmented the fragmentation is also reproduced b) if the new partition is bigger the extra space is unused c) if the new partition is smaller it fails d) if there is an error during copying, the resulting breakage can be extensive.

Is it a file system that you want to copy?

I'm interested to know if there's a better, preferably free, method besides dd to copy a partition. I use dd to backup my partition on the basis of this article.

As for item (b), above, I'm wondering if the resize2fs command addresses this issue.
This link seems to indicate that it can with limitations.

I've tested these 2 commands below with flat files, and backed up, but not recovered, a partition with the 1st. I'm interested in any comments more experienced administrators might have on them.

dd if=/$dirname/$filename | gzip > /ExternalFile/$filename.img.gz
gunzip /ExternalFile/$filename.img.gz -c | dd of=/$dirname/$filename

where $dirname = dev for the entire partition
$filename = hda2 (or sda2, etc.)

silvyus_06 01-04-2011 07:42 PM

well ... i think that it would be a good idea to drag 'n' drop the files .
1.mount the 2 partitions .
2. drag drop the files from sdb1 to sdb2
3.delete sdb1 .
4. use gparted to move /dev/sdb2 and leave the free space of sdb1 to the right .
5 . mount sdb2 (if it still remains under the same name ) with the previous mount point of dev sdb1 . eg: /dev/sdb1 mountpoint is /media/files and /dev/sdb2 mount point is /media/files1 change /dev/sdb2 mount to /media/files

if that's what you want to do ... basically .. i think it's suppose to be sdb1 after you move it on the place of the free space.

catkin 01-04-2011 09:55 PM

If you really do want to copy a partition (which does not have to contain a file system) then dd is the tool but if you want to copy a file system then it is better to use a file copying utility such as rsync. There are other utilities such as cp, cpio and tar; some people have strong preferences.

If copying a file system from a smaller to a larger partition using dd then resize2fs can be used (on ext2, 3 and maybe 4 file systems) but this is not the best method for reasons already given in this thread.

jmc1987 01-05-2011 01:56 AM

sudo cfdisk /dev/yourdevice
sudo mke2fs -j /dev/yourdevice
sudo mount -t auto /dev/yourdevice /your-mount-point
cd to/your/files/you/want/to/copy
cp -R your/files/from/here to/your/files/there
sudo umount /dev/yourdevice


were your device would be would be something like /dev/sda1 /your-mount-point would be the folder you wish to mount the device too such as /mnt. the -R on the cp command allows to to copy all file and folders that are inside a folder.

Please read

man cfdisk
man mke2fs
man mount
man cd
man cp
mount umount

to get a better understanding.

Good luck

catkin 01-05-2011 11:40 AM

If using cp, it's better to use its --archive option.

linuxunix 01-06-2011 01:01 PM

I generally prefer using this.


All times are GMT -5. The time now is 04:13 PM.