Try DigitalOcean free for up to two months!

Linux+: Linux Shell 05 – Shell Commands Part 2

Discussion in 'Shell / Command Line' started by Jarret W. Buse, Jul 13, 2014.

  1. Jarret W. Buse

    Jarret W. Buse Well-Known Member Staff Writer

    Jul 3, 2013
    Likes Received:
    Linux+: Linux Shell 05 – Shell Commands Part 2

    In this article, we will cover another command that deal with files as well as directories. This command is to copy files (cp).

    Using the Shell to copy files may seem strange to users who would rather use the Graphical User Interface (GUI) to copy and paste files. Some people do seem to prefer the Shell to the GUI. For some, it may be a matter of creating scripts to perform similar tasks over and over again.

    NOTE: Any Shell command can be used in a script.

    The syntax of the copy command is as follows:

    cp source-file target-file

    The source and target can include path names. If you are in the current directory where the source is located, then the source does not require the path. For example, if I have a directory called '/media/sda7/Data' with a file called 'Document.doc' in it to be copied to my Home folder, I can use the following command:

    cp /media/sda7/Data/Document.doc /home/jarret/

    I can also use the tilde (~) to signify the current user's Home directory as well:

    cp /media/sda7/Data/Document.doc ~

    NOTE: For DOS users who may be used to the DOS copy command, you must specify the Target. In DOS, you did not need to specify the Target folder if it was the current directory. With the 'cp' command, a Target must be listed no matter the current directory.

    If the file is in the current directory, you only need to specify the filename and not the whole directory path. For example, in the previous example, if I were in the folder '/media/sda7/Data/' and wanted to copy the same file to the Home folder, I could use the following command:

    cp Document.doc ~

    To copy a file from another folder to the current folder, you can only specify the filename for the Target as shown:

    cp /media/sda7/Data/Document.doc Document.doc

    It is possible to copy and rename a file at the same time. To do this, specify a filename for the Target option as well. For example, to copy the 'Document.doc' file to the Home folder and rename it 'NewDoc.doc':

    cp /media/sda7/Data/Document.doc ~/NewDoc.doc

    Of course, the file can be copied and renamed within the same folder as follows:

    cp /media/sda7/Data/Document.doc /media/sda7/Data/NewDoc.doc

    If you were in the folder '/media/sda7/Data/', then the command would be:

    cp Document.doc NewDoc.doc

    The copy command can use wildcards. The asterisk (*) is used for one or more characters and the question mark (?) represents a single character.

    For example, to copy all files in the current folder starting with 'to' to the '~/data' folder would be:

    cp to* ~/data

    To copy all files from the Home folder to the '~/data' directory, which are three characters long and start with a 'g' would be:

    cp ~/g?? ~/data

    You can also copy all files from the current directory to the Home folder which have 'f' as the second letter:

    cp ?f* ~/

    It is possible to copy multiple files without using wildcards, especially if they have nothing in common in the filename to use a wildcard. After the cp command, list all the files one after another and then the target folder as shown:

    cp file1.* document.doc database* ~/data

    Like most other commands, cp uses optional parameters as listed:

    • -a – copies file as exact as possible with attributes, timestamps, etc.

    If we need an exact copy of the file '~/data/document.doc' to '~/backup':

    cp -a ~/data/document.doc ~/backup

    • --attributes-only – copies attributes from one file to another
    To copy the attributes of '~/data/document.doc' to ~/data/document2.doc':

    cp --attributes-only ~/data/document.doc ~/data/document2.doc

    • --backup – makes a backup of the files in a specified directory. The option has a few parameters which is specified as --backup=simple:

      • none – never backup
      • numbered – make numbered backups
      • existing – number if numbered backup exists, otherwise do simple
      • simple – non-numbered backup

    The 'numbered backup' copies the file normally the first time if the file does not exist. The second time, the file is copied and numbered. For example, after backing up the file 'Tron.jpg' to my Home folder twice, I will have the following two files:

    1. Tron.jpg

    2. Tron.jpg.~1~

    • -b – copies a file to the specified directory as a backup. The second copy will have a tilde attached to it. All backups, afterwards, will overwrite the first file in the target. For example, after backing up the file 'Tron.jpg' to my Home folder twice, I will have the following two files:

    1. Tron.jpg
    2. Tron.jpg~
    • --copy-contents – used to copy special files such as devices in the /dev directory when doing a recursive copy.

    If I were to do a recursive copy, which is all files within all directories and subdirectories, as well as copying all special files, I would use the –copy-contents option.

    • -d – the copy command will copy files while soft and hard links are preserved.
    • -f – if the target file cannot be opened to copy over, the target file is deleted and the copy is tried again. Without the option, any file which cannot be opened will cause an error.
    • -i – prompts before overwriting a target file which exists.
    • -H – follows soft links which are specified as a source file and copies the actual file and not the link.
    • -l – creates hard links for files.
    • -L – copies actual files and not the links to the files.
    • -n – do not overwrite existing files. Only write files which do not exist in Target directory.
    • -p – copies files and keeps owner and timestamp information on Target files.
    • -P – copies files as usual, but soft links are kept as soft links.
    • --preserve=attributes – allows you to preserve a specific attribute of the file being copied. List of attributes follow:
      • mode – keep access list and file mode bits
      • ownership – keep owners and groups
      • timestamps – preserve modification and access timestamps
      • links – preserves hard and soft links without changing one to another
      • context – preserves security context of file
      • xattr – keeps extended attributes of file
      • all – equivalent to using all attribute options listed

    If no attributes are given, the default is: 'mode, ownership, timestamps'. Multiple attributes can be used by separating them with commas.

    • --no-preserve – list of attributes not to be preserved after the copy

    • --parents – creates all directories if they do not exist. Target directory must exist

    The Target directory must exist, but not all directories need to exist. For example, if a directory structure such as 'Dir1/Dir2/Dir3/' exists with a file named 'File1.txt' in the Dir3 directory. We want to copy it to 'NewDir1' with the same folder structure. We would do this using the following command:

    cp --parent Dir1/Dir2/Dir3/File1.txt NewDir1

    This command will create a folder structure with the file as: NewDir1/Dir1/Dir2/Dir3/File1.txt

    • -r – copies a directory recursively, but does not copy source files of soft links
    • --reflink – performs a Copy-On-Write (COW) if the File System supports it. Target and Source files become a Hard Link which is where they share the same data blocks on the hard disk. If one file is modified, then the Hard link is removed

    The --reflink allows two options (--reflink=option)

    1. always – if COW is not supported, report error for file and go to the next file
    2. auto – if COW is not supported, then perform a standard copy

    • --remove-destination – destination file is removed if it exists before copy is attempted
    • --sparse=option – allows for files to be written where consecutive zeroes are written in a special way to save space. There are three options for the sparse parameter:
      1. auto – (default) if the source file is sparse, then make the target file sparse as well
      2. always – attempt to make the target file sparse, even if the source is not sparse
      3. never – target file is never made sparse
    • --strip-trailing-slashes – remove trailing slashes from the source file list

    • -s – make Soft Links of copied files
    • -S=suffix – specifies suffix to use for backup (use with the -b parameter)

    • -t directory – specifies the target directory

    • -T – do not treat the last listing on the command as a target directory
    • -u – only copy the file to the destination if the source file is newer than the target file
    • -v – prints the name of each file when it is copied
    • -x – skips sub-directories when they are on different file systems, such as a mount-point
    • --help – displays help for cp
    • --version – displays the version of cp

    Attached Files:

    Niclas and DevynCJohnson like this.
  2. wenzong.pang

    wenzong.pang New Member

    Jul 25, 2014
    Likes Received:

Share This Page