Join 94,137 users already on

The Linux operating system - working with files

2 12
Avatar for valo
Written by   29
7 months ago

Having introduced how to change directories and list the files, it's time to move on to manipulating them. This includes copying, moving and deleting.

(Lead image source: Unsplash)

Copying files and directories

The command is unsurprisingly cp. It works by giving the name of the file that you want to copy followed by the name of the new file, e.g.,

cp my_file copied_file

The contents will be identical, as expected. That looks nice but... look what happens when I try to move a directory:

cp my_directory copied_directory
cp: omitting directory ‘my_directory/’

So... then I can't copy directories? What do I do then?

Of course we can do that. We just need to tell the cp command to enter the directory and copy all the files and any subdrectories that it contains. This is done by specifying the parameter to tell it to copy recursively, or

cp -r my_directory copied_directory

I can then use the ls command to prove that I have copied it.

A screenshot of the whole example above in my terminal.

Moving files and directories

Similarly, files can be moved from one place to another. Also, a file can be renamed by using the move command to a new name. The command is simply mv. Moving directories does not require the parameter -r. For example,

mv copied_file moved_file
mv copied_directory moved_directory

I can also move a file to a directory:

A screenshot showing the above commands to move files in my terminal.

Deleting files and directories

If you want to remove a file, you need to use the rm command. I previously moved the file called moved_file to my_directory. Now I want to remove this file. I need to type

rm moved_file

and when I list the files, it is gone.

Wait... but this is prone to mistakes! Windows always asks if you want to delete something. And usually, it moves it to the recycle bin. I'm going to lose my precious data by simple mistakes!

There is a solution to that. One needs to add the parameter -i, meaning interactive:

rm -i file1
rm: remove regular file 'file1'?

When I type n and press enter, it will keep my file. And, of course, if I indeed want to delete it, I need to type y.

Moving directories requires the recursive flag:

rm moved_directory/
rm: cannot remove 'moved_directory/': Is a directory

rm -r moved_directory/

And I can also add the -i flag to make it ask me for a confirmation.

A screenshot of the example about removing files and directories in my terminal.

One can redefine commands, so that typing rm includes also the interactive parameter using the alias command, however, I will dedicate a separate article on that. But next time we can start editing files. There is plenty to say about that matter and as you will find out, choosing the editor is a heated topic among Linux users. And everybody thinks that they're right.

$ 0.70
$ 0.65 from @TheRandomRewarder
$ 0.05 from @mc5punk
Sponsors of valo
Avatar for valo
Written by   29
7 months ago
Enjoyed this article?  Earn Bitcoin Cash by sharing it! Explain
...and you will also help the author collect more tips.


Many times I tried to learn and use the terminal to put these commands into practice but in the end I always got carried away by the simplicity of the graphical mode. I will be looking forward to your new publications. Regards.

$ 0.00
7 months ago

Thank you so much! Once you master them, you'll start using them more often than the GUI.

$ 0.00
7 months ago