Friday 21 July 2017

How to move, copy and delete a file or, folder on linux

Sometimes you don't remember linux command for these basic operations copy, move, delete a file and folder. There are three commands for each operation,

  1. cp: Copying Files. A basic example of the cp command to copy files (keep the original file and make a duplicate of it) .
  2. mv: Moving (and Renaming) Files. The mv command lets you move a file from one directory location to another.
  3. rm: Deleting Files.
the Linux command line offers far greater power and efficiency than the GUI. For instance, to instantly seek out and move all of the files above to a subdirectory called budget, your command line instruction would simply be:
Each of the Linux commands to move, copy, or delete files have options to make it more productive. Read on to find out more.

1. cp: Copying Files

A basic example of the cp command to copy files (keep the original file and make a duplicate of it) might look like:
In this example, we copy the joe_expenses file to the cashflow directory, which (because we haven’t specified anything else) is in our login directory.

Additional Options

Options are similar to those for the mv command:
-i  for interactive, asks you to confirm if an existing file (perhaps a version of joe_expenses already exists in the cashflow directory) should be over written in the copying process.
-r for recursive, to copy all the subdirectories and files in a given directory and preserve the tree structure.
-v for verbose, shows files being copied one by one. For example:
1
cp joe_expenses cath expenses cashflow

2. mv: Moving (and Renaming) Files

The mv command lets you move a file from one directory location to another. It also lets you rename a file (there is no separate rename command).
Let’s start with the basic format:
In this case, if JOE1_expenses does not exist, it will be created with the exact content of joe_expenses, and joe_expenses will disappear.
If JOE1_expenses already exists, its content will be replaced with that of joe_expenses (and joe_expenses will still disappear).

Additional Options

Options for mv include:
-i for interactive, asks you to confirm if an existing file should be over written.
-f for force, overrides all interactivity and executes the mv instruction without returning any prompts. (You must be sure your instruction is exactly what you want if you decide to apply the -f option.)
-v for verbose, to show the files being moved one by one

3. rm: Deleting Files

File deletion is done using the rm (remove) command.
This will delete the joe_expenses file forever (maybe Joe would like that!).

Additional Options

The rm command options include -i (interactive), -f (force), -v (verbose), and -r (recursive).
Like the commands above, it can also be applied to more than one file at a time.
This will remove both of these files.
Using the wildcard character: “*”
This will remove joe_expenses, cath_expenses, mike_expenses, and robin_expenses, forever.
Likewise, if you decide you want to remove everything you copied into the cashflow directory above and the directory itself, use:

 Use Caution with These Commands

For each of these commands, the use of the -i (interactive) option is highly recommended, at least in the beginning. This gives you a second chance to spot any unfortunate mistakes.
Similarly, use caution if you apply either -f (force) or -r (recursive), especially if you are also using a wildcard character like “*” to apply the command to several files at once.

Beware of the -r Option!

We’ll say it once and once only. Don’t do this:
This will delete every file and every directory you have.

No comments:

Post a Comment