Linux/Unix: How to copy a directory and save the date/time file information

If you need to copy a directory on Unix/Linux and want to preserve the date/time information while copying the directory and files, use the -p option to save the date/time information, and the -r option to copy the directory properly. For instance, I just used this cp command to copy a directory named OldDir to a new directory named NewDir, while retaining all of the date/time file information:

Unix find command: How to move a group of files into the current directory

I just bought a bunch of MP3 music files from Amazon, and when I downloaded the zip file they provide onto my Mac, it was a bunch of files in a bunch of subdirectories; not really convenient to work with when you’re trying to import them into iTunes. So I used this Unix find command to move all of the music files from the subdirectories they were scattered in into the root directory that was created when I expanded the zip file:

cd Amazon-Music-Folder
find . -type f -exec mv {} . \;

If you ever need to either copy or move a bunch of files with a single command, I hope this example shows the correct find command syntax for your needs. (If you need to copy the files, use the cp command instead of the mv command.)

The Linux copy command (cp)

Linux file copy FAQ: How do I copy Linux files and directories? (Or, Can you share some cp command examples?)

You use the cp command to copy files and directories on Linux systems. Let's look at some copy examples to see how this works.

Using Linux cp to copy files

At its most basic, here's how you copy a Linux file:

Linux cp command man page

This page shows the contents of the Linux cp comamnd man page. This cp command output was created on a CentOS Linux system.

You can see this same cp command man page output by entering this command on your own Linux system:

Error message: mv cannot unlink remove file, operation not permitted

I got this Unix/Linux error message today "mv: cannot unlink '/tmp/forms-1.2.1.jar': Operation not permitted" when trying to move (mv) the file forms-1.2.1.jar from the /tmp directory to another directory. The full error output looked like this:

Linux cp command examples

The Linux cp command lets you copy one or more files or directories. To be clear, the cp command makes a duplicate copy of your information, whereas the mv command moves your information from one location to another.

Let's take a look at some Linux cp command examples.

Simple file copying

Using a Linux or Unix system, to copy a file named "foo" to a new file named "bar" just type:

Linux tutorial, part 3 (ssh, cd, ls, cp, mv)

Logging in to a remote system

To login to that system I'll use a command named ssh, which stands for "secure shell". It's basically an encrypted login session to a remote system. To login to that remote system I'll type this command in my terminal window:

ssh al@foo.bar.com

(Of course everything after the ssh command there is made up. I don't have a login account on any systems named anything like that.)

Linux ‘find’ example: How to copy one file to many directories

Did you ever need to take one file on a Linux or Unix system and copy it to a whole bunch of other directories? I had this problem recently when I changed some of the header files on this website. I had a file named header.html, and I needed to copy it to a bunch of subdirectories.

Using Unix, Linux, or Cygwin this turns out to be really easy. I used the Linux find command, in combination with the cp command. Once I figured out the right syntax, I was able to copy the file to nearly 500 directories in just a few seconds.