Home > Articles

This chapter is from the book

Copying and Moving

Course Objectives Covered

  1. Executing Commands at the Command Line (3036)

  2. Common Command Line Tasks (3036)

  1. Copying and Moving Files and Directories (3036)

  2. Creating Directories (3036)

  3. Deleting Files and Directories (3036)

System administration would be so much easier if nothing ever moved or changed. Unfortunately, it is very rare for anything to stay static any more, and changes take place at a nonstop pace. Linux offers two powerful utilities for copying and moving files: cp and mv, respectively, and a third utility—dd—that combines features of both.

cp

cp works with both files and directories and can move multiple entities at a time using wildcards. After the name of the utility, you must specify the source, followed by the target. The simplest use of all can be illustrated as follows:

$ ls -l fileone onefile
ls: onefile: No such file or directory
-rw-r--r--  1  root  root  85  Aug 22 10:26
  fileone
$
$ cp fileone onefile
$ ls -l fileone onefile
-rw-r--r--  1  root  root  85  Aug 22 10:26
  fileone
-rw-r--r--  1  edulaney  users  85  Aug 30 16:18
  onefile
$ 

Notice that the original entry (source) remains unchanged, but now there is a second entry (target) as well. On the second entry, the contents are identical, but the date and time are those of the present (when cp was executed), not those of the source. Notice as well that the owner and group of the new file became that of the user executing the command. This is the same action that would take place if you were creating the target file completely from scratch.

NOTE

To be able to copy a file—create a new entity equal in content to another—you need only read permission to the source.

The -p option can be used to force as many of the old variables to remain the same. It preserves what it can in terms of attributes:

$ ls -l fileone nextfile
ls: nextfile: No such file or directory
-rw-r--r--  1  root  root  85  Aug 22 10:26  fileone
$
$ cp -p fileone nextfile
$ ls -l fileone nextfile
-rw-r--r--  1  root  root  85  Aug 22 10:26
  fileone
-rw-r--r--  1  edulaney  users  85  Aug 22 10:26
  nextfile
$ 

Notice that the date and time associated with the source were kept, but the owner and group still must change.

When you do a copy operation, the utility first checks to see if the source file exists. If it does, whatever file is specified as the target is created (with a size of zero), and the contents of the source are copied. The emphasis here is on the fact that the target is always created—regardless of whether it existed before or not. Here's an illustration:

$ ls -l fileone filetwo
-rw-r--r--  1  root  root  85  Aug 22 10:26
  fileone
-rw-r--r--  1  root  root  16432  Aug 28 13:43
  filetwo
$
$ cp fileone filetwo
$ ls -l fileone filetwo
-rw-r--r--  1  root  root  85  Aug 22 10:26
  fileone
-rw-r--r--  1  edulaney  users  85  Aug 30 16:18
  filetwo
$ 

The original contents of filetwo have been lost, except for any backup tape versions, as filetwo is created to be a copy of fileone. There is a -i (as in inquire) option, which can be used to always ask if you really want to erase the contents of the target file if it already exists:

$ ls -l fileone filetwo
-rw-r--r--  1  root  root  85  Aug 22 10:26
  fileone
-rw-r--r--  1  root  root  16432  Aug 28 13:43
  filetwo
$
$ cp -i fileone filetwo
cp: overwrite 'filetwo'? 

At the prompt, you can enter "y" to perform the operation, or anything else to stop it.

You can copy a number of files from one directory to another so long as the last item on the command line is a valid directory path into which the files will be copied:

$ ls -l /usr/home/sdulaney
-rw-r--r--  1  root  root  85  Aug 22 10:26  exit
$
$ cd /usr/home/examples
$ ls -l s* q*
-rw-r--r--  1  root  root  585  Aug 23 12:16
  questions
-rw-r--r--  1  root  root  1985  Aug 24 15:17
  samples
-rw-r--r--  1  root  root  8501  Aug 25 18:30
  snapshot01.gif
$ cp s* q* /usr/home/sdulaney
$ cd ../sdulaney
$ ls -l
-rw-r--r--  1  root  root  85  Aug 22 10:26  exit
-rw-r--r--  1  root  root  585  Aug 31 22:50
  questions
-rw-r--r--  1  root  root  1985  Aug 31 22:50
  samples
-rw-r--r--  1  root  root  8501  Aug 31 22:50
  snapshot01.gif
$

To move an entire directory from one location to another, use the -r or -R option to recursively move the directory as well as any subdirectories and files beneath it. Other options that can be used include -f to force a copy without any prompting (the opposite, so to speak, of -i); -u to copy only when the source file is more recent (updated) than the target; and -v for verbose mode (show all operations, rather than perform them silently).

Lastly, the -P option will reproduce the entire path to a file in another location—creating directories and subdirectories as needed to do so.

mv

The move utility (mv) can be used for several operations. At the risk of being overly simplistic, this includes the ability to

  1. Rename a file

  2. Rename a directory

  3. Move a file from one directory to another

  4. Move a subdirectory from one directory to another

  5. Move an entity to another partition or media

The simplest operation is to rename a file in its current directory:

$ ls -l file*
-rw-r--r--  1  root  root  85  Aug 22 10:26
  fileone
-rw-r--r--  1  root  root  16432  Aug 28 13:43
  filetwo
$
$ mv fileone filethree
$ ls -l file*
-rw-r--r--  1  root  root  85  Aug 22 10:26
  filethree
-rw-r--r--  1  root  root  16432  Aug 28 13:43
  filetwo
$

The dates, permissions, and everything else associated with fileone stay with filethree. This is because when a file is moved within the same directory (or even on the same partition), all that changes is the information about the name—no physical operation takes place; only the descriptor is changed. The move has become a simple rename operation.

NOTE

As simplistic as it may sound, always remember that when you copy a file, you leave the original intact and create something that did not exist before—thus a new set of attributes is created for the new entity. When you move a file, however, the default action is a rename—you are changing only the name of the original entity and not creating anything new.

One way to put it in perspective is that if you copy a file that is 9MB in size, it will take longer than if you copy a file that is 9 bytes in size. With move being used as a rename, it will take the same amount of time to do the operation regardless of the size of the file.

As with the copy operation, if you attempt to move a file to a name that already exists, the contents of the target are lost. Here's an illustration:

$ ls -l file*
-rw-r--r--  1  root  root  85  Aug 22 10:26
  fileone
-rw-r--r--  1  root  root  16432  Aug 28 13:43
  filetwo
$ mv fileone filetwo
$ ls -l file*
-rw-r--r--  1  root  root  85  Aug 22 10:26
  filetwo
$ 

The -i option (as in inquiry or interactive) can be used to prompt before overwriting, and the opposite of it is -f (for force), which is the default operation. The -u option will only do the move if the source file is newer, and -v turns on verbose mode. The -b option makes a backup of the target file, if it exists, with a tilde as the last character—essentially performing a pseudo-copy operation:

$ ls -l help*
-rw-r--r--  1  root  root  85  Aug 22 10:26
  helpfile
$ mv -b helpfile helpfiletwo
$ ls -l help*
-rw-r--r--  1  root  root  85  Aug 22 10:26
  helpfiletwo
$ 
$ ls -l file*
-rw-r--r--  1  root  root  85  Aug 22 10:26
  fileone
-rw-r--r--  1  root  root  16432  Aug 28 13:43
  filetwo
$ mv -b fileone filetwo
$ ls -l file*
-rw-r--r--  1  root  root  85  Aug 22 10:26
  filetwo
-rw-r--r--  1  root  root  16432  Aug 28 13:43
  filetwo~
$ 

In the first instance, there was not an existing file with the target name present, so the -b option was ignored. In the second instance, a file by the name of the target was in existence, so the original target file is renamed with a tilde (~) as the last character.

NOTE

The -b option can also be used with cp to perform the same action during a copy as it does with mv.

dd

The device-to-device (dd) utility is used to copy a file from one device to another. It goes beyond that in functionality, however, for it can convert a file during the copy process from one format to another. It can convert from EBCDIC to ASCII (and reverse), change uppercase to lowercase (and reverse as well), and work with bytes, blocks, or keywords.

The most common use for dd is copying files to and from removable media, and you must use arguments that can include

  • bs—block file size

  • if—input file

  • of—output file

Removing Files and Directories

When files are no longer needed, they can be removed from the system with the rm (remove) command. Be careful using this command, for Linux offers no undelete command or function like those found in other operating systems.

$ ls -l file*
-rw-r--r--  1  root  root  85  Aug 22 10:26
  fileone
-rw-r--r--  1  root  root  16432  Aug 28 13:43
  filetwo
$ rm fileone
$ ls -l file*
-rw-r--r--  1  root  root  16432  Aug 28 13:43
  filetwo
$ 

When used with the -i option, a prompt appears before each file to be deleted. Pressing Y deletes the file, and pressing any other character skips the file.

$ ls -l t*
-rw-r--r--  1  root  root  85  Aug 22 10:26
  today
-rw-r--r--  1  root  root  16432  Aug 28 13:43
  tuesday
$ rm -i t*
rm: remove 'today'?

The -f option forces deletion, and -v puts the utility in verbose mode. The -r or -R option recursively deletes directories (including subdirectories and files beneath). In order to delete a file, you have to have write permissions within the directory where it resides.

NOTE

Write permission is only required on the directory from which you are deleting the file—not on the file itself.

A safer utility, rmdir, can be used to delete directories that have nothing beneath them. It will only delete empty directories and cannot be used for directories that have files or subdirectories beneath them. The only option that can be used with rmdir is -p to remove parent directories (if empty).

$ ls -R kdulaney
kdulaney:
docs

kdulaney/docs:
attempt
$ 
$ rmdir kdulaney
rmdir: kdulaney: Directory not empty
$ 

Because there is a file (attempt) within kdulaney/docs, and a subdirectory (docs) beneath kdulaney, the directory kdulaney cannot be deleted with the rmdir utility. There are three possible ways to accomplish the task:

  1. Use the rm -r command.

  2. Delete attempt with rm, and then delete docs with rmdir, and—finally—delete kdulaney with rmdir.

  3. Delete attempt with rm, and then delete kdulaney/docs with rmdir -p.

NOTE

Because rmdir can only delete empty directories, it is naturally a safer utility to use than rm for cleaning a system.

Making Directories

Now that you've learned how to copy, move, and delete directories, the only order of business left is to make a directory, which you can do by using the mkdir command. Used without options, it creates a child directory (subdirectory) in the current directory. There are two options that work with it as well:

  • -m—To specify permissions other than the default for the new directory (covered in a later chapter)

  • -p—To create a parent and child in one command

Here are some examples of the utility:

$ pwd
/usr/home
$ mkdir edulaney
$

This created the subdirectory edulaney beneath /usr/home(/usr/home/edulaney).

$ pwd
/usr/home
$ mkdir kdulaney/docs
mkdir: cannot make directory 'kdulaney/docs': No such file or
  directory
$
$ mkdir -p kdulaney/docs
$ cd kdulaney
$ cd docs
$

In the first attempt, the utility fails as you cannot create multiple directories by default. If you use the -p option, however, the multiple directories are created.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020