cp is one of the basic command in Unix. You already know that it is used to copy one or more files or directories from source to destination.
While this tutorial is for beginners, it is also helpful for everybody to quickly review various cp command options using some practical examples.
Even if you are using cp command all the times, probably one or more examples explained below might be new to you.
The general form of copy command:
1. Copy a file or directory from source to destination
NDM File transfer between unix to unix server. 257 views July 25, 2020. Manuc jackson May 13, 2010 0 Comments The below info i am getting in mail after,i ran my. Connect:Direct or NDM (Network data mover) is file transfer program, which copy file from source server to target. We can use this in both Mainframe and Mid-range to transfer file. It uses TCP/IP to transfer file and more made file transfer routine and reliable. Following a step in NDM to tranfer file. Hands down, the best free download manager aviable right now. It has all the features I want – grouping, multiple connections, clipboard integration, speed limiting.
To copy a file, you need to pass source and destination to the copy command. The following example copies the file from project/readme.txt to projectbackup/readme-new.txt
The first example shows a simple file transfer. A Process is submitted to a Sterling Connect:Direct for Microsoft Windows node to send, or push, a file to a Sterling Connect:Direct for UNIX node. In this example, the Sterling Connect:Direct for Microsoft Windows node is the PNODE and the Sterling Connect:Direct for UNIX node is the SNODE.
If you want to copy a file from one folder to another with the same name, just the destination directory name is good enough as shown below.
A directory (and all its content) can be copied from source to destination with the recursive option -r as shown below:
2. Copy multiple files or directories
You can copy more than one file from source to destination as shown below:
If the source files has a common pattern, use wild-cards as shown below. In this example, all c extension files gets copied to /home/thegeekstuff/projectbackup/src/ directory.
Copy multiple directories as shown below.
3. Backup before copying into a destination
In case if the destination file is already present with the same name, then cp allows you to backup the destination file before overwriting it.
In this example, the readme.txt exists in both project/ and projectbackup/ directory, and while copying it from project/ to projectbackup/, the existing readme.txt is backed up as shown below:
The existing file has been moved to readme.txt~ and the new file copied as readme.txt as shown below.
Talking about backup, it is important for you to understand how rsync command works to backup files effectively.
4. Preserve the links while copying
When you execute the cp command, if the source is a link file, then the actual file gets copied and not the link file. In case if you only want to copy the link as it is, specify option -d as shown below:
The following shows that without option -d, it will copy the file (and not the link):
To preserve the link while copying, do the following:
5. Don’t overwrite an existing file
If you want to copy only when the destination file doesn’t exist, use option -n as shown below. This won’t overwrite the existing file, and cp command will return with success exit code as shown below:
As you see below, the destination file didn’t get overwritten.
6. Confirm before overwriting (interactive mode)
When you use -i option, it will ask for confirmation before overwriting a file as shown below.
7. Create hard link to a file (instead of copying)
When you execute cp command, it is possible to create a hard link of the file (instead of copying the file). The following example creates the hard link for sample.txt file into directory test/,
As seen above, the test/sample.txt is a hard linked file to sample.txt file and the inode of both files are the same.
8. Create Soft link to a file or directory (instead of copying)
When you execute cp command, it is possible to create a soft link to a file or directory. In the following example, a symbolic link gets created for libFS.so.6.0.0 as libFS.so,
9. Preserve attributes of file or directory while copying
Using -p option, you can preserve the properties of a file or directory as shown below:
It is also possible to preserve only the required properties like mode, ownership, timestamps, etc.,
The following example preserves the mode of a file while copying it:
10. Copy only when source file is newer than the destination or missing
Copy doesn’t take much time for a small file, but it may take considerable amount of time when a huge file is copied. So, while copying a big file, you may want to make sure you do it only when the source file is newer than the destination file, or when the destination file is missing using the option -u as shown below.
In this example, the two files LICENSE and readme.txt will be copied from project/ to projectbackup/. However, the LICENSE file already exists in projectbackup/ directory and that is newer than the one in the project/ directory.
So, in this example, there is no need to copy LICENSE file again to projectbackup/ directory. This is automatically taken care by cp command, if you use -u option as shown below. In the below example, only readme.txt file got copied as indicated by the time-stamp on the file.
This file explains how to copy files between Unix/Linux machines and between Unix/Linux and other machines running Windows or Mac OSX.
The Course Linux Server is an example of a Unix/Linux machine.
For security reasons, SFTP file transfer to the CLS is not permitted. You may only transfer files from the CLS to your local machine.
1.1 Connecting from other machines to the Course Linux ServerIndex
Usually, you will be using some local machine (e.g. your own laptop or desktop machine running Windows or OSX) and you will want to connect to the Course Linux Server (CLS) to transfer a file. This means you need to know the network name of the CLS. It has two names:
The CLS is located on the Internet at address cst8207.idallen.ca
(a public address). This address is visible anywhere on the Internet, giving you access to the machine without needing to use the Algonquin College VPN. Use it from home or off-campus.
The CLS also has an on-campus private IP address cst8207-alg.idallen.ca
usable only via the VPN or while on campus at Algonquin College. Use the private address when you are on-campus.
1.2 Connecting from the Course Linux Server to other machinesIndex
Rarely, you might be logged in to the CLS and want to connect from the CLS (the local machine) to another machine (the remote machine) to transfer a file.
Connecting from the local Course Linux Server out to a remote machine (e.g. using a command-line ftp
or ssh
command) requires that the remote machine have its own accessible IP address – the remote machine should not itself be behind a firewall, unless you have arranged a pass-through port on that firewall. The remote machine must be running a file transfer server of some sort, to receive a connection from the CLS.
This is not usually the case, so most file transfer is done by running the file transfer software on the local machine and connecting to the CLS as the remote machine.
Ndm File Transfer Tutorial Unix Programming
1.3 Locked out of the CLSIndex
All file transfer programs need your CLS account name and CLS account password. If you fail to provide both accurately, you will be locked out of the CLS after several failed connection attempts.
A common error is to fail to provide your account name to the file transfer program, so that it uses a blank name. Don’t do that. Always make sure your CLS userid is being used.
If your IP address gets locked out, follow the directions to get your IP address unlocked.
1.4 Text File Line End DifferencesIndex
Ndm File Transfer Tutorial Unix Pdf
Text files contain lines of text delimited (or terminated) by an end-of-line character (or characters). The line end character(s) in text files is not the same between Unix/Linux, Windows, and some versions of MacOS.
- A text file written on Unix/Linux contains only a linefeed (
LF
) (sometimes called newlineNL
) character at the end of each text line. - A text file written on early MacOS contains only a carriage-return (
CR
) character at the end of each text line. - A text file written on DOS or Windows contains both a
CR
character and aLF
character at the end of every line.
Here is a Linux example using the od
file dumper command to make the newline character visible:
The Unix/Linux command file
can identify text files that have CR
(Macintosh) or CRLF
(DOS/Windows) line terminators:
Line-end differences may result in “staircasing” text (missing carriage returns) if you send a Unix/Linux text file to a Windows printer from some programs (e.g. Notepad). MacOS files may end up over-printed all on one line, due to the missing line feed characters!
Print a small sample first, and on Windows try using Write or Wordpad to read or print a Unix/Linux file instead of the old Notepad.
Some file transfer programs may optionally translate line-endings if they recognize that the file being transferred is a text file.