Let’s take a closer look at Linux Files and Directories. Our Linux system has a whole boatload of files. In our case we’re working with homestead which is a custom built Ubuntu Linux server running as a Virtual Machine on VirtualBox. To check how many files and directories a particular directory is comprised of, we can use the tree
command with the –a
switch to include all files and directories. When we run this command on our home directory, we get 2,132 directories and 11590 files. That is a lot of directories and files! Let’s learn a bit more about those Linux Files and Directories now.
Linux Filesystem and Directories
Check Linux File Types
Since we have so many Linux Files and Directories to work with, it would be nice to determine what type of files we are working with. We can do this with the file
command. To test this out, let’s go ahead and navigate to a directory which we know contains some files and test it out. In the following example, we’ll head on over to our Laravel directory and examine the server.php
file to see what it is.
ls
app bootstrap composer.lock phpunit.xml readme.md vendor
artisan composer.json CONTRIBUTING.md public server.php
vagrant@homestead:~/Code/Laravel$
file server.php
server.php: PHP script, ASCII text
vagrant@homestead:~/Code/Laravel$
Nice! This is pretty slick, we can see that the file is a PHP script, made up of ASCII text. There are a few other files in this directory, namely readme.md, composer.json, composer.lock and phpunit.xml. Let’s have a look at what the file command tells us about those files.
file readme.md
readme.md: ASCII text, with very long lines
vagrant@homestead:~/Code/Laravel$
file composer.json
composer.json: ASCII text
vagrant@homestead:~/Code/Laravel$
file composer.lock
composer.lock: UTF-8 Unicode text
vagrant@homestead:~/Code/Laravel$
file phpunit.xml
phpunit.xml: XML document text
vagrant@homestead:~/Code/Laravel$
As we can see, we get some nice information about these files. So why all the fuss about files? Well, there is a saying or idea that in Linux, “everything is a file”. Kind of like how in JavaScript, everything is an object. So when working with Linux, or any operating system for that matter, we must think in the files mindset.
Look Inside Linux Files
In Windows, when browsing files with the GUI, we all know what to do. In fact, your 4 year old probably knows what to do. Double click to view. Well in the wild world of the command line with Linux Files and Directories, you don’t have that option. To view file contents in Linux you must (collective gasp!) type commands! There are several ways to do this, but one way is by using the less
command. So that server.php
file looks interesting, let’s have a look.
less server.php
<?php
$uri = parse_url($_SERVER[‘REQUEST_URI’], PHP_URL_PATH);
$uri = urldecode($uri);
$paths = require __DIR__.’/bootstrap/paths.php’;
$requested = $paths[‘public’].$uri;
// This file allows us to emulate Apache’s “mod_rewrite” functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a “real” web server software here.
if ($uri !== ‘/’ and file_exists($requested))
{
return false;
}
require_once $paths[‘public’].’/index.php’;
server.php (END)
Note! Lest you get stuck in this state, know that you need to type the Q
key on the keyboard to bring you back to the command line once you’re done looking around your Linux Files and Directories. Now here is something to know about working with Linux at the command line. Visually, it is extremely bare bones. No pretty graphics, no fluff, no syntax coloring. White text on a black background is what you get. So in the example above, we can see the PHP code contained in the server.php
file, however don’t expect anything pretty to look at like you would if you opened this file with PHP Storm. Such is the nature of working with the command line in Linux.
Explore The Linux Filesystem
No matter what flavor of Linux you decide to use, or need to use for a specific job or project, there are many Linux Files and Directories that are going to be the same across these different systems. This is really great since if you learn Linux on one distribution, your knowledge will more or less directly transfer to another type of Linux or UNIX. So now that we have a few commands under our belt, have a look at and inside the following directories and files to get more familiar with them. See if you can use cd
, ls
, file
, and less
to have a look at some of the various files and directories you see here in this table.
/ | This is the root directory of the system |
/bin | In here you will find programs needed for Linux to boot up and function properly |
/boot/grub/grub.conf | This file is used to configure the boot loader, or what you see when the system first powers on |
/boot/vmlinz | The Linux kernel itself. Steer clear of this or Linus Torvalds will drop a Watermelon on your head. |
/dev | This directory holds what are called device nodes. It is in /dev that the kernel keeps a list of devices the system makes use of. |
/etc | This directory holds configuration files that apply globally across the system. There are also a few shell scripts that launch at boot time. Files in /etc are normally readable text which makes sense as config files and simple scripts should be human readable. This is a good directory to become familiar with. |
/etc/crontab | This is a file that handles when to run cron jobs. These are automated tasks, and can be extremely useful. |
/etc/fstab | This is a text file that has information about storage devices such as hard drives and their mount points within the file system. |
/etc/passwd | This is a text file that contains all of the user accounts for the system. If we use the less command on our system, it looks like this:
vagrant@homestead:/etc$ |
/home | This directory is the home for all users on the system. So if you had a user Bob, he might have a directory located at /home/bob. Standard users can only write files in their directory. This prevents them from turning you Linux machine into a brick. |
/lib | This directory has shared files with get used by many core processes in the Linux system. |
/lost+found | Yes, even in operating systems there is a lost and found. The purpose of this directory is to attempt system recovery in the case of a crash. Normally, this directory is empty. |
/media | This directory handles mount points for things like USB drives or other types of removable media which mount / unmount automatically on insertion. |
/mnt | This is more of a generic mount point and may contain information about the hard disk in use on a dual boot machine. |
/opt | This directory holds software that as its name implies, is optional. For example, the software contained here is not core level, it is an add on, much like Google Chrome. |
/proc | The proc directory has all kinds of magical voodoo happening within it. Think of it as a virtual filesystem of the Linux Kernel. It’s best not to mess around in this directory unless you have a very specific reason to. |
/root | The home directory for the root user. |
/sbin | This directory holds system programs, or binaries as they are called in the Linux world. These are programs that are typically run by the superuser, as they carry out vital system tasks. |
/tmp | Just like in Windows, you will have the need for temporary files during normal operation of the system. In Linux, those files get written to and deleted from this directory all the time. |
/usr | This directory is HUGE. It contains all programs and files used by users of the system. By running a tree command on our Linux homestead system, we returned a result of 10,522 directories and 71,046 files. |
/var | This directory contains files and directories that may be dynamic. Data that gets updated frequently like databases, mail, and others are held here . |
/var/log | This directory has all kinds of log files that are useful for debugging various system issues. |
Conclusion
We’re making good progress getting up to speed with all of the basics of Linux Files and Directories. In this episode, we created a nice table to remind ourselves what some of the more commonly used directories and files are used for. One last helpful trick, when you are practicing all of this navigation at the command line, you may occasionally want a “fresh start” so to speak. Simply type the command clear
at the terminal and you will once again have a nice blank slate to work with.