Softpanorama
May the source be with you, but remember the KISS principle ;-)

Contents Bulletin Scripting in shell and Perl Network troubleshooting History Humor

cp command

News Recommended Books Recommended Links Reference mv command

 ln command

Renaming files with special characters
tar cpio   Unix History Admin Horror Stories

Humor

Etc

The cp command in Unix can copy one or several files. For each file it reads the contents of a file and creates a new file or overwrites an existing file. If can also recursively copy subtrees of the Unix directory tree. There are two basic formats of the cp command that allow you to:

Following is the general format of the cp command.

cp    [ -ip ]  source_file destination_file
cp    [ -ipr ] source_file_list destination_directory
cp -r [ -ip ]  source_directory destination_directory

The following list describes typical three options that may be used to control how cp functions.

The following list describes the arguments that may be passed to the cp command.

source_file The existing file that will be copied
source_file_list The name of files and/or directories to be copied to a new destination directory
destination_file The name of the file the new copy will be named
destination_directory The name of the directory where a copy of the files will be made
source_directory The name of the directory from where copies are read

The first format copies one ordinary file to a new file. The source_file is the pathname of the file being copied. The destination_file is the pathname of the file being copied to by cp.

The second format will copy one or more files to a specific directory. The source_file_list is a list of pathnames of the files being copied. If you specify a directory with the -r option, the entire directory tree is copied to the destination directory.

The third format allows you to copy an entire directory structure to a new directory.

If the destination_file exists and you have write permissions, cp will overwrite the contents of the file. The access permissions and ownership of the overwritten file will be used for the new destination_file.

If the destination_file does not exist, the access permissions of the source_file are used. Your ownership and group IDs are used for the new file if you are the user performing the cp command.

You cannot copy a file to itself.

Some common formats are:

   cp letter letter.bak    # make a backup copy of letter
   cp letter ../tmp        # copy letter to temporary working directory
   cp letter ../LTRS       # copy letter to original location (mv is better)
   cp /dev/tty note        # copy input from keyboard to file "note"

Top updates

Bulletin Latest Past week Past month
Google Search


NEWS CONTENTS

Old News ;-)

BigAdmin Submitted Article How to Safely Move and Copy Files and Directories by Darek Licznerski. This article discusses the problems that can occur when you move and copy files and directories, and the author also offers solutions.

Contents:
Introduction

You could say, "This is not a problem for me because I can copy files with the cp  or cp -r  commands or move files with the mv  command." However, sometimes you can break something and lose data.

I recommend you avoid such solutions as the cp  or mv  commands when you need to copy or move big files, large numbers of files, or large amounts of data. Also, avoid using commands such as cp  or mv  when you need to keep the same owner, group, permissions, and all the other information about the data.

Caveats

NFS

You can see problems when the destination or source directory is mounted on a local machine from a remote machine via NFS or through the network with any other protocol. To avoid such problems check the source and destination directories via commands. For example:

# df -k <directory> 
where <directory> - source or destination directory

Or use this for the Solaris 9 Operating System (OS) and later versions:

# df -h <directory> 
where <directory> - source or destination directory

Example:

# df -k  /source_directory
Filesystem      kbytes    used     avail      capacity    Mounted on
127.0.0.1:/source
                34750583  28174655 6228423    82%     /source_directory

Here the directory /source  from the host with the IP address 127.0.0.1  (in this case localhost) is mounted on a local machine under the directory /source_directory. To check what type of service is used to mount the directory, run a command such as the following:

# mount -v | grep "<directory>"  where <directory> - source or destination directory

Symbolic Links

You can see problems when the destination or source directory -- or even worse, the subdirectories -- are linked in the source directory.

When you run the cp -r  command on the directory that contains symbolic links, the links in the destination directory will be changed to the real data to which those links pointed. If you use the cp -r  command directory to copy about 1 Gbyte of data that contains large quantities of links, the destination will be 1 Gbyte plus all the data to which those links pointed. Sometimes the result could be huge and could overload your system.

To check if the source directory contains symbolic links, do the following:

# cd <directory>
where <directory> - source or destination directory

# find . -type link | more

To count the number of links, use this:

# find . -type link | wc

Here is the result of copying directory example01  to the new one, example02, using the command cp -r ./example01 ./example02:

# find ./example01 -type link | wc
    2440    2440  171743

# du -ks ./example01
2772092 ./example01

# find ./example02 -type link | wc
       0       0       0 

# du -ks ./example02
6326172 ./example02

Unknown Destination

Problems can surface when you are not familiar with the real location, destination, or source directory because they may point somewhere else.

For example:

# df -k /source_directory
Filesystem         kbytes    used    avail     capacity    Mounted on
/dev/dsk/c0t0d0s0  34750583  28174655 6228423  82%   /source_directory   

# df -k /destination_directory
Filesystem    kbytes    used    avail     capacity    Mounted on
127.0.0.1:/destination
              34750583  28174655  6228423   82% /destination_directory

Here /source_directory  is the directory created on a local disk (on the root partition). /destination_directory  is the directory /destination  on the host with the IP address 127.0.0.1  (in this case localhost), mounted on a local machine. For the purpose of this example, the IP address is localhost. However, in a real scenario, it could be an IP from a different subnet, and the packets (data) would be transferred through the network and who knows where.

When the source or destination directory is mounted through the network, you need to know the following:

The best way is to copy some example data to the destination and get a measurement.

Problems With Access

If you are unfamiliar with how to access a destination or source directory, you may be unable to access the directories or subdirectories.

When you decide to move data using the mv  command, make sure that no processes are running in the source directory that will be moved.

Sometimes you can't access the mounted directory because you have the wrong permissions on the remote host for the directory you are trying to mount. Make sure that the destination directory is not mounted as a read-only file system. For example:

# df -k /destination_directory
Filesystem kbytes     used     avail    capacity    Mounted on
127.0.0.1:/destination
          34750583   28174655   6228423  82%     /destination_directory 

# mount -v | grep "<directory>"

where <directory> - destination directory

Also, check that no read-only flags are set up. The best way is to try to write an example file to the destination before copying or moving large amounts of data.

Interruptions During the Copying or Moving Process

Various problems are possible, for example, if you are using the mv  command from the source to the destination directory and one of them is mounted through the network. If the network becomes unavailable while the files are being moved, or you kill the terminal or kill the data moving process by mistake, data will be moved partially and you also can lose data.

A problem also occurs if you run the cp  command from the source to the destination directory, with one of them mounted through the network, and the network becomes unavailable while files are being copied. In this case data will be moved partially.

When you do such moving of data remotely and, for example, you export DISPLAY to your machine, there is a high probability that the process could be interrupted. So make sure that the place from which you execute such commands is stable.

To check the terminal name, do the following:

# tty
/dev/console

# tty
/dev/pts/1

If you are logged in to the console you also have control during the reboot. Consider using the nohup  command to avoid interruptions caused by long-running processes.

Moving Between Different Devices

It takes much longer to move files and directories via the mv  command among directories that are not created on the same partition or disks than it does to move directories sharing the same partition. (On the same partition, this mv  command works at once, but between different devices you can be surprised by how long it takes.) The time required mostly depends on having access to such a partition or device. Make sure that during the moving of files the machine will not be rebooted and your moving process will not be interrupted.

For example:

# df -k /source_directory
Filesystem         kbytes    used      avail    capacity  Mounted on
/dev/dsk/c0t0d0s0  34750583  28174655  6228423  82%  /source_directory   

# df -k /destination_directory
Filesystem        kbytes    used     avail   capacity  Mounted on
/dev/md/dsk/d30   34750583  28174655 6228423 82% /destination_directory

In the previous case the /source_directory  is the directory created on the local disk (root partition) and the /destination_directory  is the directory created on the metadevice. So the time would not be the same as it would be on the same partition.

Make sure that the destination and source directory have the same block structure. If not, avoid using cp  or mv  commands. To check this you can create the same text file on each device and run the du -ks  command on each file.

For example:

# du -ks ./example.txt
4       ./example.txt

# du -ks /tmp/example.txt
8       /tmp/example.txt

But the diff  or sdiff -s  commands show no differences.

Owner, Groups, Permissions

Make sure that owner, group, permissions, and any other file information are preserved. Some users or services might lose access to such data in the new location, and you would not be aware of that. Sometimes the service could be a critical part of the system.

Summary and Some Issues

Instead of using mv  or cp  commands to move or copy large amounts of data, consider the following procedure:

  1. Make sure that none of the caveats mentioned previously would apply.
  2. Verify disk spaces.

    For example:

    # df -k <directory> 
    where <directory> - source directory

    # df -k <directory> 
    where <directory> - destination directory

  3. Copy data to the destination using commands such as cpio  (recommended), tar, rsync, ufsdump, or ufsrestore.

    Example:

    Let the source directory be /source, and let the destination directory be /destination.

    # cd  /source
    # cd ..
    # find  ./source  -depth -print | cpio -cvo> /destination/source_data.cpio
    # cd /destination
    # cpio -icvmdI ./source_data.cpio
    # rm -rf ./source_data.cpio
    		

    This -c  option is important in case the data is copied among different types of machines. This option provides read or write header information in ASCII character form for portability. There are no UID or GID restrictions associated with this header format. Use this option between SVR4-based machines, or use the -H odc  option between unknown machines. The -c  option implies the use of expanded device numbers, which are only supported on SVR4-based systems. Use the -H odc  option when you are transferring files between the SunOS 4 or Interactive UNIX and the Solaris 2.6 OS or compatible versions.

    The procedure above first creates a file in the /destination  directory that contains all the data packed via the cpio  command and then unpacks this file in the destination.

    For more information, see the manual pages for the cpio  command and other commands if needed.

    Also refer to:


     
  4. Compare data in the source directory and in the destination directory via commands such as sdiff -s, diff, du -ks, find, cmp, and dircmp.

    Example:

    Let the source directory be /source, and let the destination directory be /destination.

    # cd /source
    # cd ..
    # dircmp -s ./source ./destination
    		
  5. If you intend to move data instead of copying, and you are completely sure that all data in the new location is correct, just remove all data in the source location or back it up somewhere else.

[Jul 26, 2006] BigAdmin Submitted Article How to Safely Move and Copy Files and Directories

Abstract: This article discusses the problems that can occur when you move and copy files and directories, and the author also offers solutions.

 

Contents:
Introduction

You could say, "This is not a problem for me because I can copy files with the cp  or cp -r  commands or move files with the mv  command." However, sometimes you can break something and lose data.

I recommend you avoid such solutions as the cp  or mv  commands when you need to copy or move big files, large numbers of files, or large amounts of data. Also, avoid using commands such as cp  or mv  when you need to keep the same owner, group, permissions, and all the other information about the data.

Recommended Links

Softpanorama Top Visited

Softpanorama Recommended

Linux Command Directory cp

cp - Linux Command - Unix Command

Linux Guide-Getting Help - Wikibooks, collection of open-content textbooks

Reference

cp(1) copy files-directories - Linux man page> cp - copy files and directories

cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.

-a, --archive
same as -dpR
--backup[=CONTROL]
make a backup of each existing destination file
-b
like --backup but does not accept an argument
--copy-contents
copy contents of special files when recursive
-d
same as --no-dereference --preserve=link
-f, --force
if an existing destination file cannot be opened, remove it and try again
-i, --interactive
prompt before overwrite
-H
follow command-line symbolic links
-l, --link
link files instead of copying
-L, --dereference
always follow symbolic links
-P, --no-dereference
never follow symbolic links
-p
same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps) and security contexts, if possible additional attributes: links, all
--no-preserve=ATTR_LIST
don't preserve the specified attributes
--parents
use full source file name under DIRECTORY
-R, -r, --recursive
copy directories recursively
--remove-destination
remove each existing destination file before attempting to open it (contrast with --force)
--sparse=WHEN
control creation of sparse files
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-s, --symbolic-link
make symbolic links instead of copying
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
copy all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose
explain what is being done
-x, --one-file-system
stay on this file system
--help
display this help and exit
-Z, --context=CONTEXT
set security context of copy to CONTEXT
--version
output version information and exit

By default, sparse SOURCE files are detected by a crude heuristic and the corresponding DEST file is made sparse as well. That is the behavior selected by --sparse=auto. Specify --sparse=always to create a sparse DEST file whenever the SOURCE file contains a long enough sequence of zero bytes. Use --sparse=never to inhibit creation of sparse files.

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup option or through the VERSION_CONTROL environment variable. Here are the values:

none, off
never make backups (even if --backup is given)
numbered, t
make numbered backups
existing, nil
numbered if numbered backups exist, simple otherwise
simple, never
always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup options are given and SOURCE and DEST are the same name for an existing, regular file.

docs.sun.com man pages section 1 User Commands

OPTIONS

OPERANDS

USAGE

EXAMPLES

ENVIRONMENT VARIABLES

EXIT STATUS

ATTRIBUTES

SEE ALSO

NOTES

SunOS 5.10  Last Revised 13 May 2004

O'Relly/Linux Command Directory cp

This directory of Linux commands is from Linux in a Nutshell, 5th Edition.

Click on any of the 687 commands below to get a description and list of available options. All links in the command summaries point to the online version of the book on Safari Bookshelf.

Buy it now, or read it online on Safari Bookshelf.

cp

cp [options] file1 file2 cp [options] files directory

Copy file1 to file2, or copy one or more files to the same names under directory. If the destination is an existing file, the file is overwritten; if the destination is an existing directory, the file is copied into the directory (the directory is not overwritten).

Options

-a, --archive

Preserve attributes of original files where possible. The same as -dpr.

-b, --backup

Back up files that would otherwise be overwritten.

-d, --no-dereference

Do not dereference symbolic links; preserve hard-link relationships between source and copy.

-f, --force

Remove existing files in the destination.

-i, --interactive

Prompt before overwriting destination files. On most systems, this flag is turned off by default except for the root user, who is normally prompted before overwriting files.

-l, --link

Make hard links, not copies, of nondirectories.

-p, --preserve

Preserve all information, including owner, group, permissions, and timestamps.

-P, --parents

Preserve intermediate directories in source. The last argument must be the name of an existing directory. For example, the command:

cp --parents jphekman/book/ch1 newdir

copies the file jphekman/book/ch1 to the file newdir/jphekman/book/ch1, creating intermediate directories as necessary.

-r, -R, --recursive

Copy directories recursively.

-S backup-suffix, --suffix=backup-suffix

Set suffix to be appended to backup files. This may also be set with the SIMPLE_BACKUP_SUFFIX environment variable. The default is ~. You need to explicitly include a period if you want one before the suffix (for example, specify .bak, not bak).

-s, --symbolic-link

Make symbolic links instead of copying. Source filenames must be absolute.

--sparse=[always|auto|never]

Handle files that have "holes" (are defined as a certain size but have less data). always creates a sparse file, auto creates one if the input file is sparse, and never creates a non-sparse file without holes.

-u, --update

Do not copy a file to an existing destination with the same or newer modification time.

-v, --verbose

Before copying, print the name of each file.

-V type, --version-control=type

Set the type of backups made. You may also use the VERSION_CONTROL environment variable. The default is existing. Valid arguments are:

t, numbered

Always make numbered backups.

nil, existing

Make numbered backups of files that already have them; otherwise, make simple backups.

never, simple

Always make simple backups.

-x, --one-file-system

Ignore subdirectories on other filesystems.

Example

Copy the contents of the guest directory recursively into the /archives/guest/ directory, and display a message for each file copied:

cd /archives && cp -av /home/guest guest




Etc

Society

Groupthink : Understanding Micromanagers and Control Freaks : Toxic Managers : BureaucraciesHarvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Two Party System as Polyarchy : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

Skeptical Finance : John Kenneth Galbraith : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotes : Oscar Wilde : Talleyrand : Somerset Maugham : War and Peace : Marcus Aurelius : Eric Hoffer : Kurt Vonnegut : Otto Von Bismarck : Winston Churchill : Napoleon Bonaparte : Ambrose Bierce : Oscar Wilde : Bernard Shaw : Mark Twain Quotes

Bulletin:

Vol 26, No.1 (January, 2013) Object-Oriented Cult : Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks: The efficient markets hypothesis : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Vol 23, No.10 (October, 2011) An observation about corporate security departments : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

 

The Last but not Least


Copyright © 1996-2014 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine. This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting hosting of this site with different providers to distribute and speed up access. Currently there are two functional mirrors: softpanorama.info (the fastest) and softpanorama.net.

Disclaimer:

The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: February 19, 2014