Wednesday 1 January 2014

Linux COPY



Linux Copy:

When we mention ‘copy’ command, the command ‘cp’ comes to mind. But cp is just to copy the file content to another file. There are two other important commands called ‘cpio’ and ‘dd’. In this blog, we are going to talk about the usage and difference.

#cp: copy the file from one location to another

cp [options] [source] [destination]
some useful options”

  • -f: forced without prompt
  • -i: interactively
  • -s: create the softlink insteaf of copying.
  • -p: preserve the permission and timestamps
  • -r: copy recursively

Example. Copy all the files from /var/tmp/a to /tmp/b and reserve the timestamps and permissions
#cp –rp /var/tmp/test /tmp/newtest











We need to care about the destination. It can be either a directory or a file.  Once we want to daily copy the log file to a directory using the command  
#cp –p /var/log/apache/access.[date].log  /logpool/apachelog
The directory apachelog is not existing so it copies the file to /logpool and overwrites as apachelog everyday! And the cp command returns well every time.
We should

  • 1.       Check if /logpool/apachelog is existing
  • 2.       Using  #cp –p /var/log/apache/access.[date].log  /logpool/apachelog/ instead.


dd command.

dd is the command to do the block copy in linux.
Basic command is like:
dd if=[file] of=[destination] [options]
the dd command can copy the regular files as below
dd if=/var/tmp/test.txt of=/tmp/mytest.txt











but it is more used in device copy than the file copy.

Most useful options are:

[tbc]
Some useful scenarios:
Backup the MBR
#dd if=/dev/sda1 of=/usr/local/testdd/mbr count=1 bs=512      //sda is the boot device.








Hard disk backup
#dd if=/dev/hdb of=/root/image             //backup the file to a image file







[root@NOSQL ~]# ######## test the disk read and write speed
[root@NOSQL ~]# dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 6.52976 s, 157 MB/s
[root@NOSQL ~]# dd if=/root/1Gb.file bs=64k of=/dev/null
15625+0 records in
15625+0 records out
1024000000 bytes (1.0 GB) copied, 3.45494 s, 296 MB/s


cpio:

cpio is another useful command to backup and restore the file.
#cpio –i : (copy in) cpio -i extracts files from the standard input.
#cpio-o: cpio -o reads the standard input to obtain a list of path names and copies those files onto the standard output.


#example:
find . -print | cpio -ocv > /dev/fd0
Above, using the find command would list all files and directories and using the cpio command copy those files listed to the floppy drive.
find . -print | cpio -dumpv /home/users/hope
In the above example the find command would find all files and directories and using the cpio command copy those files to the hope user account.
cpio -icuvd < /dev/fd0
The above command would restore the files back from the floppy.
 

No comments:

Post a Comment