List The Files In A Directory

Today’s article is only going to be useful if you are a new user and don’t yet know how to list files in a directory. This is, of course, something you do in a terminal. I do believe this will be a quick and easy article, so read on if you don’t yet know how to list files in a directory.

If you’ve used Linux for more than a month, you can safely ignore this article.

Yes, this has been covered all over the web. This isn’t anything remotely new, nor is it anything all that complicated. However, this is a holiday weekend and I figure it’s a good article for new users who happen to be just browsing around. I’ve also covered some ‘ls’ commands before.

On the other hand, it should be a short article! So, there’s that!

List The Files In A Directory:

Of course, this article requires an open terminal, like many other articles on this site. Should you not know how to open the terminal, you can do so with your keyboard. Press CTRL + ALT + T and your default terminal should open.

With that terminal now open, let’s try a simple command:

That will list all the files in the directory. If you’d like to see the output in columns, you can always try this command which lists the output in a denser manner.

That command doesn’t list dot (hidden) files. Seeing as you’re in the home directory, you should have some hidden files. If you want to use the ‘ls’ command to show files, you need the -a flag. You can combine it with the above and use this command:

That’s the command I use more often than not. This being Linux, you get to decide which how to use the command. I think I type the ‘ls -la‘ command out of habit.

Closure:

Well, you got an article. Sure, it’s a simple article but someone may find this important information. Of course, they could just use the man page   – but what’s the fun in that?

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.

How To: Prevent A File From Being Deleted*

Today, we have another simple article, where we discuss how to prevent a file from being deleted. It’s not quite true and I’ll explain in a moment, but for the most part, it makes it so you can’t remove a file by accident or the like.  So, if you want to prevent a file from being deleted* read on.

Now, let me be more clear. I’m a bit sorry for the cheesy headline, but there’s a reasonable character limit. I included an asterisk just to make sure. A user with root permissions (eg a user with sudo privileges) can still delete the file – but it takes an extra step to do so. They have to know and work at it to delete the file.

The tool we’re going to use is called chattr. While it looks like ‘chatter’ and I want to type ‘chatter’ every time I use it, it is a tool that you use to change a file’s attributes. It’s a handy tool but we’ll only be examining one specific use for chatter. If you’re curious, the man page defines chatter as:

chattr – change file attributes on a Linux file system

If you’re using a mainstream distro, you shouldn’t need to install chattr. You should find it already installed. Some of the more lightweight distros may not include it by default, but you can verify that it’s installed by running the following in your terminal:

If you do find that chattr isn’t installed, go ahead and install it. It’s in your default repos. I’m sure of it!

Prevent A File From Being Deleted:

You guessed it. You’ll need an open terminal for this one. That’s easy, just press CTRL + ALT + T and your default terminal should open. See? Pretty easy!

Now, I’m going to show you how to use chattr to set an immutable bit. It’s not difficult and just uses the +i flag. Let’s start with creating a file:

With the file created, let’s set the immutable bit with chattr:

Now, let’s try to remove it with the rm command:

No luck? Well, let’s grab a hammer. We’ll try to remove the file with sudo:

Surprise! You still can’t delete it, even with sudo or logging in as root. To delete the file, you have to first remove the immutable bit, like so:

After that, you can happily (and trivially) remove the file with:

In case that’s not very clear, I’ve made you an image. That should help!

prevent a file from being deleted
See? If the words aren’t useful, perhaps the picture will explain it better.

That should explain it well enough. The file can still be deleted, it just requires sudo and removing the immutable bit from the file’s attributes. Pretty neat, huh?

Closure:

Of course, there’s always a way for a root user to be able to remove a file. However, you can make it difficult should you want to prevent a file from being deleted. A skilled user will check the file’s attributes and know how to delete the file, so it’s more about protecting a file from being deleted unless you want to.

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.

How To: List Open Files

Today’s article might seem unimportant and that’s okay because we’re just going to examine one way to list often files. So, in a way, it’s probably not all that important. If you are someone who wants to list open files, this would be the article for you!

For the most part, most of my readers aren’t going to want to know how to list open files because they just have no good reason to do so. It won’t be a very long article or anything. I did some long-format articles and they didn’t get much feedback. I’ll do more, but I don’t see them as needing to be all that immediate – or consistent. The shorter articles just seem to be what folks are looking for. I can live with that. (If you do like the longer articles, it’s not too late to mention that.)

Today’s tool will be one you may have never heard of. We’ll be using the right tool for the job, however. That tool will be the ‘lsof’ command. The ‘lsof’ command should be installed by default in any major distro and checking the man page will reveal that it describes itself like so:

lsof – list open files

Read that line and then look at the headline. See? I told you that it was the correct tool for the job. If you want to list open files, a tool that lists open files is the tool for the job. Imagine that?!?

List Open Files:

Of course, the lsof command is run in the terminal. As such, you’ll need an open terminal for this exercise. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

With your terminal open, you can just run the following command which will show every currently open file (it’s a lot):

You’ll notice some of the output will say something about “permission denied”. So, if you’re going to list all the open files, you probably should use sudo with the command. Like so:

Type in your password at the prompt and be prepared for the deluge of file names. It takes quite a bunch of open files for an operating system to work – and Linux treats everything like it’s a file.

So, what if you just want the output to tell you all the files opened by a specific user? The lsof command will help you out with that. Try this command:

For example:

That command will show you all the files opened by that user. It’s still a lot, but it’s an easier output to process. If you wanted to show the files opened by everyone except a specific user, you’d use this command:

That command would (assuming you removed the brackets) show you all the open files except those files opened by ‘username’. In any of these cases, if you see some permission denied errors you can just slap sudo on the front of it and be good to go.

Closure:

Well… If you’ve ever wanted to list open files you now know how to do so. I suppose it’d mostly be useful for things like auditing or troubleshooting performance issues. I have the command in my book of tricks, but I don’t recall ever needing to use it for much. I’ve played with lsof and it does list open files. So, there’s that.

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.

How To: Unzip All The Files In A Directory

Today’s article might seem like a strange one, but it’s all about how to unzip all the files in a directory. It’s something only a few folks will need to know and something you may not need all that often. However, those folks who need to know this, and those folks who need this often, will be grateful to learn this little tip.

I would think that this will be a short article, as I don’t see it taking up that many words. I’m not sure of the appeal, but I want to write it anyhow. So, if you want to know how to unzip all the files in a directory, this is the article for you!

For me, this is another article based on necessity. See, it’s I who frequently has to unzip a bunch of files all at once. They’re fortunately all in the same directory, making this just a pair of easy commands. Sometimes I have to unzip nested files, so I’ll include the command to do that along with the more simple command that I usually run.

Every month, I get a whole lot of documents from my accountant. For bandwidth’s sake, these get organized and compressed before they’re emailed to me. In theory, I’m supposed to go over them and verify them individually. At best, I open them all up and look at a few of them. I do a sanity check every few months. My accountant is an old lady and I’m pretty confident in her integrity. So, I’m pretty lax…

However, see, I’m supposed to check them… So, I kinda go through the motions and at least look at a few of the documents. That’s where this command comes in…

Unzip All The Files In A Directory:

For those following along, it has been a minute since you needed to open a terminal to follow one of these articles! Well, that wait is over! You know what to do. Otherwise, just press CTRL + ALT + T, and your default terminal should open.

With your terminal open, use the cd command to navigate to the directory that contains multiple .zip files. You should know how to do this, but it’s just: 

Once you have reached the directory you’ve chosen, you can just run this command to unzip all the files in a directory:

Now, sometimes there will be folders inside that directory and those folders will also contain .zip files. To unzip the files recursively you’d just want to use the following find command first, like so:

That lovely command will scour the directory you’re in for sub-directories, enter those directories, and then unzip any files it finds in those sub-directories. It’s not all that complicated, you’re just finding files with .zip in their name and then executing the unzip command on them.

As I said at the start, it’s not one of those things you’ll need often – but when you do need it, you need it. Well, your life is significantly improved by it. I suppose it’s not technically a necessity. You could unzip all the files one by one or even unzip them using a GUI. Still, it’s handy to have.

Closure:

And there you have it. Today’s article is back to a shorter format article, largely because this is what I was thinking about today. Because of that, you got an article explaining how to unzip all the files in a directory – and how to do so recursively – unzipping files nested in sub-directories. You’ll eventually need it! Maybe…

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.

How To: Show File Sizes When Listing Files In A Directory

Today we’re going to have an easy article that shows you how to show file sizes when listing files in a directory. It’s a simple set of commands and easy enough for a newbie to learn. Though the title is unwieldy, read on to learn how to show file sizes when listing files in a directory…

We’ll be using the ‘ls’ command for this exercise. Like normal, I’ll go ahead and suggest you read this page about why you shouldn’t parse the output from the ‘ls’ command. There are better ways and that link will explain it, and what you should consider, far better than I can.

Anyhow, the ‘ls’ command defines itself as:

ls – list directory contents

Of course, regular readers will recognize the ls command as we’ve used it numerous times before. Even though you shouldn’t parse the output from the command doesn’t mean it’s not still useful for a variety of other tasks.

Today, we’ll be exploring one of those tasks. We’re going to show you how to show file sizes when listing files in a directory – specifically with the ls command. Don’t worry, you won’t need to install anything. If you’re using any desktop Linux distro, the ls command is certainly available to you by default.

For the record, you should probably view the ls man page (run man ls in the terminal) to learn more about the command. It’s still a useful command – and I do sometimes parse the output from the command, but only when nobody is looking and when the output isn’t all that vital. I use grep with the ls command almost every day! Shh!!!

Show File Sizes When Listing Files In A Directory:

(That’s an unwieldy title!)

Anyhow, it’s the ls command. That’s a terminal-based command. So, of course, you’re going to need an open terminal (like so many of these articles). Press CTRL + ALT + T and your default terminal should open. Tada!

With your terminal now open, we can just use your home folder. To run the basic ls command, it’s just that:

Now, to show file sizes when listing files in a directory, you’d start with the -s flag:

You may find the output from that command is relatively useless without doing a bit of math to go along with it. Fortunately, you can tell the command to show the file size in a ‘human-readable format’. To do that, you’d run this command with the -h flag:

Now, if you wanted to show all the files, including the hidden files in that directory, you’d just add the -a flag. The command would look like this:

And if I was a Linux newbie, that’s the command I’d commit to memory. It doesn’t make a mess. The output is still plenty clear. It’s easy to understand. It’s easy to remember and turn into a habit. This way, when you ran the ls command, you just default to using -ash flags.

If you don’t want columns (and some do not), and you want more information such as file permissions or ownership values, you can just add an -lflag to the above. That’d look like:

So, it’s up to you…

You can remember ‘ash’ or you can remember ‘lash’. You can also not remember either and ignore the flags completely, but I find developing a habit is a good way to remember things. It’s your device, you do what you want!

Closure:

There you have it! You have another article. This one was simple enough and I hope you found it useful. If you’re new and running the ls command is new to you, it’s worth learning some additional flags and gathering more information at a glance. That’s why there’s this relatively simple article that shows you how to show file sizes when listing files in a directory.

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