How To: Find The File Type In The Linux Terminal

There are many file types in Linux, and we’ll learn to find the file type in the Linux terminal in this article. It’s not terribly difficult and is a good basic article, with a command not that widely discussed. It’s a good time to learn.

Let’s start at the beginning… As many of my readers are new, there’s some stuff you ought to know.

First things first, everything in Linux is a file. I realize that that may confuse some folks new to Unix/Linux, but it’s true. If you don’t know how this works, click this link. That should explain it well enough.

Linux also uses the whole Magic Bytes thing. You can click here and learn about Magic Bytes. To explain it a bit differently than Wikipedia – it’s why you can make a text file without an extension and still have it open with a text editor when you click on it. The system sets Magic Bytes that mark the file as being of a certain type.

Well, there’s a tool that you can use in the terminal to find a file type. Amazingly enough, that tool is called ‘file’. The man page for which is clear:

file — determine file type

Yup, that’s the tool and that’s what it does. It’s pretty accurate and works with a number of file types. It checks things like whether the file is an empty file, what response it sends when queried, if it has Magic Bytes, and the language used in the file. It’s pretty comprehensive.

Find The File Type:

Obviously, this requires an open terminal. After all, we’re finding the file type in the terminal. That kinda needs an open terminal! Just press CTRL + ALT + T and your default terminal should open. Tada!

Now, with your terminal open, and enter the following command. I’m pretty sure this will work on any desktop Linux!

It’ll happily spit out that you have a data file on your hands.

But, here, let’s see if we can fool it. Grab an image and call it image.png (or whatever extension) and run file <filename.extension> to see the output. Now, rename the file to just plain ‘image’ and run the command again (sans extension). What does it tell you? It should look something like this:

file command in action
See? It still knows that it’s an image file. Images use the Magic Bytes.

Go ahead and try to fool it. Rename it image.txt and try it again. Pretty neat, huh?

I don’t need to patronize, by now you get the idea. You see what it can do. Well, there’s a bit more. You can create a text file with a list of files in it (with their path if in different directory) and the run file on that file you created – just make sure it’s a plain text file that you created. It’ll happily output the types of all the files listed inside.

You can also use the whole wildcard thing. You can get all the file types in a directory with this command:

If you want all the files starting with the letter I, you’d do this:

If you want, you can even use it on compressed files. For that, there’s the -z flag. It looks something like this:

It’ll spit out some information, perhaps letting you know the minimum version of your archive manager needed to open it. It doesn’t give you information about the compressed files, however. To do that, you’d have to extract the files first.

Closure:

And there you have it. You have yet another article! This one shows you how to find the file type in the terminal, and is a handy tool indeed. I normally take the entirety of January off, but I can’t do that this year. This year, I must ensure there are articles. Maybe next year! The good news is I can author these things with a wee bit o’ the wine in me. 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 own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Today, We Have Fun In The Linux Terminal

Today, we will learn nothing of value – except how to have fun in the Linux terminal. You know what they say, all work and no play makes Jack a dull boy. So, today we’ll be doing some playing.

Why? Well, because it’s fun. There are a number of fun things you can do in the Linux terminal, but this article is only going to cover a few of them. All of these are harmless, some perhaps even fun to share with a co-worker (perhaps even mischievously) and some just to make you smile.

For the sake of brevity, I’m just going to write this from the perspective of an Ubuntu user. You should be able to find equivalents for most every major distro, but that’s up to you. I mean, it’ll work in Debian and Mint etc, but you’ll have to hunt for yourself to find them in Arch, Fedora, or Gentoo!

So, without further ado…

Fun In The Linux Terminal:

This article requires an open terminal, like many other articles on this site. 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.

Once you have your terminal open, let’s visit the Matrix!

cmatrix:

Start this off by installing ‘cmatrix’ with this command:

Next, in your terminal, type ‘cmatrix‘ and watch the screen. It’ll look something like this as the text flows by:

cmatrix in action
With the new Matrix coming out… Feel free to run this when nosy people look over your shoulder!

See the man page for more detailed usage. Or not… It’s just for fun!

sl (Steam Locomotive):

This one is for those times when you fat-finger ‘ls’ and type ‘sl’ by mistake. My screenshot abilities were lacking with this one. So, you’ll have to make do. 

To install ‘sl’, just do this:

The next time you mistype ‘ls’ you’ll see what it does. Of course you can just type ‘sl’ and not wait for it. If you do so, be prepared for a steam locomotive that looks a little like this:

sl in action
Choo choo!!! It makes a more complete train, I just suck with graphics.

That’s sure to brighten a day such as the one where you’d be mistyping ‘ls’. Who doesn’t like trains?!? Sad people. Sad people don’t like trains.

Toilet:

You’ll find that toilet is a free version of figlet, a tool for making text larger for things like banner printing. I don’t think too many people have printed banners since the end of the dot matrix era, but it’s fun to play around with in the terminal.

To install toilet, and its wonderfully juvenile name, you just use:

There are a number of advanced features, but you can just type ‘toilet <text>’ and press the enter button. For example, there’s this output:

toilet in action - showing large text
There you have it, a giant KGIII! Just what you wanted for your birthday!

Again, go through the man page for more options – like the ability to choose a font and size. Have fun with it!

Closure:

See? Just like I promised, you’ve learned nothing of value. You can use ‘sl’ on a co-worker, frighten people on a train with cmatrix, or make large ASCII text in the terminal to amuse yourself. Yup, nothing of value! You’re welcome!

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.

How To: Make A Directory In Linux

Today’s article will teach you how to make a directory in Linux. Making a directory in Linux is pretty basic, straightforward, and easy. There are some options when making directories which we’ll be covering, but we’ll just be using ‘mkdir‘ for this exercise.

You’ll find the man page describes mkdir eloquently enough:

mkdir – make directories

And that’s exactly what it does. It’s a tool for making directories. Again, it’s pretty straightforward.

If you want a decently useful directory structure, you’re going to want more than the default directories. You’re going to want to make a consistent and meaningful directory structure, which will save you quite a bit of time and effort. Life is easier when you have a useful directory structure that makes sense to you – making things easier to find.

In case one doesn’t know, you can use ‘folder’ as a synonym for ‘directory’. It’s a hierarchical marker to which files can be designated – meaning you can stick stuff in your folder if you want to. They’re one of the best ways to organize your files in a meaningful fashion.

Like files, there are permissions for folders. Often, those permissions are inherited by the files within, though that’s not strictly necessary. We’ll lightly cover that as well.

For such a simple subject, there’s a bit of meat to it. We’ll cover that too in this article about how to make a directory in Linux. It’s mostly a beginner oriented article, but there may be some options that are unfamiliar more advanced users.

On to the article!

Make A Directory In Linux:

This article requires an open terminal, just like many other articles on this site. You can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

Once you have your terminal open, you can change directories or make these practice directories in your home folder (which would lead to clutter). So, if you want you can run mkdir tmp && cd tmp to get a fairly clean workspace going. (See? We’re using mkdir already!)

Anyhow, a nice basic use is to make a directory. To make a directory called ‘foo’ then you’d simply use:

If you want to make parent and child directories, you can also do that with just one command:

You can even make multiple directories in the same directory. That’s just a simple use of brackets and looks like this:

If you want to set permissions at the same time, that’s also an option:

You can also add the -v flag (meaning ‘verbose’) to any of these commands. That will output the results of your command so that you can verify that the command actually created the appropriate directories. After all, you never know when you’ll fat-finger something.

Closure:

There you have it! It’s another article, this one teaching you how to use the mkdir command to make a directory in Linux. This article is not terribly difficult, but there are a few advanced options that can make your file management even better. It’s a handy set of flags to know if you’re keen on keeping your system free of clutter.

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.

How To: Find Your Present Working Directory

There are times when you need to know your present working directory. You may need to know because the terminal doesn’t output that data, you may need to know for scripting, you may need to know for a variety of reasons.

So, what is the present working directory? Well, it’s simply a fancier name for the directory you happen to be working in – usually in the terminal. If you open your terminal and type something like cd Downloads, then your present working directory is going to be something like /home/<user>/Downloads. It’s pretty simple and this is going to be a pretty basic article. It’ll be a nice and easy exercise, even for a newbie.

There are a couple of ways to determine your present working directory and we’ll share them with you today. It’s really not a very complicated article, and not all articles need to be complicated. Sometimes, you just need new information that will help get you up to speed with your Linux understanding. After all, that’s the stated goal of the site.

Your Present Working Directory:

This article requires an open terminal, like many other articles on this site. 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.

As I said above, there are a couple of ways to do this. With your terminal open, why don’t you type something like cd Downloads just to have a different directory than your default directory.

When that’s done, just enter the following command:

The output should look a little something like this:

present working directory
See? It’s all pretty basic information that you’ll need to know.

But wait, there’s more! There’s also an environment variable that will show you your present working directory. It’s pretty simple and you can use it like this (among other ways):

Be sure to make note of the dollar sign and the all caps. You’ll need to include those if you want it to work. Depending on what you’re doing, you may want to use the environment variable (as I understand, it’s better to do this when  you’re scripting).

You can see an example of $PWD usage here.

Closure:

There you have it, another article. This one shows you how to determine you present working directory. As far as articles go, this one is pretty short and easy, aimed at newer Linux users.

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 Learn About Absolute And Relative Paths

In your Linux journey, you’ll use both absolute and relative paths. It’s important to know the difference and, perhaps, when you want to use absolute paths or when you want to use relative paths. It’s a pretty easy lesson and not a terribly long article.

First, paths are directory names and links to files. They’re always unique names to files and folders. They’re useful for referencing things, knowing where things are, organization, and the likes. To manage a file, you should probably know where it is.

An example of an absolute path would be something like:
/home/<user>/.bash_profile

An example of a relative path would be something like:
~/.bash_aliases

An absolute path will always begin with a /, for example. As they’re the easiest, let’s cover them first:

Absolute Paths:

Let’s say you have a file in your Documents directory and it is named foo.txt. If you wanted to read that file, you could navigate to the Documents directory and use cat foo.txt to read the file. 

That’s only going to work when you’re already in the Documents directory. If you’re not in the proper directory, you’ll have to use the absolute path. That would look like cat /home/<user>/Documents/foo.txt and that would work. 

As I said already, absolute paths have to start with a /. They’ll also start with one of the root directories, such as /etc, /bin, or /media. You can see those directories in your file manager by going up a level until you can go no further, when you’re at the absolute / directory.

An absolute path works even if you’re working in a different folder in the terminal. When you’re in /home/<user>Pictures, cat /home/<user>/Documents/foo.txt will still work just fine. 

Again, they’re a unique identifier for a directory or a file. If you wanted to change the directory in the terminal, you could use cd /home/<user>/Pictures. That would work just fine.

Relative Paths:

Relative paths are paths based on where you are already. They’re based on your “PWD” or Present Working Directory. There’s a few ways that these can be used. Conversely, a relative path never begins with a /.

For example, if you’re already in /home/<user> then you can cd Documents to get to your Documents directory. As you can see, that’s much easier to type.

Of course, there’s the tilde – which already means /home/<user>. So while possibly not the same thing you’re thinking of when you think of relative paths, you can also get to the Documents directory with cd ~/Documents.

If you’ve used the ‘ls‘ command before, you may have noticed a . and a .. in there. Well, the . stands for your current directory (and some commands will want that) and the .. stands for the next highest directory.

Assuming you were in the Documents folder, you could do cat foo.txt you could also do cat ./foo.txt and get the same results.

While in the Documents directory, you could cd . and that’d take you exactly nowhere. More useful, you can cd .. and you’d be back in /home/<user>. If you did cd ../.. you’d end up in the /home directory.

You can also (though I have no idea why you’d want to) use the above mentioned $PWD environment variable, but I never do and I don’t see much of a reason why you’d want to. Assuming you were in /home/<user> you could, again if you wanted, use cd $PWD/Documents and happily reach the Documents directory just fine.

Absolute and Relative Paths:

As you can see, there are different times and places to use a relative and an absolute path. When in doubt, I prefer the absolute path. If I’m writing notes that may apply differently to different systems, I’m more likely to use the absolute path. When I’m in a hurry, I tend to use a relative path – when I can use one.

You might as well get used to using both of them. They both have a right time and place. Commands like chmod may want an absolute path or will want the directory included. So, you’ll possibly need to chmod +x ./foo.txt instead of chmod +x foo.txt. In time, you’ll adjust and know which suits your needs best.

By the way, if the tab autocomplete doesn’t work after the command, the command probably demands that you denote the current directory with the . indicator like in the chmod above. If you type chmod +x fo<tab> it will refuse to autocomplete the filename. Once again, in time you’ll learn when that’s needed.

Learning to use Linux is a journey, not a destination!

Closure:

And there you have it! You have yet another article said and done. This one isn’t like some of my normal articles, but it’s still information folks might want to know. I find as the site grows, it gets possible to reference earlier articles, which is quite nice.

There are some articles I simply can’t write until I’ve taken the time to write other articles. I kinda like that. I’ve been enjoying typing these things out. I find the better I can explain them, the better I understand them. Heck, I recommend y’all make your own sites as this is pretty darned informative for me. The feedback is awesome and helps me understand even more.

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.

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