Renaming and Removing Files and Folders at the Command Line
14 January, 2020
One of the great things about the command line is that you can do just about anything there that you can do within a graphical environment like GNOME, KDE, xfce, OpenBox, or whatever window manager you use. Sometimes, you can do it faster and more efficiently.
One of the tasks I do at the command line is renaming and deleting both files and folders. That’s often because I’ve converted or combined files and need to change their names or get rid of the ones that I don’t need any longer.
There are three commands that let me do that quickly and easily. Let’s take a look at how to use them.
Renaming Files or Folders
When you rename a file or a folder at the Linux command line you’re not just substituting one set of letters for another. You’re actually moving the file or folder to that new name.
Which is why you use the mv command. To rename a file or folder, run the command:
mv originalName newName
Say you have an audio file name yardbirdsheartfullofsoul.ogg and want to rename it Yardbirds-HeartFullofSoul.ogg. Here’s how to do it:
mv yardbirdsheartfullofsoul.ogg Yardbirds-HeartFullofSoul.ogg
But let’s be honest: underscores and bunched up words in file names are so Windows 3.1. Why not put spaces in a file name? You can do that by surrounding the new name of the file with quotation marks.
Taking the example above, let’s rename the file Yardbirds - Heart Full of Soul.ogg. Here’s how:
mv yardbirdsheartfullofsoul.ogg “Yardbirds - Heart Full of Soul.ogg”
Guess what? You can use the mv command to rename folders, too. Use the same format. And, as with file names, you can add spaces to the name of a new folder by surrounding it with quotation marks.
One more thing about renaming a folder: using mv will work, even if that folder has a number subdirectories under it.
Deleting Files
Warning: When you delete a file at the command line, it doesn’t go into your Trash folder. It's gone. So be careful!
To do that, you use the rm (remove) command. Just type:
rm fileName
So, to get rid of a file named readme.txt, type:
rm readme.txt
If you have a file with a name that contains spaces, just surround the name with quotation marks to delete it.
Deleting folders
Warning: I’m going to repeat this: use these commands with extreme caution. Once you delete a folder at the command line, it’s gone. You have been warned.
If you want to delete a folder, use the command rmdir (remove directory). Type:
rmdir folderName
Let’s say you have a folder named Docs that you no longer use. To get rid of it, type this:
rmdir Docs
And it’s gone.
Sometimes, you might see a message like this:
rmdir: failed to remove ‘Docs’: Directory not empty
That could mean that there are either files in the folder or directories under the folder. Let’s assume that you run into a folder with a number of directories under it. You can use the cd command to go into the folder and delete each file and/or subdirectory.
That’s a lot of work. Instead, you just need to go back to the rm command and type this:
rm -rf directoryName
Notice the -rf? Those are the options that help rm do its work. The r option tells rm to remove any subdirectories that it finds under the directory that you specify. The f option tells rm to delete any files it finds.
Final Thoughts
The ability to rename and delete files and folders at the command line can be very useful. It can also be very dangerous. But with a bit of care and a bit of attention, you can harness the power of another trio of helpful Linux commands.