cp ~-/somefile . | cp -s ~/htf-daily/file1 ~/Desktop/ | cp -u file1 ~/Desktop | cp --attributes-only --preserve file2 file1 # copy attributes of file2

The "~-" refers to the directory "cd -" would take you to (your previous location).
$ cd /a/b/c
$ touch somefile
$ cd /x/y/z
$ cp ~-/somefile .

---------------

The -r or -R option for "recursive" means that it will copy all of the files including the files inside of subfolders.
The -a option as listed is the same as -dR which means it will preserve links as well as copy the contents of subdirectories. What it means by preserving links is that it will not follow links as it is recursively copying.
---------------
If you want, you can also ask cp to create a symbolic link instead of actually copying a file. This can be done using the -s command line option.
For example:
cp -s ~/htf-daily/file1 ~/Desktop/
How to create symbolic links using cp command

Q6. How to make cp overwrite destination file only if source is newer?

Sometimes, the requirement is to overwrite existing file only when the source file is newer - think of this process as updating the file. This can be done using the -u command line option.
For example, suppose you want to copy 'file1' residing in current working directory to the Desktop directory, but the destination already has a file named 'file1'. And you only want to copy if source is newer than destination. This can be accomplished using the following command:
cp -u file1 ~/Desktop
By default, the cp command follows symbolic links in source. This means that, for example, if you are trying to copy a file which is a symbolic link to another one, then by default, the copy action is taken on the the file the symbolic link refers to.  What I mean is, if ~/Desktop/file1 is a symbolic link to ~/htf-daily/file1, and you try copying ~/Desktop/file1 to ~/Downloads, then ~/htf-daily/file1 will get copied there.
However, if the requirement is to copy the symbolic link itself, the this can be made possible using the -P command line option, which asks cp to not follow symbolic links in source. So in our case, the command would be:
cp -P ~/Desktop/file1 ~/Downloads/

Q8. How to copy only file attributes?

Sometimes, the requirement could only be to copy attributes (like ownership and timestamps), and not the content of the file. This can be achieved using the --attributes-only command line option along with the --preserve option.
For example, file1 has following attributes:
-rw-rw-r-- 1 root himanshu 97 Jun 14 17:18 file1
And file2 has following attributes:
-rw-rw-r-- 1 root himanshu 179 May 25 15:09 file2
Note: You can use the ls command to fetch these attributes for a file. For more information on ls, head here.
And the requirement is to copy attributes of file2 and have them for file1 as well, then here's how this can be done:
cp --attributes-only --preserve file2 file1

Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all