Files and directories are fundamental concepts in Linux. Files contain content and data, while directories organize those files into meaningful and standardized groups. Having a working knowledge, particularly on the Linux command line, of files and directories gives you latitude for configuring your system, automating processes, and easily manipulating data.
Today, we’ll take a practical, real-world look at a few basics of Linux files and directories. I use this stuff all the time.
What’s in a File?
Files are simply containers of data. The data could be a program, binary numbers, digital audio, video, regular old text or whatever.
Log into your Linux machine, as a normal user and open a terminal window. Use ls on the command-line to get a listing of what’s in your home directory.
rob% ls -l
Here’s a portion of a typical user home directory.
The left-most columns are the directory indicator and the file permissions. Moving right we then have the owner and group. A little further is the file size and creation date. Finally, the right-most column contains the file or directory name. If there is a “d” in the left-most column, that particular line actually lists a directory.
File names usually contain a combination of letters and numbers, typically without special characters. Upper and lower case letters in filenames have significance and can’t be used interchangeably in Linux. A “ROB.TXT” file is not the same as “rob.txt”, even though it might contain exactly the same data. They are entirely different file names. Likewise, “Rob.txt” is a different file than roB.Txt. You get the idea.
Most Linux configuration is performed through the use of text files. Using text files for configuration makes it easy to change things, using a simple command-line human-readable text editor, even if you aren’t using a desktop or window-type graphical user interface. I prefer to use vi, in a terminal window to edit text files. Other people like a program called nano.
Often the file name extension reflects what’s in the file. For example “rob.txt” contains human-readable text. A rob.wav file contains digital audio in a WAV format. Programs use standard filename extensions. For example, a .odt file is a LibreOffice document. In the Windows world, a .ppt file is a Microsoft PowerPoint slide stack.
Files can vary all over the map in size. A simple Linux shell script or a dozen-line text file might only be a few kilobytes (KB) in size. On the other hand, a current version of Linux, in the form of a .iso image file might be over 1.5 gigabytes (GB) in size. It’s always good to be aware of file sizes because over time, big files tend to accumulate and fill up your disk space. I like to use the -h option with ls to show the sizes in KB and GB.
rob% ls -lh
OK, so now we know a little more about files. Where the heck are they? Directories organize and group your files.
Where’s My File?
The Linux file system looks like an upside down tree. A directory is simply a branch on the Linux file system tree. Other operating systems call directories folders. They are one-in-the same. You might think of files as the leaves on a branch. You might also have smaller branches (sub-directories) with their collection of leaves (files) and even smaller branches (sub-sub-directories). Most of my directories never go more than four or five levels deep.
The top-most Linux directory is called the root directory and it is represented with a “/” (slash) symbol. The “/” symbol indexes lower-level directory names, as well. /etc is the name of the directory that contains auxiliary files used by the Linux operating system. My user files and lower-level directories reside in the /home/rob directory. Some common Linux system directories include /etc, /bin, /home, /dev and /sys. Some of my rob user directories include /home/rob/Downloads, /home/rob/music and /home/rob/webdocs. Logging in with your user name and password will automatically put you in your own “home” directory, typically /home/[your user name].
Here’s a screenshot of a typical root directory.
You can create a new directory with the mkdir command and call it whatever you like.
rob% mkdir tech
Executing this line in a terminal will create the “tech” directory, in my current directory.
How do I know where I am in the directory tree? Use the pwd (present working directory) command.
Having created a new “tech” directory, we can move into it using the cd command.
rob% cd tech
Notice that I didn’t use /tech. That would put me up under the root (/) directory within a “tech” sub-directory, if it exists.
You could get to your tech directory, no matter where you are in the Linux file system tree by using the fully-qualified directory name. Say you are over in the /bin directory. Type the following.
rob% cd /home/rob/tech
Now you are back in your tech directory.
I could then go back up to my home directory by simply issuing a cd by itself. This works from anywhere too. You can also go up one directory (to my home directory) by issuing a cd .. command. I use this all the time. You can even get fancy and use cd ../.. to go up two directories.
Taking It Further
The best way to get familiar with Linux files and directories is to start using the cd, mkdir and ls commands to look around through the tree structure. Take a look inside some text files with cat or vi, too. Use “vi [file name.txt] to look at a text file. Hint: use the ESC key, then “:”, then “q” to get out of vi. Use vi –help to list out the command-line options.
You might also get your hands on a Raspberry Pi 4 and use it as your educational platform. Additionally, you’ll need an HDMI monitor, keyboard/mouse pad, 3-Amp power supply and at least a blank 16GB micro-SD card to get the Pi operational. Check out their “getting started” page. That way you can make the inevitable mistakes, without too much pain and keep all your personal and production work on your existing Linux, Windows or Mac notebook safe from inadvertent mishaps as you learn. The Pi product line was, after all, designed as a learning tool.
The Linux Foundation is a sponsor of The New Stack.
Feature image by TeeFarm from Pixabay.