Let’s Make The rm Command Even Safer

Today’s article is one I don’t expect most folks to follow, but it’ll be a fun way to make the rm command even safer. If this article sounds familiar, then you remember a recent article. Well, this article takes it a step further and we’re making the rm command even safer!

See, in the last article you learned how to:

Let’s Make The rm Command A Little Safer

Despite the naysayers not seeing the value, that article will help you make the rm command a little safer. It adds a sanity check. If you followed the directions, you’d get the chance to tell the rm command to ‘never mind’ and to ignore your command prompt. This can stop you from removing files accidentally.

While I wrote that article, I already had this one planned, but didn’t want to publish the two back-to-back. I try to mix things up a little.

NOTE: If you followed the directions in the first article and decide you’d rather go this route, you’ll have to undo the actions you took. These two things can not work together at the same time. It’s a one or the other kind of thing.

What will we be doing in this article?

Well, simply put, we’re going to use an alias like we did in the previous article about making the rm command safer – except we’re also going to define a function and alias the rm command to that function instead.

What will that function do? Well, in short, it will take the results of the rm command and stick the files in your trash (recycle) bin. It makes the rm command work similarly to your regular delete (depending on the distro). Instead of deleting files, it happily sends them to the trash bin instead. So, if you screw up you can restore the files nice and easily with your GUI (by going into the trash bin and restoring the files, of course).

This is a bit more complicated. That’s why I went with the previous rm command modifications earlier. This still isn’t all that complicated. I realize this is something new to most of my readers, so I’ll make it as clear as humanly possible. (Wish me luck!)

In the previous article, we made the rm command a little safer. This time around, we’re going to try to …

Make The rm Command Even Safer:

Yes, you’ll need an open terminal. You can likely press CTRL + ALT + T and your default terminal should open. Otherwise, open it from your application menu.

These directions will assume you’re using Nano. If you are not using Nano, please adjust the directions to suit your text editor of choice.

With your terminal open, I want you to enter the following command:

Then, and this may be confusing, I want you to copy and paste all of the following text at the very bottom of that file:

Next, you need to save the file. As we’re using Nano, you save the file by pressing CTRL + X, then Y, and then ENTER.

Next, you need to tell your system to reload the ~/.bashrc file. You can log out and back in, you can close all the terminals and open a new one, or you can simply type the following:

NOTE: In the second code block you’ll see ~/.local/share/Trash/files. This should be the correct path to your trash bin. You should verify this and change the path accordingly. This directory ONLY exists if you’ve previously moved something to the trash. You’ll need to create a file and move it to the trash or you’ll have to make the directory manually.

Testing This New rm Command:

Leave your terminal open and open your GUI file editor. With your GUI file editor open, navigate to ~/.local/share/Trash/files.  You may not see it by default, so change your GUI file manager’s options to show hidden files and directories. (You can often use CTRL + H to show hidden files.)

Now that you’ve navigated to the directory with your GUI file manager, return to your terminal emulator. Once there, type the following:

You can then run ls to ensure the file foo.txt exists. Next, you’re going to delete it with the rm command:

Again, you can then run ls to ensure the foo.txt file has disappeared. It should certainly be gone from the directory you were in and removed by the rm command.

Except, it wasn’t!

Go back to your GUI file manager and (you might need to refresh it, depending on the file manager) check the list of files. Sure enough, you should see a foo.txt in the trash bin. It’ll remain there until you restore it or until you empty the trash.

Pretty neat, huh? 

Closure:

The first article made the rm command safer. This article will help you make the rm command even safer! You can’t do both (easily), but you can do one or the other. I mean, I’m sure it’s possible but I’ve never thought of a way to do so. I haven’t tried to think of a way to do so. So, there’s that…

If you follow this article, your rm command will send the files deleted by rm to the trash bin. If you follow the first article, you get the chance to view your command’s outcome and decide to back out of the file removal process. You can pick whichever one works best for you.

You can also completely ignore these things and just keep on doing what you have been doing. This being Linux, you can make all sorts of choices – including this one. Though, I think it was Rush who said, “If you choose not to decide you still have made a choice.”

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Manage Files In A tar Archive

Today we’ll have a fairly simple article, albeit a bit archaic, as we learn how to manage files in a tar archive. We often work with modern compression methods but tar files still exist and are in wide use. If this is something you’re interested in, this is the article for you. If this isn’t something you need right now, remember this article and look it up later when you do need to manage files in a tar archive.

Sure, tar now often ends with .gz or .bz, but they still exist. You’ll still see them and you’ll still get regular questions about them. In fact, a lot of folks will ask questions like, “How do I install files from a .tar file?” Alas, that’s a pretty broad question and not something we’ll answer here. However, the answer is, “Manually.” That’s how you install files from a tar file, generally speaking.

What Are tar Files?

So, I’m pretty sure we’ve covered this before, but ‘tar’ stands for ‘tape archive’ and is a pretty old standard. Yes, I am well aware that tape archives still exist. There’s no reason to leave a comment telling me that. I know, but I suspect most tar files never see a tape throughout their entire lives. If you’re curious, the man page describes the tar application as this:

tar – an archiving utility

The format has been around since 1979, though it has undergone some changes along the way. If you’re interested, there’s an excellent Wikipedia article about tar files. It’s well worth skimming it, as I did when I wrote an earlier article.

Basically, tar is a way to organize multiple files into a single file. Tar files are not compressed in and of themselves. We more often see them compressed with .gz (or the like) but the format doesn’t include compression of any kind. It is also meant to work with any file system, as it contains no real file system in and of itself.

Well, eventually you may want to work with tar files. You may want to add, remove, or modify tar files. That’s all easily done within the Linux terminal and this article will show you how.

Manage Files In A tar Archive:

Obviously, you need a terminal for this application. By now, regular readers will know how to open a terminal. In most distros and desktops, you can just press CTRL + ALT + T and, with that, the default terminal will pop open.

With your terminal now open, let’s go ahead and create a new tar file to mess around with…

Now, we’re all set for this exercise…

Add Files To A tar File:

So, it’s easy enough to add files to a tar file. The syntax to do so is quite simple, though you do need a few flags.

Of course, you can add multiple files by just adding their file name. Using the files you created in the first step, you’d add files to a tar file like so:

To avoid any confusion later, we’re now going to clean up after ourselves, leaving just the foo.tar file.

Now, in your terminal you can type the following:

In the list of files, you should now only have the foo.tar file remaining. For neatness sake, this is an important step. You can list the files in a tar file with the following command:

The output should look like this:

See?

Remove A File From A tar File:

Now that you have created a tar file and added files to it, you next need to learn how to remove a file from a tar file. That’s actually a very simple process. The syntax is as follows:

So, using our current example file, we’d do something like this:

You can check again to see that the foo3 file no longer exists.

Still with me?

Well then, the next logical step in this process would be learning how to…

Change Files In A tar File:

If a file already exists in a tar file, you will first need to extract that file. You can do so easily. Use the tar -tvf <file_name>.tar to find the specific file’s name. With the name of that file, you can extract it with this command:

So, using the example file that we’ve worked through, we’d change the file called foo2 by first extracting it, like so:

That will extract the foo2 file from the archive. You can then edit that file, say with nano, to make it say what you want it to say. Feel free to do so, but you must remember to save the file after you’re done editing the file. (To save files in nano, press CTRL + X, then Y, and then ENTER.)

Now, you just replace the file with the file you’ve edited. In this case, it’d be foo2 that you’ve edited. The syntax is pretty easy:

So, in our case, you’d be replacing foo2 with the previous foo2 file. That’s a nice and simple command. To finish the exercise, you can run this command:

Be sure to check the man page to learn what the flags do and to see the many other options that come with your tar application. It should absolutely be installed by default, on even the most bare of distros.

Closure:

So, well, I figured this would be a fun article to write. I was not wrong. If you want, you can clean up after yourself with this command:

I figured it’d be a fun article for folks to follow and wrote it in a way that I hope facilitates that. There’s nothing all that complicated about it. If you want to manage files in a tar archive, this is a good way to do it. You could probably do it with a GUI, but this way is just as good – I think.

Also, this was the article I wanted to write last time. You got a meta article because of my poor internet connection. This was the article I’d hoped to write. It’s quite a bit different than most of my articles, but hopefully, it takes you clearly from the start to the finish – including cleaning up after yourself!

As always…

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Find And Remove Duplicate Files With fdupes

Today’s article has you cleaning up your storage space as you learn to find and remove duplicate files with fdupes. This isn’t something you need to do often and it’s something that could theoretically break your system. If you’re going to remove duplicate files, it’s a good idea to exercise some caution.

I’ve previously shared another way to remove duplicate files:

Find And Remove Duplicate Files With rdfind

I suppose it’s pretty obvious as to why one might want to remove duplicate files. You do so to keep your storage tidy and you do so to make space when space is limited. There are all sorts of ways to make free space and removing duplicate files is just one of them.

The tool we’ll be using this time around is known as ‘fdupes’ and the man page describes it like this:

fdupes – finds duplicate files in a given set of directories

It’s an easy enough application to install and this article shouldn’t be all that long. You’ll find that fdupes is available in your default repositories, or there’s a good chance that it is in there. Of course, this means it’s easy enough to install.

Installing fdupes:

You can install fdupes with a GUI and your software manager, but you can just as easily install it via the terminal. We’ll cover the latter, as it’s the most universal (and, I find, quickest) method. Of course, you’ll need an open terminal. In most cases you can just press CTRL + ALT + T and your default terminal should open.

With your terminal now open, let’s go ahead and install fdupes:

Debian/Ubuntu/derivatives:

SUSE/OpenSUSE/derivatives:

RHEL/Fedora/Rocky/derivatives:

Arch/Manjaro/derivatives:

Gentoo/Calculate/derivatives:

And More! (Just search your default repositories for ‘fdupes’ and it’ll almost certainly be there.)

As you can see, you’ll find that fdupes is available for pretty much every Linux system out there. Not only is it available, it’s already packaged for you and easy enough to install via the terminal. On top of that, it doesn’t take much space to install fdupes, a mere 110 kB or so.

NOTE: I do not have anything against GUI tools. While I have terminals open at all times, I do the majority of my computer interaction in a browser – specifically a GUI browser. I suggest and write about the terminal because it’s more universal. It’s also often faster, assuming you can type or at least cut and paste than it is to go mucking about with a GUI software installer.

Anyhow, it’s nice and easy to remove duplicate files with fdupes. This article is going to show you how – and the article shouldn’t even be that long! It’s pretty simple.

Remove Duplicate Files With fdupes:

I’m going to assume that you left your terminal open after installing fdupes. If you didn’t, you’ll need to open it again. The only way to run the fdupes application is in the terminal. So, even if you installed it with a GUI, it’s a CLI tool and you’ll need the command line to use it.

The basic syntax is pretty easy, and not entirely unlike rdfind. For example, if you want to find duplicate files, you simply run this command:

So, if you wanted to find duplicates in your home directory, you’d run this:

Don’t worry, you’re safe running that command. That won’t delete anything at all. That fdupes command will simply show you the duplicate files that it found.

If you want to run the fdupes command recursively, that is to check all the folders within the directory, you’d run the command like this:

If you want to calculate the size of the files that would be removed when removing the duplicates, the command is just this:

A summary is also available with this command:

You can also search multiple directories for duplicates. That’d be something like this:

Of course, you can run those commands together to get quite a bit of customization. They’re all reasonably harmless and will simply point out the duplicates as well as some meta information. You can then remove the duplicates by hand if you want.

You can also tell fdupes to remove the duplicates that it found. You’d never want to run this command without knowing what exactly is going to be removed, so don’t do that. Always check to make sure you’re not removing anything of value before automatically removing duplicates.

Fortunately, there’s a bit of a stopgap. You can run the following command and fdupes will ask for confirmation before removing the file:

If you want to go whole-hog and remove every duplicate found, and remove the files without any confirmation, you can run this command:

Of course, that’s just the basics. If you want to know more about fdupes, simply check the man page (man fdupes) for more information.

Closure:

Hmm… I think I need to clean or replace this keyboard. The colon key is sticking on me. It’s a bit of a pain in the butt.

Anyhow, if you’ve ever wanted to remove duplicate files with fdupes, you now have directions to do so. This being Linux, you have all sorts of options when it comes to removing duplicate files, though I again urge caution when doing so. If you were to run this on the root directory, you’d likely find a lot of duplicates, and removing them might break your system. So, be careful with these tools, as they’re pretty powerful.

I’m not yet out of ideas for articles but it’d be great if folks might suggest something they’d like to read about. You never know, it might be something I know about. As it is, I have to search this site before writing an article, or else I’d end up with even more duplicate articles. I don’t want that and you don’t want that. It’s not all that easy to keep up this pace, writing a new article every other day. I’ve managed so far, but I’m eventually going to miss a day or two. It’s going to happen.

As always…

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Let’s Count The Number Of Files In A Directory

Today’s article probably isn’t all that useful unless, of course, you want to count the number of files in a directory. This can be useful if you want to quickly see if all the files were copied over or the like. Maybe you’ve made a backup and want to ensure the number of files matches. Who knows? It’s your terminal, you do what you want!

This article comes from my notes. These notes were right next to the notes used for the last article, where we learned to count letters, words, and lines in a file. I did get some feedback about why some folks want to know that information, which is always good feedback as far as I’m concerned. It’s great to learn the ways you put these commands to work.

Seeing as I’m lazy and just using the next notes in line, we’ll be using the ‘wc’ command again. If you didn’t read yesterday’s article, or got here by way of a search engine result, then the wc command describes itself like this:

wc – print newline, word, and byte counts for each file

You can expect the wc command to be included in every distro. It is a fundamental application and small enough to be a default application. Even the tiniest desktop distros will likely have this application and you won’t need to install anything. You will need an open terminal.

Count The Number Of Files In A Directory:

As I said in the intro, you’re going to need to use the terminal. Regular readers will notice a trend. We use the terminal a great deal. Just press CTRL + ALT + T and you should be good to go. (Some distros don’t do that as a standard. I’m looking at you, Elementary OS.)

With your terminal now open, navigate to your favorite directory with:

For example:

Then, when you’re there, try the following command:

You can also specify the path, just like you would with the regular ls command. That’d be something like:

If you want to count all the files, including the hidden files, the command would be quite similar:

In case any of this is unclear, I’ve made an image showing how I can count the number of files in a directory with a few similar commands. It looks like this:

using the ls and wc commands together to count the number of files in a directory
See? If you want to count the number of files in a directory, you can do that!

As always, be sure to check the man page for both of these commands:

And:

So, have fun counting the number of files in a directory – while learning a bit about ls, wc, and the almighty pipe (which I’ve not yet written about in any great detail).

Closure:

Of course, I hope you have fun with this one. I’m not sure how many creative ways you can use this set of commands, but it’s nice to use the pipe in an article, even if it’s just one where we count the number of files in a directory. We did use it in a fun article about cowsay and fortune. It has also been used in a few other articles, but we should have an article all about it.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Let’s Make A Directory Tree

Today we’re going to install a nifty application and then we’re going to make ourselves a directory tree. We’ll learn a couple of other things along the way, as we often do. Linux is great like that, and so we’ll have some fun along the way – including generating an entirely pointless text file that takes up too much space and takes too much time! That sounds fun to me!

So, what is a directory tree? Well, you start in one directory and then make a “tree” of all the directories below it. The directory you start in will be the top of the tree, and the directories below will be the branches. In its simplest form, it might look a little like this:

using the tree command to make a directory tree
As you can see, that’s part of the tree – with various directories and sub-directories.

You can’t see the parent directory, but there is one. That’s just a piece of the tree, which should be enough to explain this tree concept.

The tool we’ll be using is known as ‘tree’, of course. The man page describes it as being:

tree – list contents of directories in a tree-like format.

You may not have ‘tree’ installed and you will need to install it. It should probably be a default tool, but it is not. Use your package manager, search for ‘tree’, and install it. In Ubuntu/Debian/Derivatives, you can try:

That should get you started and, with tree now installed, we can…

Make A Directory Tree:

If you had your terminal open to install ‘tree’, you might as well leave it open. You’ll need it for the rest of this article. If your terminal is not open,  you’ll need to open it. Just press CTRL + ALT + T. Tada!

Now, the first thing we’re going to do is right there in your home directory, simply make a directory tree with the following command:

That will make a lovely tree. The -d flag means that it will only show directories in the output. That’s what we wanted, a directory tree. However…

You can make a tree with the files included. Just drop the -d flag and try this:

Now, let’s send that output to a file. If you want a tree (with or without files listed in it) as a saved text file, you can try this command:

Now, for some fun, you can try this (it will take a long time):

You need ‘sudo’ for that command so that it can traverse and list the various directories for which you have no permissions. It will take a long time. I want to say that it took about 30 minutes, but I currently have a bunch of external stuff hooked up to this device.

It will also generate a giant text file. Mine was over 500 MB in size. Opening it is a slow and painful process, but it’s not too bad once it is open. There are other options for the tree command, just type man tree to learn more about the tree command.

Closure:

So, now you know how to create a directory tree. You also know how to make your directory tree include files and how to make a directory tree of your entire computer’s file system. I don’t know why you’d need to know this, but now you do. They do make good text documents for small sections of the tree, especially if you need to quickly see the directory structure for some other task.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.