Now that learning how to Work With Directories is off your checklist, we can focus on handling and working with files in Linux. So, this time, we are going to talk about moving, copying, renaming, deleting files and a host of other options.
Now, this may seem to be a pretty big thing to ask because, ‘Hey when I can easily move, copy or delete stuff just at the few clicks of the mouse! Why should I go against my convenience and avoid it?’
Well, there may be times, like when you are interacting with a server, where you can’t use a mouse and then you would find yourself struggling to operate things from the terminal.
We use the rm command in order to delete files in Linux. The format for the rm command is as follows:
rm [options] file
Be very careful while removing a file using the rm command, because there is no recycle bin from where you could retrieve the file. Once a file is removed, it is gone forever. Suppose I have a file named “Unwanted” that I wish to delete, here’s how I can do that:
In case you wish to get a prompt before removing a file, you can use the rm command with the -i option. When asked, just type in y (for Yes) or n (for No):
rm -i file
Suppose you have an entire directory which has further subdirectories or files in it, whose contents you no longer need. In order to remove the directory and its contents recursively we use the rm command with the -r option:
rm -r file
In order to forcefully remove a file without prompting for confirmation of any kind, use the rm command with the -f option:
rm -f file
Copying Files using the Terminal is an easy task, all you need to know is the cp command. The format for which is as follows:
cp [options] source_file destination
Here is an example where I have tried to copy the file “Random” from one folder to another by specifying the absolute path:
If you wish to create a copy of the same file in the same location, you can do it in this way, but remember you would have to name it something else:
cp file1 file2
It is also possible to copy multiple files from one place to another, all you have to do is this:
cp source_file1 source_file2 [source_fileN...] destination
The -i and -f options work in the same way for the cp command as they did for the rm command.
Moving and Renaming Files
Another aspect of working with files in Linux is moving and renaming files. Both these things can be done by using the same command, which is the mv command.
For renaming purposes it can be used in this manner:
mv original_filename new_filename
For moving it from one place to another, utilize the mv command in this way:
mv file1 [fileN..] destination
Here is an example where I have moved the file “Linux_Lexicon” to the Desktop from its current location:
Another wonderful command which I would like to share with you in this article of working with files in Linux is the sort command. This command comes in real handy when you wish to sort the data inside a file. The format for it is:
sort [options] file
Here is an example of a file which contained random data and has been sorted using the sort command:
You can even check out if a file is sorted or not, by using the -c (check) option. If a file is not sorted then the command returns the first occurrence with line number and the disordered value.
There are a plethora of other options that you can use along with the sort command. Check out its man page to find them out for yourself.
Well, I know that working with files in this manner might be a little irksome at times. But trust me, once you get the hang of it, you will wonder why you hadn’t been using it all along. Keep practicing and may the ‘FOSS’ be with you.
Got any doubts, queries of your own, or any suggestions on the topics that you would like us to cover? Drop them in the comments below.