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 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.


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.


Please enter your comment!
Please enter your name here





Free up hard disk space ubuntu
Hard disk space can easily get filled up with cached package files, old kernels, and other obsolete files that occupy unwanted hard disk space. Here are top five best and safer ways to clean and free up hard disk space in Ubuntu. We show you Terminal and GUI way of cleaning up system.
Uninstall Programs in Ubuntu
Uninstallation of programs can be done by graphical way using the Ubuntu Software Center, and the Synaptic Package manager. Command-line way of doing it is also possible using apt-get and aptitude commands. We shall discuss each one of them in detail.
Application Launchers for Ubuntu
Ubuntu is one of the most used Linux distributions worldwide. It is also the reason why it has the maximum number of available programs for itself. Today we are going to talk about one category of those programs, the application launchers.
best linux distros beginners
Making the shift to Linux is not as complicated as some people make it out to be. There is a variety of Linux distributions available that cater to the needs of people who are planning on making the jump to Linux from any other operating system.
best music players for linux
For sure, there will be Linux users out there who cannot live without music. If you're one of those people, you would know how important it is to have a good music player installed on your computer system. In this article, we present to you the 5 Best Music Players for Linux, which will hopefully be able to satisfy each of our readers' needs.