June 1, 2015 Notes:

"rm" Command

--- “ rm “ command

--> Cleaning up the file system is a task that needs to be performed on a regular basis. This command is used for this purpose

--> Removes files or directories

-- “rm” options:

> -f, --force / Ignore nonexistent files and never prompt before removing

> -i / prompt before every removal

> -I / prompt once before removing more than 3 files, or when removing recursively

> -r , -R, --recursive / Remove directories and their content recursively

 Examples:

To remove a file whose name begins with a dash ("-"), you can specify a double dash ("--") separately before the filename. This is necessary so that rm does not misinterpret the filename as an option.
For instance, if there is a file in your current directory named "-file.txt", you can delete it with the command

rm -- -file.txt

Or, you can delete it by referring to it with a pathname. For instance, if the file "-file.txt" was located in the directory "/home/chope", you

could delete it using:

rm /home/chope/-file.txt

or, if /home/chope is your current directory,

rm ./-file.txt

More “rm” Examples

rm myfile.txt

 Remove the file myfile.txt. If the file is write-protected, you will be prompted to confirm that you really want to delete it.

rm -f myfile.txt

 Remove the file myfile.txt. You will not be prompted, even if the file is write-protected; if rm can delete the file, it will.

rm *

 Remove all files in the working directory. If it is write-protected, you will be prompted before rm removes it.

rm -f *

 Remove all files in the working directory. rm will not prompt you for any reason before deleting them.

rm -i *

 Attempt to remove every file in the working directory, but prompt before each file to confirm.

rm -I *

 Remove every file in the working directory; prompt for confirmation if more than three files are being deleted.

rm -r mydirectory

 Remove the directory mydirectory, and any files and directories it contains. If a file or directory that rm tries to delete is write-protected, you will be prompted to make sure that you really want to delete it.

rm -rf mydirectory

 Same as the above command, but you will never be prompted; if rm can delete the files, it will.

---> Important Note: Be very careful when using "rm" this way. . .it can result in very serious consequences.. For example, if I typed "rm -rf / somedir (with a space between / and somedir), instead of rm -rf /somedir... everything would be removed in root 1st, then the somedir would attempted to be removed as the 2nd step...

- “ cp “ (copy) command:

---> Copies files and directories

---> For example:

cp is the command which makes a copy of your files or directories. For instance, let's say you have a file named picture.jpg in your working directory, and you want to make a copy of it called picture-02.jpg. You would run the command:

cp picture.jpg picture-02.jpg

...and the file will be copied. Here, picture.jpg is the source of the copy operation, and picture-02.jpg is the destination. Both files now exist in your working directory.

Another Example:

The source and destination files may also reside in different directories. For instance,

cp /home/chuck/pictures/picture.jpg /home/chuck/backup/picture.jpg

...will make a copy of the file /home/chuck/pictures/picture.jpg in the directory /home/chuck/backup. The destination file will also be named picture.jpg.
(If you are the user chuck, you can abbreviate your home directory ("/home/chuck") using a tilde ("~"). For instance,

cp ~/pictures/picture.jpg ~/backup/picture.jpg

functions the same as the above command when it is run by chuck.)

  • cp command main options:

option
cp -a / archive files
cp -f / force copy by removing the destination file if needed
cp -i / interactive – to prevent overwriting [cp –i SindoDeMayo file42]
cp -l / link files instead of copy
cp -L / follow symbolic links
cp -n / no file overwrite
cp -R / recursive copy (including hidden files) [copy entire directory : # cp –r dir42/ dir33]
cp -u / update - copy when source is newer than dest
cp -v / verbose - print informative messages

Examples:

Copying Multiple Files To A Directory

You may want to copy multiple files into another directory. To accomplish this, you can specify multiple files as the source, and a directory name as the destination.

 Let's say you are the user sally, and you have a bunch of files in the directory /home/sally/pictures/ named picture-01.jpg, picture-02.jpg... and you want to copy them into the directory /home/sally/picture-backup/. This command will do the trick:

cp ~/pictures/picture-*.jpg ~/picture-backup

Here, we use a wildcard (the asterisk, "*") to indicate that the source files are all the files in the directory /home/sally/pictures whose name starts with "picture-" and has the extension ".jpg". They will be copied into the directory /home/sally/picture-backup, assuming that directory already exists. (If it doesn't exist, cp will give you an error message, and no files will be copied.)

 You can also specify multiple source files one after the other, and cp will expect that the final argument is a directory name, and copy them all there. For instance,

cp ~/pictures/picture-01.jpg ~/pictures/picture-02.jpg ~/picture-backup

...will copy only those two files, /home/sally/picture-01.jpg and /home/sally/picture-02.jpg, into the directory /home/sally/picture-backup.

Copying Files Recursively

 You can use cp to copy entire directory structures from one place to another using the -R option to perform a recursive copy.

 Let's say you are the user steve and you have a directory, /home/steve/files, which contains many files and subdirectories. You want to copy all those files, and all the subdirectories (and the files and subdirectories they contain), to a new location, /home/steve/files-backup. You can copy all of them using the command:

cp -R ~/files ~/files-backup

...and the entire directory structure will be copied to the directory /home/steve/files-backup. When performing a recursive copy:

  • if the directory files-backup already exists, the directory files will be placed inside;
  • if files-backup does not already exist, it will be created and the contents of the files directory will be placed inside it.