How To: Delete A Swapfile In Linux

Today’s article will be a bit more advanced than some as I try to explain how to delete a swapfile in Linux. If I do my job right, it’ll be easy enough for a newbie to follow. However, I’m not going to suggest newbies mess around with removing swapfiles – but they certainly can.

It should probably be noted that some folks will call this a “swapfile” and others will add a space to call it a “swap file”. I’m not sure which is correct, so I’m sticking with “swapfile” for consistency sake. After all, about two years ago, I wrote an article about how you can enable a swapfile.

How To: Create And Enable A Swapfile

This article might be a bit complicated. The directions themselves are pretty simple. Distilling the concept of ‘swap’ down to an 800-word article is the difficult part. 

What Is A Swapfile:

What is swap? Well, it’s not really a place where the kernel sticks stuff when there’s no more free RAM. People seem to think that’s what swap is, and it’s not. Even on systems with lots of RAM, the kernel will use swap when it is available.

It’s quite a bit more complicated than that, though it can sort of be related to that. It would be prudent to suggest that you read this thread first, concentrating on the first post in the thread:

To swap or not to swap, that is the question.

I’d further suggest reading this article from the folks at linux.com:

All about Linux swap space

There’s more to it than the article goes into, but pay attention to this bit of text quoted below:

Second, a significant number of the pages used by an application during its startup phase may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other applications or even for the disk cache.

In the early years, we used a special partition for our swap. These days, there probably isn’t much need for a special partition. Most of us that still use swap will be using a swapfile. This is essentially the same thing, but it is a file instead of a special partition on your storage drive.

Your distro may have created a swapfile during the installation process. You may have created your swapfile along the way. Well, this article is going to tell you how to delete a swapfile, just in case you changed your mind.

Delete A Swapfile In Linux:

Yeah, this sort of thing pretty much requires using the terminal. I don’t know of a way to do this graphically, though there’s probably a way that I don’t know about. So, press CTRL + ALT + T to open your terminal and we’ll head straight into the article.

With your terminal now open, let’s first make sure that you have swap to begin with. To do that, you can run this command:

Technically, you don’t need the -m flag, but it’s easier to read in my opinion. The output might look something like this:

As you can see, the system has 16 GB of RAM and 2 GB of swapfile space. Of that 2 GB of swapfile, 737 MB is being used. There’s lots of free RAM, but swap is still being used. Like I said, it’s not just a place where the kernel stuffs things when there’s no more free RAM.

So, now that we’ve learned there’s a swapfile, you can run this command:

The output from that might look something like this:

The bit we’re looking for is /swapfile because that’s the literal name of the swapfile. You could have called it anything while setting it up, so it’s important to get the name correct for the next command.

Now, we want to delete your swapfile. That’s a very easy command. It looks like this command:

In my example, if I wanted to delete a swapfile on my system, the command would look exactly like this:

To confirm that the file is deleted and no longer being used, just run this command all over again:

It might seem a little complicated at first and I hope I explained it well. Using swap (or not) can be a complicated subject, but it’s not all that taxing to delete a swapfile in Linux. If you can follow the above directions, you should be good to go.

Closure:

As I’ve mentioned before, I leave swap enabled because the kernel uses it. The kernel uses swap even on systems with a lot of free RAM. I figure the kernel is smarter than I am, so I leave it enabled.

The second link in this article is well worth reading. Deciding to use swap or not is a very personal thing. If you have plenty of RAM, you can get away with not using any swap at all. Of course, if your system is fast enough and you’re using an SSD, having a swap isn’t going to add much latency to your system – if any at all.

This is something you get to decide. You can decide what is right for you and your computing needs. If you want a swapfile, you can easily add one. If you want to delete a swapfile that was created during the installation, you can do that as well. This is Linux and your configuration options are nearly endless. You decide what’s best for you. You decide how you run your Linux because it is your Linux. And that is awesome.

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.

Yet Another Way To Find Files By Extension

You might look at the title and think that you’ve seen this before, and you have, but this is another way to find files by extension. That’s right! This is Linux! There’s almost always a variety of ways to think about this. There are almost always a variety of ways to accomplish the same task!

Well, this article will touch on a theme we’ve seen before. We’ve seen it recently. I’ve even explained why you might want to find files by their extension type. Fortunately, I’ve even explained the limitations this has.

See a shell script file could have a .txt extension but still work just fine as a script. Linux cares about the file itself more than it cares about the extension. If you have files mislabeled as an .iso file, this operation will still list them as .iso files. This is only about the extension and using that as a search category. It has limitations.

You can read these articles for more:

Another Way To Locate Files By Extension
Find Multiple Filenames By Extension – With Locate
How To: Find Multiple Filenames By Extension

See? It might seem that this article is like beating a dead horse, but it’s not. There are use cases for all of these options, including the option I’ll give you in this article.

Also, it seems like a good day to take it easy. Life has sent me a bit of extra stress. That won’t stop me from writing this article. It seems nothing will stop me from writing these articles!

The ‘ls’ Command:

This is kind of the sledgehammer way to find files by their extension. It’s a good way. It’s even an easy way to remember, without needing extra commands. At the same time, it’s a pretty basic way to do so.

The first command we’ll work with is one you have installed already. You won’t need to add any software to follow along with this article. We’re simply going to use the ls command to list directory contents. 

If you check the man page for the ls command, you’ll see this:

ls – list directory contents

What did I say in the paragraph before that? I said we’re going to list directory contents. Sure enough, Linux provides the perfect tool for this. 

As an aside, what you can accomplish with a basic Linux install is amazing. You can accomplish a whole lot of work without actually adding any additional software. This is awesome. Thank you, Linux.

The ‘grep’ Command:

The second command we’ll be using is the grep command. If you’re a regular on this site, you’ll have used the grep command many times. You can think of the grep command as a filter. You use the grep command to process the output of another command (more often than not) and, more specifically, you use it to filter that output.

You want to use grep when you’re trying to isolate some information. Well, sure enough, we can check the man page for grep. If you do so, you’ll find that grep accurately describes itself like this:

grep, egrep, fgrep, rgrep – print lines that match patterns

You’ll see that grep comes in many forms. You’ll also see that it’s used to print lines that match patterns. The command is filtering out those lines that don’t match the prescribed pattern.

In fact, I used it as an example in the previous article:

Find Out When A File Was Created

Scroll down to see how you can use the grep command to filter out the lines that aren’t important when you simply want to find out when a file was created. Simple, eh? Yes… Simple!

Find Files By Extension:

As you can guess, we’ll be using both the ‘ls’ command and the ‘grep’ command to find files by extension. As those are terminal-based commands, this will be a terminal-based article. You’ll need to open a terminal and you can (more often than not) do so by pressing CTRL + ALT + T.

With your terminal now open, this is the syntax I want you to use for this exercise. We’ll keep it simple:

If you’ve used mix-case, maybe having extensions of .TXT (in capital letters), you can add the -i flag to the grep command. That will (don’t forget to check the man page) tell the grep command to ignore the case. So, an example of the syntax for that command might be:

For example, I might want to search my ~/Downloads directory for .iso files, finding the various distros I’ve downloaded on this computer. If I wanted to do that, my command would look like this:

And, sure enough, here’s an example output from that very command:

See? That output shows you all the .iso files that I have in my ~/Downloads directory. There’s no fuss. There’s no muss. It’s just a simple way to find files by extension.

Anyone can do it and they won’t need any additional software to do so. Everyone (except maybe someone with a very light embedded Linux system – and probably still them) can find files by extension without adding anything to their system. It’s maybe a bit creative, but it’s effective. I’m not sure about you, but I like effective things. This meets that definition squarely – it’s effective.

Closure:

Well, I guess this is still a fairly long article. I’ve been making them longer and this one just got written that way. My style of longer writing isn’t without thought. I have altered the format of these articles a half-dozen times.

Each time I do so, I do so for you. The goal of altering the format is to make it easier for you to digest and to offer more information than previous articles – even for the more basic articles like this one. I do not know if I’ll settle on this style. Evidence would suggest that I’ll improve upon it as I go.

I welcome feedback on this. I don’t suppose you’ll give me that feedback. The odds are good that you won’t even read this far down. Still, please let me know if you like the current writing style. I’ve consistently written to a formula, but that formula has changed over time. This is the most recent iteration, so please let me know what you think. Thanks!

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.

Compare Two Text Files Side By Side

Today’s article has a theme that may seem familiar, as I recently wrote an article about how to compare two text files side by side. That article was well received and I figured I’d share another way to compare two text files side by side. After all, this is Linux and there are usually many paths to the same destination.

You can use a variety of tools to compare files. It’s not hard to check two files to see the differences between them. However, you can visually compare them quite easily. All you need to do is to show those two files in a side-by-side manner.

The previous article can be read here:

Show Two Text Files Side By Side

So, that’s one way to show two text files side by side. This article is going to show you another way. Why? Because I can! Also, I’d like to write this article while it’s still fresh in my memory. If I don’t write it soon, I’ll have forgotten to write it at all. Age will do that to a person. It is what it is.

Preparation:

In the previous article, I had you download two files. I’m going to suggest that you do so again. That way, we’re on the same page. 

Download these two files to your ~/Documents directory:

File One
File Two

Those two files should be the same in every way. Byte for byte, they should be the same (with maybe a slight difference in metadata like last accessed time).

The tool we’ll be using is sdiff and you almost certainly have this installed by default. It’s part of the diff tools, so it is likely installed by default. You can verify that sdiff is previously installed with the following command:

 The output should show you that sdiff is just a part of the diff utilities. It should say something similar to this:

sdiff (GNU diffutils) 3.8

Those are the only tools you’ll need. Well, that and a terminal emulator of your choosing. It’ll be easy enough for anyone to follow along with this article.

Compare Two Text Files Side By Side:

As I mentioned above, you’ll need an open terminal. For most distros, you can just press CTRL + ALT + T and your default terminal emulator should open. If not, open a terminal from your application menu.

If you followed the above directions and downloaded the two files, you will first need to change your directory to your ~/Documents directory. That’s easily done with this command:

Then, you can simply compare the two files. The syntax is like this:

So, in this case, the command would look like this:

As you can see the files are the same.

using sdiff to compare two text files side by side
See? The files are the same. You can use sdiff to compare two text files side by side.

Let’s mix things up…

Change One File:

We already have the terminal open, so let’s see this sdiff in action with a changed file. Let’s edit the sort1.txt file with Nano. Run this command:

Now, scroll to the bottom and add the following line:

Then, you need to save the updated sort1.txt file. As we’re using Nano, you can do that by pressing CTRL + X, then Y, and then ENTER. It’s not hard to save a file in Nano.

Now, let’s run the sdiff command a second time so that we can compare two different text files side by side. 

You’ll see a different output. It should look like this:

comparing two different files with sdiff
You can see that one line is different when you compare the two text files.

The > character indicates that there’s a difference. Specifically, it indicates that there’s a new line that isn’t in the first text file. 

On the other hand…

comparing two files with sdiff and one has a different line
This time, the sdiff command is showing two different text files side by side.

The | character shows that the lines are different. This is a different character that shows that there’s a new line. Instead, this one is indicating that the text on that line is different than the file it is being compared with.

See? Pretty simple stuff!

Closure:

Well, this is another way to compare two text files side by side in the Linux terminal. There are so many ways to accomplish a goal in Linux and this is just another way that you can do this operation on text files. This sort of stuff would be handy in dealing with revisions or easily spotting differences in readme files. You can apply this skill to all sorts of situations. Feel free to comment telling me how you use the sdiff command.

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.

Another Way To Locate Files By Extension

I figure that today is a good day for a shorter article, so we’ll look at another way to locate files by extension. This is something that you can do in so many different ways. We’ll just cover another one, simply because we can.

You might want to locate files by extension when you’re doing file management tasks. When you have a directory containing many files, the output from the ls command can be pretty cluttered. For example, you might want only to list the files with a .txt extension. This article will help with that.

It should be noted that Linux doesn’t much care about file extensions. If you create a plain text file without any extension at all and add some text to the file, Linux will know that it’s a text file. See this article on Wikipedia about magic bytes:

List of file signatures

See also this article:

Magic Number on Wikipedia

About a year ago, I wrote a couple of articles on this same subject – that is finding files by extension. Of course, those articles are different than this article. This is a different way to locate files by extension. Those previous articles were:

Find Multiple Filenames By Extension – With Locate
How To: Find Multiple Filenames By Extension

So, finding files by their extension type has been covered before. We’re just going about it another way. The previous examples were a bit convoluted and this is just going to be an “easier” way. (I suspect that it’ll be easier. I guess that depends on what you consider to be easy!)

Locate Files By Extension:

Yes, we’ll be doing this in the terminal. I’m sure there’s a GUI way to do so, but that’s going to be a matter of the file manager you use. They will not all have the same features and not all of them will access those features in the same way. So, let’s get started by opening your terminal – just press CTRL + ALT + T and your default terminal should open.

With your terminal now open, we’ll use the find command again. You almost certainly won’t need to install anything but you can confirm that you have find installed with this command:

See? You have the find application installed already. 

If you check the man page (with man find), you’ll see find is described as:

find – search for files in a directory hierarchy

We’ll also be using grep and a pipe “ |” in this article. You should know that grep helps you sort stuff and that the pipe lets you take the output from one command and use it in another. The pipe is a very underrated tool by people who don’t know much about the terminal.

The command we’ll be using is a find command. The syntax is quite simple and would look something like this:

For example:

The -type f is telling the find command to only look for regular files. Then, the output from that command is then piped to the grep command. After that, the grep command looks for file names that contain a . and the specific extension you’re looking for. The output might look like this:

using find and grep to locate files by extension
This is a fairly new installation and most of my .iso files are stored on the network.

Now, this isn’t going to work if you just use wonky file names. This isn’t going to find files without that extension name. Remember, we’re trying to locate files by extension. We are not determining if the extension is correct and we are not determining what the file really is.

See? Pretty simple!

Closure:

Well, I said today’s article would be shorter. It’s also fairly simple. If you need to locate files by extension, there are many ways to do it. Some methods are easier than others. Perhaps some are more robust than other methods. Either way, you can now locate files by extension type. 

You might also be interested in this article:

Find Out What A File Is

Otherwise…

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.

Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.