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.

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.

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