Delete Directories From The Terminal In Linux

In your Linux journey, you’ll eventually want to delete directories as you do some housecleaning. Keeping your computer tidy makes you more efficient. Deleting directories you no longer need helps keep your system tidy. 

It’s easy enough to manage and remove directories while you’re in your graphic file manager. Though sometimes, if you’re managing files in the terminal, you might just as well learn to manage directories while you’re in there. It’s not too terribly difficult.

As an example of why this is worth knowing, you may be working with a headless system, something without a GUI desktop environment. In that case, you will have no choice but to delete directories from the terminal. It’s just something you’ll come up against as you learn file management.

Either way, being able to delete directories from the terminal is a skill that comes in handy, a skill that you should know. And so it should be something you have in your toolbox.

The tools we’ll be using for this article are rmdir and rm. They’re both basic commands that have existed since the earliest of Unix days, and they’re both useful commands today.

The rmdir command describes itself as:

rmdir – remove empty directories

And rm describes itself as:

rm – remove files or directories

So, with that in mind, let’s jump into the exercise.

Delete Directories From The Terminal:

Deleting directories from the terminal obviously requires said open terminal. 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.

The first command is really simple. If it’s an empty directory that you want to delete, you can use the rmdir command. As you read in the intro, the directory MUST be empty for this command to work. It’s also really simple to run:

So long as the directory is empty, you should be all set to go. If the directory you want to delete is not empty, you have to use the rm command. It’d look like this:

If you’re curious, the -r flag means recursive – it’ll even delete directories (and the files within them) within the directory. The -f flag means force – it should delete all files and directories no matter what and without any confirmation.

If you’re curious about either of those two commands, and there should someday be an article about ‘rm’, and for now you can just check the manual. You’d use either man rmdir or man rm. There’s not much to the first, and the latter is complex enough to warrant its own eventual article.

Closure:

Whoop! There it is! It’s another article in what’s turning into quite a long list of articles. If I’m going to keep this up for a full year, I might just as well have fun with it – and this was a nice, easy, and fun article. It’s mostly aimed at new users, as long-term users would hopefully already know how to delete directories from the terminal.

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.

A Little About The ‘tail’ Command

Today’s article is about the ‘tail’ command, seeing as the last article was about the ‘head‘ command. The tail command is the head companion’s counterpart. It only makes sense to cover one after covering the other, so today’s article will do just that.

Like the head command, the tail command has been with us for a long time, since pretty much the earliest days of Unix. Where head shows you the first lines in a file, the tail command shows you the lines from the end of the file. The man page describes tail as:

tail – output the last part of files

The tail command is pretty handy, often used by sysadmins to monitor log files. It can also be used like the head command to quickly check the contents of a text file, but it shows the material at the end of the file and not at the start of the file. That’s useful for remembering where you left off, for example. Anyhow, there are all sorts of ways to use it and this article will explain some of them.

Getting Started With The ‘tail’ Command:

I don’t think it’s all that important for this article (I’m not sure, I haven’t written it yet!), but we can start on the same page like we did with the head command. 

We’ll need to get started with the terminal open. You can do open your terminal with your keyboard – just press CTRL + ALT + T and your default terminal should open.

Once you get the terminal open, you can run the following two commands. Be sure to press the enter button after each of them and it will download a handy text file (just some random numbers) so that we’re all working on the same file.

With that complete, we can head on into the main article! It shouldn’t be all that long or difficult.

The ‘tail’ command:

Seeing as you’ve already got the rnd-num.txt file downloaded and your terminal is already open, I think we can just jump into using the tail command. If you just want to view last 10 lines of a file, you can use this command:

On the other hand, you can use the -n flag to show a specific number of lines. If you only wanted to see the bottom 5 lines, you’d use this command:

Assuming you’re all playing the home game, and just to show a good example, the output from the final command would look similar to this:

tail with the -n flag
As you can see, it only shows the last five lines of text. Pretty neat, huh?

Along the same lines, but not necessarily as useful, is the -c command. It works the same way it does in the head command, namely showing the specified number of bytes. If you wanted to see the final 5 bytes, the command would look like:

You can also use tail on more than one file at a time. If you do so, you can also use the -v flag and it will helpfully show the names of the files. The command would look a little like this:

The output would look similar to this:

tail being used on multiple files
It helpfully shows you the file names, which can be handy if you’re using multiple files.

One of the command options available with tail isn’t available with head. That flag is the -f mostly used for logs. What happens is you use the -f flag and then tail keeps running, outputting new lines to your terminal as the occur. In that case, it’d be something like:

That should show the last 10 lines of the log file and then update when new lines are added to the log file you’ve opened. Use man tail for more usage information.

Closure:

And that’s it! There’s another article for the site and another article closer to reaching the project’s goals. This article covers the tail command, seeing as the head command was covered in the last article. Feel free to leave a comment sharing how you use the tail command, or maybe even just a comment or question to motivate me.

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.

It’s Time To Learn A Little About The ‘head’ Command

Today’s article is about the ‘head’ command. The head command is a tool for viewing a file’s contents (or piped data), starting from the top. There’s not a whole lot to the command, and this will make be a pretty short article that’s fit for a beginner.

The head command has been with us since the heady days of Unix (See what I did there?) and is still a useful command today. In fact, the man page defines it like this:

head – output the first part of files

As you can guess, it does exactly what it says on the tin. There are any number of circumstances when you might want to use it, but I often use it when I don’t remember the exact filename I’m after and just want to see the first few lines of text as a reminder. There are better uses.

Anyhow, there’s not a whole lot to it, but I’ll show you the basics. Like we did recently, let’s see if we can all get started on the same page. So, open your terminal and enter the following:

Doing that will put you in the Downloads directory and will download a text file (it’s perfectly harmless) and it means we are all working with the same settings. You do not need to do this, but it could help.

About The ‘head’ Command:

Seeing as you’ve already got the terminal open, and that you figured it out without me having to repeat it like I do in almost every article, we’ll just jump right into the first command.

That should output the first ten lines of the rnd-num.txt file, looking something like this:

head in action
If you used the rnd-num.txt, your output should be the same. Pretty neat, huh?

That’s the first ten lines from the rnd-num.txt file or, in other words, the head of the file has been outputted to the terminal. This has a number of uses, including the pipe. You can easily pipe it to another command. It’d look something like this:

That’s not all that head can do, it can output a specified number of lines. To do that, you use the -n flag. It looks like this:

You can also use the -c flag to show the first x-number of bytes in a file. That’s not very complicated. In this case, we’ll look at the first 25 bytes.

You’ll find the output looks pretty similar to the output from the previous head command. You can even work with multiple files and the head command will handle them easily. If you’re going to use multiple files, you should use the -v flag.

It’ll helpfully preface the start of each file with the name of the file. In this case, the first line of the output would look like this:

head with multiple files
See? It’ll handily list the filename before listing the output.

See? Pretty helpful!

As shown in the image, you can easily deal with multiple files and whatnot, but there’s really not much more to be done with the head command. If you’re curious, you can also enter man head to get more usage information.

Closure:

Yup… There’s another article. This is about the head command, a command that’s not used often but worth having in your toolbox. If you use it more often, feel free to leave a comment explaining what you do with 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 own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Let’s Play Around With The ‘MV’ Command

Today, we’re going to play with the ‘mv’ command, just as the headline implies. It’s a pretty easy command for anything you’re likely to do with it, and it’s a great way to accomplish some pretty neat things in the terminal.

So, let’s start with the ‘mv command. Right away, when we check the man page, it shows you one of its greatest features. It defines itself as:

mv – move (rename) files

And right away you see that it does more than just move files. It also seems to be a command that newer folks don’t use. Maybe they’re afraid of it? Maybe it just doesn’t get enough love? I dunno, but I know it’s a pretty great command. This article is gonna cover it.

Let’s mix things up a little! Why don’t you open your terminal and we’ll start from the same page. Enter the following commands into your terminal, one after the other, pressing enter between each one.

Now, assuming I’ve done that properly and you’ve followed along, you should have made some files, a couple of directories, and your ‘pwd‘ (print working directory) is ~/Documents/tmp.

See? Now we’re all on the same page, and this is a fine time to start an article about the ‘mv’ command!

Play With The ‘MV’ Command:

So, for once, I don’t have to start this section by telling you how to open your terminal! You should have it open from the preparation we did in the intro! That’ll save some time…

So, with that terminal open, I want you to enter the following:

That should move the ‘file1.txt’ to the test directory. If you want to actually verify this, just go like:

Which should show you that the file has in fact been moved. Let’s go back to that same tmp directory and verify that it’s no longer there.

And you’ll see that file1.txt is no longer there in the tmp directory. And, seeing as you’re back in the right directory, let’s rename a file using the ‘mv’ command. Try this:

This will have renamed the file2.txt to changed.txt and you can once again verify that the former has been renamed to the new filename changed with the ls command.

Finally, with one more trick up its sleeve, you can both change the name and move the file at the same time. To do so, you’d just go with:

If you then run ls in both the pwd and test directory, you’ll see all the changes have been made, exactly as intended. If you need to, open your graphical file manager and verify it visually, but you can just as easily use the ls command and verify it all.

For fun, if you want to delete all those, you can move to the ~/Documents directory (cd .. or cd ../..) and use this command:

That’ll clean up any sign that we’d been there and done this exercise. There’s more to the ‘mv’ command and, as always, be sure to check the man page for more usage instructions (to learn things like the -f switch). But, these are the most common ways you’ll use the ‘mv’ command and I wanted you to have a basic familiarity with it.

Closure:

And there you have it… It’s something a little different, as I felt it important that we start from the same place. I also figured I’d try a little bit of a different writing style for this one. If I can’t have fun, why do it? Either way, it’s yet another article. It’s one step closer to the year-long commitment I’ve made. (It’s gonna be a long year.)

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.