Delete, Copy, Move, and Rename Files using command-line in Linux

Learn how to delete, copy, move, and rename files using command-line in the Linux Terminal. These work on any Linux distro.

If you’re using a Linux operating system, it’s beneficial to learn the Linux Terminal commands as well. It may seem a bit scary at first, but once you get proper guidance and a bit of practice, you get the hang of it. It’s more efficient and faster most of the times.

We’re going to explain today how to delete, copy, move, and rename files using the CLI. We’re going to assume that the readers are familiar with ls and cd commands. If you want to know about the most basic commands of the Linux CLI, visit this link.

Deleting files

Let us start this off by deleting the files!

Deleting files using the Terminal is simple, and generally wrapped up by one command: rm. You can expand rm as remove. In the CLI mode, the only attribute that the rm command requires is the name of the file. For example, if I need to delete a file named ‘test’ which is in my working directory, I have to write:

rm test

And that’s it. Now, some useful options that rm has.

rm -r

First one, the ‘-r’ option. The rm command doesn’t remove any directories that have files in them. The ‘r’ in any basic Linux command (generally) stands for recursive. What that means is that the command will act on any sub-directories or the files contained by a directory, assuming that you run the command on the directory.

So if you require to delete any directory, you have to use the ‘rm -r‘ option. For example, if I have a file test inside the directory testdir, a plain rm command won’t remove it, but with the -r option, it will.

rm -r test.
Deleting directory

rm -f

Next, the -f option. It is the force delete option. It means that the command line will never prompt you about any errors while deleting the files. So, all added up, if you want to delete some files, write the name of the files or directories after rm -rf in the terminal, and they will be deleted.

By default, when a file is created, the user who created it, and the user group of the user have permissions to read and write the file. Others, just to read. For the demonstration of rm -rf, I have created a file named test2 in the same testdir, and changed the permissions, so that no one has the permission to do anything to it.

It is the image for evidence, try to decipher the displayed permissions.

No permissions file.
No permissions file.

Now we delete it:

rm -rf
Force deleting files

Copying files

Next up, copying files. The command used is cp. The cp command requires two attributes. One, the location of the file that you want to copy, and next, the location where you want to copy. Now here, I will copy a file named test3 to the directory testdir.

cp simple command
Simple file copying.

As seen in the output, the file has been copied, and the ls command proves that it’s in the testdir directory. Now again an important option:

cp -r

Yes, the function of the -r is the same here. It copies the files in a directory recursively. If you use plain cp to copy a directory that has files, the cp command will simply omit the directory and move on. So, a -r option is necessary to copy a folder.

cp -r test
Copying directory.

cp -i

The next option is -i. The -i option is called the interactive mode. If you use this option, the CLI will prompt you before copying every file to the destination. If you want to copy, you enter ‘yes’ or just press enter. If you don’t want to copy, enter ‘no’.

For example, here I’ll copy all files in the working directory to the previous directory.

cp -i
Interactive copying

I have used the asterisk(*), which means everything. It is explained in more detail at the end of the article. The ‘..’ means the previous directory. In Linux CLI, the working directory is represented by ‘.’ and the previous directory by ‘..’

Moving and renaming files

Moving and renaming are in the same category because the Linux CLI uses the same command for both of them.

Moving

Moving files is similar to copying files. You need to use the command, then the location of the file you want to move, and the destination. The command, in this case, is mv, which naturally means move. But a piece of good news here. The mv command does not require the -r option even to move directories. Here’s an example of moving testdir2 to testdir.

mv command
Moving files

As seen in the image, the testdir2 directory isn’t in the home directory anymore and is in the testdir directory.

Renaming

For renaming, as you know, the mv command is used as well. So if you notice, Linux doesn’t rename the same file, but moves the file to the same directory again, but with a new name. So it is necessarily the same contents of the file, but the file is new, with a new name.

Here’s an example of the mv command to rename. I will rename the file test as tested.

mv test
Renaming files

So as you can see, the file previously with the name test has now become tested (pun intended).

Terminal Trick

Now, this is a simple trick that can be used to make your tasks much easier and faster. The usage of the asterisk(*). In the Linux CLI, the asterisk means everything. Literally. So, if you want to remove all the files in the working directory, what do you do?

rm -rf *

You are done. All files deleted. It also comes in handy with other commands like cp, mv etc., where you want to apply the command on all files in the directory.

It can be used in another way, to operate on all files using a certain keyword. For example, I will copy all files that have the word ‘test‘ in their names, to the dir directory. The command is:

cp *test* dir

It means copy everything*test*everything to dir.

Usage of *
Using * to copy files.

So, that will be all for this article. Keep checking Terminal Tuts for more such tutorials. Cheers!

Pulkit Chandak
Pulkit Chandak is a Linux enthusiast and has been using and experimenting with open source software, and hardware too since a long time. He is a huge admirer of open source software and wants to ventilate it to all around him. He is interested in reviewing and writing tutorials on Linux and its innumerable distributions. He believes that freedom in software leads to freedom of the mind from the chains of limits.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

24,936FansLike
151FollowersFollow

LATEST ARTICLES

RECENT COMMENTS