iEntry 10th Anniversary LinuxHaxor WH MH

Manipulating Files






This lesson will introduce you to the following commands:

  • cp – copy files and directories
  • mv – move or rename files and directories
  • rm – remove files and directories
  • mkdir – create directories

These four commands are among the most frequently used Linux commands. They are the basic commands for manipulating both files and directories.

Now, to be frank, some of the tasks performed by these commands are more easily done with a graphical file manager. With a file manager, you can drag and drop a file from one directory to another, cut and paste files, delete files, etc. So why use these old command line programs?

The answer is power and flexibility. While it is easy to perform simple file manipulations with a graphical file manager, complicated tasks can be easier with the command line programs. For example, how would you copy all the HTML files from one directory to another, but only copy files that did not exist in the destination directory or were newer than the versions in the destination directory? Pretty hard with with a file manager. Pretty easy with the command line:

[me@linuxbox me]$ cp -u *.html destination

Wildcards

Before I begin with our commands, I want to talk about a shell feature that makes these commands so powerful. Since the shell uses filenames so much, it provides special characters to help you rapidly specify groups of filenames. These special characters are called wildcards. Wildcards allow you to select filenames based on patterns of characters. The table below lists the wildcards and what they select:

Wildcard Meaning
* Matches any characters
? Matches any single character
[characters] Matches any character that is a member of the set characters. The set of characters can be expressed as a range of characters. (For example, [A-Z] represents all uppercase letters)
[!characters] Matches any character that is not a member of the set characters

Using wildcards, it is possible to construct very sophisticated selection criteria for filenames. Here are some examples of patterns and what they match:

Pattern Matches
* All filenames
g* All filenames that begin with the character “g”
b*.txt All filenames that begin with the character “b” and end with the characters “.txt”
Data??? Any filename that begins with the characters “Data” followed by exactly 3 more characters
[abc]* Any filename that begins with “a” or “b” or “c” followed by any other characters
[A-Z]* Any filename that begins with an uppercase letter. This is an example of a range.
BACKUP.[0-9][0-9][0-9] Another example of ranges. This pattern matches any filename that begins with the characters “BACKUP.” followed by exactly 3 numerals.
[!a-z]* Any filename that does not begin with a lowercase letter.

You can use wildcards with any command that accepts filename arguments.

cp

The cp program copies files and directories. In its simplest form, it copies a single file:

[me@linuxbox me]$ cp file1 file2

It can also be used to copy multiple files to a different directory:

[me@linuxbox me]$ cp file1 file2 file3 directory

Other useful examples of cp and its options include:

Command Results
cp file1 file2 Copies the contents of file1 into file2. If file2 does not exist, it is created; otherwise, file2 is overwritten with the contents of file1.
cp -i file1 file2 Like above however, since the “-i” (interactive) option is specified, if file2 exists, the user is prompted before it is overwritten with the contents of file1.
cp file1 dir1 Copy the contents of file1 (into a file named file1) inside of directory dir1.
cp -R dir1 dir2 Copy the contents of the directory dir1. If directory dir2 does not exist, it is created. Otherwise, it creates a directory named dir1 within directory dir2.

mv

The mv command performs two different functions depending on how it is used. It will either move one or more files to a different directory, or it will rename a file or directory. To rename a file, it is used like this:

[me@linuxbox me]$ mv filename1 filename2

To move files to a different directory:

[me@linuxbox me]$ mv file1 file2 file3 directory

Examples of mv and its options include:

Command Results
mv file1 file2 If file2 does not exist, then file1 is renamed file2. If file2 exists, its contents are replaced with the contents of file1.
mv -i file1 file2 Like above however, since the “-i” (interactive) option is specified, if file2 exists, the user is prompted before it is overwritten with the contents of file1.
mv file1 file2 file3 dir1 The files file1, file2, file3 are moved to directory dir1. dir1 must exist or mv will exit with an error.
mv dir1 dir2 If dir2 does not exist, then dir1 is renamed dir2. If dir2 exists, the directory dir1 is created within directory dir2.

rm

The rm command deletes (removes) files and directories.

[me@linuxbox me]$ rm file

It can also be used to delete a directory:

[me@linuxbox me]$ rm -r directory

Examples of rm and its options include:

Command Results
rm file1 file2 Delete file1 and file2.
rm -i file1 file2 Like above however, since the “-i” (interactive) option is specified, the user is prompted before each file is deleted.
rm -r dir1 dir2 Directories dir1 and dir2 are deleted along with all of their contents.

Be careful with rm!

Linux does not have an undelete command. Once you delete a file with rm, it’s gone. You can inflict terrific damage on your system with rm if you are not careful, particularly with wildcards.

Before you use rm with wildcards, try this helpful trick: construct your command using ls instead. By doing this, you can see the effect of your wildcards before you delete files. After you have tested your command with ls, recall the command with the up-arrow key and then substitute rm for ls in the command.

mkdir

The mkdir command is used to create directories. To use it, you simply type:

[me@linuxbox me]$ mkdir directory


Comments are closed.

  • Buy Cheap online pharmacy no prescription nizral cream Online 24/Online Pharmacy. Top Online Pharmacy.
  • Buy Cheap when to take suhagra Online Best Prices. The Largest Internet Pharmacy.
  • Buy Cheapest pharmacy software vendors Online Discount Online Pharmacy. Best Online.
  • Buy Cheapest zofran medicine Now Low Prices. 100% Satisfaction Guaranteed.
  • Buy Cheapest purchase prednisolone alternative Now Top Online Pharmacy. Cheap Online Pharmacy.
  • Buy Cheap medicine amlodipine Online The Largest Internet Pharmacy. Best Online.
  • buy myambutol Online Without Prescription Cheap Meds Without Prescription.
  • Buy arthritis medication for dogs Without Prescription Doctor. Best Prices. Best Internet.
  • Buy Cheapest acne medication online Online Pharmacy Store. 24/Online Pharmacy.
  • Buy Cheap cvs pharmacies with clinics Now Guaranteed Shipping. No Prescription Needed.
  • Buy Cheapest cvs prescription coupon Online Guaranteed Shipping. WorldWide Shipping.
  • Buy Cheap order lopressor Now Cheap Online Pharmacy. 24/Online Pharmacy.
  • Buy Cheap canadian pharmacy no prescription adderall Now Pharmacy Store. Top Online Pharmacy Supplier.
  • Buy Cheapest mistake pharmacy Now Low Prices. Cheap Prescription Drugs.
  • Buy Cheap tamoxifen evista Online Best Drugstore. Discount Online Pharmacy.
  • Buy Cheap candida medication Online Pharmacy At The Best Price! Best Drugstore.
  • Buy Cheap constipation medications over the counter Now Online Prices For constipation medications over the counter! Free Viagra Pills!
  • Buy Cheapest hpv medications Online Pharmacy Store. Top Online Pharmacy.
  • Buy Cheap buy pills wellbutrin sr online Online WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheapest zyban medication side effects Online Best Prices. Pharmacy At The Best Price!
  • Buy alternative medicine to treat constipation Online Without Prescription. Best Online. Best Prices.
  • Buy Cheap bipolar disorder medications list Now Cheap Prescription Drugs. Free Viagra Pills!
  • Buy Cheap healthy bones organic vitamin Now Pharmacy Store. 24/Internet)(safe Pharmacy.
  • Buy Cheap order zimulti Online No Prescription Needed. Pharmacy Store.
  • Buy Cheapest buy antifungals online pharmacy Now Discount Online Pharmacy. Best Prices.
  • Buy Cheap veterinary health dog Now Buy Medications Online. Guaranteed Shipping.
  • Buy Cheap epilepsy medications weight loss Now Top Online Pharmacy Supplier. Best Drugstore.
  • Buy medication simvastatin Online Without Prescription. Best Online. Best Prices.
  • Buy Cheap online pet meds Now Cheap Online Pharmacy. 24/Online Pharmacy.
  • Buy Cheapest take female passion strips pills Now Buy Medications Online. Best Online.
  • Buy Cheapest pcos metformin Now Guaranteed Shipping. Free Viagra Pills!
  • Buying Cheap cheap detrol. Mexican Rx, Best Prices. 24/Online Pharmacy.
  • Buy Cheap medco.com prescriptions Now The Largest Internet Pharmacy. Pharmacy Store.
  • Buy Cheapest online veterinary prescriptions Now Buy Medications Online. Pharmacy Store.
  • Buy Cheap buy tinidazole overnight delivery Now Best Online. No Prescription Online Pharmacy.
  • Buy Cheap osco pharmacy prices Now Best Internet. Safe And Secure Payment System.
  • Buy Cheapest menopause tablets natural Now Free Viagra Pills! Buy Medications Online.
  • Buy Cheap buy dostinex without prescription Online Guaranteed Shipping. Top Online Pharmacy.
  • Buy Cheapest antibiotics for pregnant women Now Prescription Drugs And Generic Medications.
  • Buy periactin otc Without Prescription Doctor. Internet Prices For periactin otc!
  • Buy Cheap cheap detrol Now Best Internet. Special Prices For cheap detrol!
  • Buy Cheapest metoprolol medication Now Best Online. Drugs, Health And Beauty.
  • Buy Cheap pharmacy zolpidem Online Guaranteed Shipping. Online Medical Shop.
  • Buy Cheapest lopressor medicine Online Low Prices. Order Cheap Meds Without Rx.
  • Buy Cheap cheap asthma inhaler Online Pharmacy Store. Special Prices For cheap asthma inhaler!
  • Buy Cheapest medicine tamiflu Online Discount Pharmacy Online. Best Internet.
  • Buy Cheapest south african pharmacists Online Top Online Pharmacy. Best Prices.
  • Buy Cheap men health belly off Now Cheap Prescription Drugs. Best Drugstore.
  • Buy Cheapest human pheromone Online Cheap Pharmacy Online. Best Online.
  • Buy Cheap what is loratadine side effects Online Special Prices For what is loratadine side effects! Best Internet.
  • Buy Cheap wellbutrin sr prescription online Now Safe And Secure Payment System. Best Prices.
  • Buy Cheap buy tinidazole overnight delivery Now Top Online Pharmacy. Discount Pharmacy Online.
  • Buy Cheap purchase prinivil Now Cheap Pharmacy Online. Guaranteed Shipping.
  • Buy Cheap south african pharmacists Online Online Medical Shop. WorldWide Shipping.
  • Buy Cheapest online pharmacy no prescription terramycin Online Online Medical Shop. Best Online.
  • Buy Cheapest isotretinoin pregnancy Now 24/Internet)(safe Pharmacy. Low Prices.
  • Buy Cheap sumycin price Online Free Viagra Pills! Top Online Pharmacy.
  • Buy Cheap avapro lisinopril Online Pharmacy At The Best Price! Pharmacy Store.
  • Buy border pharmacies Online Without Prescription. Low Prices. Pharmacy Store.
  • Buy Cheapest california board of pharmacy Now Buy Medications Online. Best Online.
  • Buy Cheap wellbutrin sr price Now Order Cheap Meds Without Rx. Low Prices.
  • Buy Cheap order pyridium next day delivery Now 24/Online Pharmacy. Cheap Pharmacy Online.
  • Buy Cheap anabolic pharmacy Now Best Internet. Top Online Pharmacy Supplier.
  • Buy Cheap levothroid price Online Best Online. 100% Satisfaction Guaranteed.
  • Buy Cheapest sam's club pharmacy drug list Online Discount Pharmacy Online. Best Internet.
  • Buy Cheapest osteoporosis medicine your bones Now Discount Pharmacy Online. Pharmacy Store.
  • Buy Cheap combivent inhaler dosage Online Cheap Pharmacy Online. Best Internet.
  • Buy Cheap walmart discount medications list Now Online Medical Shop. Cheap Prescription Drugs.
  • Buy Cheapest lexapro wellbutrin withdrawal Now Best Drugstore. Online Medical Shop.
  • Buy Cheap candida medication Online Free Viagra Pills! Cheap Online Pharmacy.
  • Buy Cheap hydrocodone diarrhea Now Guaranteed Shipping. Drugs, Health And Beauty.
  • Buy Cheap approved drugs postmenopause osteoporosis Now Online Medical Shop. 24/Online Pharmacy.
  • Buy Cheapest medication respiratory infection Online 24/Online Pharmacy. Low Prices.
  • Buy Cheap albuterol inhaler no prescription needed Online Buy Medications Online. Free Viagra Pills!
  • Buy Cheapest torsemide no prescription Online Best Internet. WorldWide Shipping.
  • Buy Cheapest lady snoring Online Best Internet. Top Online Pharmacy.
  • Buy Cheap mexican pharmacy online Online Internet Prices For mexican pharmacy online! Low Prices.
  • Buy Cheapest most common medication for bipolar disorder Now Best Internet. Buy Medications Online.
  • Buy Cheap bph medication Online Buy Medications Online. Pharmacy Store.
  • Buy Cheapest pharmacy for psoriasis meds Online Best Internet. Online Medical Shop.
  • Buy Cheap prednisolone side effects children Now Top Online Pharmacy. WorldWide Shipping.
  • how to increase female sex drive Online Without Prescription Pharmacy Store. Best Drugstore.
  • Buy Cheap what antibiotics are prescribed for chlamydia Online Best Internet. Cheap Prescription Drugs.
  • Buy Cheapest my canadian pharmacy reviews Online Low Prices. Discount Pharmacy Online.
  • Buy Cheapest order prednisolone online Now Best Internet. Discount Online Pharmacy.
  • Buy Cheap side effects digoxin medication Now Pharmacy Store. 24/Internet)(safe Pharmacy.
  • Buy Cheapest prinivil generic Now Drugs, Health And Beauty. Low Prices.
  • Buy Cheap causes cholesterol lowering medications Now Free Viagra Pills! Online Medical Shop.
  • Buy Cheapest which antibiotic is best for strep throat Now Pharmacy Store. No Prescription Needed.
  • Buy Cheap oxycodone online pharmacy Now Best Drugstore. Discount Online Pharmacy.
  • Buy Cheap take tinidazole cheap Online Cheap Pharmacy Online. WorldWide Shipping.
  • Buy Cheap wellbutrin sr price Now 24/Online Pharmacy. Buy Medications Online.
  • Buy Cheap ranitidine medication used Now WorldWide Shipping. 24/Online Pharmacy.
  • Buy Cheapest psoriasis medications Now Online Medical Shop. Cheap Online Pharmacy.
  • Buy Cheapest buy dostinex online without prescription Now Best Online. Discount Pharmacy Online.
  • Buy Cheap which antibiotic is best for strep throat Online Low Prices. Discount Pharmacy Online.
  • Buy Cheap acne medication online Now Best Online. FDA Approved Rx: Online Pharmacy.
  • Buy Cheapest order nitrofurantoin Online Guaranteed Shipping. Best Prices.
  • Buy Cheap symptoms of ovarian cancer Now Special Prices For symptoms of ovarian cancer! Free Viagra Pills!
  • Buy Cheapest use proactiv acne medication Now Pharmacy Store. Drugs, Health And Beauty.