File System Essentials
Software engineers say that file system looks like a tree. Every branch is directory, each directory may have one or more sub-directories (other branches) or files (leaves). Directory can be empty. The top, main directory often called root directory (don’t confuse it with home directory, which is the root place for all the files for the current user).
And here is where miscommunication begins. If file system is tree-like, why is the main directory called root and not the trunk? In fact, branches always grow off the trunk. Also, when we imagine the tree, we assume that tree grows up. However, in all file managers branches (directories) grow down. You need to press Down key to move your cursor one position down the directory tree. Maybe it’s better to say that the top directory is not the root, but trunk?
Regardless of how you call it, trees do have less branches at the top, and more at the bottom, which is not the case for the file system. In a file system all files and directories are usually sorted in alphabetical and ascending order. Probably there is no any other good analogy, and now we assume that file system is a tree-like, and it is sort of true.
By the way, we highly recommend to find out how to sort files in your file manager. One of the tricks here is to sort by modification date in descending order. With this sort order you’ll see recently modified files at the top. And a programmer usually works with the most recent files.
Sometimes only knowing the names of recently modified files in your work directory can give you a sense of what is going on with your project (and it’s one of the reasons why every beginner needs a file manager). Believe us, when you’ll try this sort mode, you will never look back, it’s so useful!
If you craft programs for more than 20 years, you know that there are hardly many tools from the past which are relevant today. It is one of many reasons we’re using Ruby now: the knowledge gained ten and more years ago is still relevant today, and hasn’t devalued over the time. However, besides Ruby there are some other tools that still work and quite useful.
One of these tools is a file manager. There are many flavors of file managers, the most popular one for Windows is Far Manager:
Like it was mentioned before, working with a file system is crucial for any programmer, you should be confident with creating files, directories, copying, editing, removing files from your computer. File manager helps you a lot.
Most programming books do not cover this topic, giving you brief overview of essential shell commands. These authors assume you already know how to deal with files, and there is no emphasis on how to work with a file system efficiently, easy and fast. But we highly recommend installing and using file managers in your everyday work. File managers are far easier than shell commands, and it’s worth putting some time into it. At the same time learn file system the hard way, through shell commands. Sometimes we just need to get things done and move on, instead of spending time poking around with this or another shell command.
Because of that you don’t need to remember the list of shell commands given below. Eventually you will remember all of them. Moreover, these commands are for Linux/MacOS only. But some key combinations are for Windows. Good news is that there is unspoken rule, unwritten convention between authors of file managers: all of them have kind of the same commands and hot keys.
A careful reader will say “Wait a minute! We want to get rid of Windows, but at the same time about to learn some Far Manager commands?”
Well, it’s true, but with few caveats.
File manager is kind of ubiquitous tool. There are many flavors of file managers. Norton Commander is one of the first file managers, released in the age of DOS (Disk Operating System, outdated operating system from Microsoft released in 1981). Midnight Commander is another file manager for Linux/MacOS, and it’s quite popular today:
There are some other flavors of file managers. However, Far Manager is so useful tool, so programmers have ported this manager to Linux and MacOS. You can find setup instructions by following the link above, but we’ll cover it here for MacOS (link for Windows users is available at the beginning at this chapter):
- Install HomeBrew if it’s not installed.
- Run the following command in your terminal (can take up to 15 minutes):
brew install yurikoles/yurikoles/far2l
- Run Far Manager from your terminal with
far2lcommand. Keep in mind that after running Far Manager it will “block” your terminal window, you won’t be able to type anything until you close Far. The workaround for this is to run another terminal window, or open new terminal tab with ⌘+T combination.
You can try installing Far Manager on Linux by yourself, following instructions - which can be tricky for absolute beginners, but doable. You can also try Midnight Commander. Installation instructions are as follows:
$ sudo apt-get update $ sudo apt-get install mc
mc to run Midnight Commander.
Exercise If you’re on Windows, install Far Manager now, and plan switching to Linux Mint Cinnamon in the course of the next few weeks (don’t forget to backup your data before installing Linux!). If you’re on Mac or Linux, install Far Manager (recommended), Midnight Commander, or other file manager you like (you can google them by “best file manager for MacOS”).
Don’t be discouraged if this chapter was a little bit rough for you. Keep in mind that whatever you do, you’ll be able to run your Ruby programs in the cloud. For example, services like Repl.it offer hassle-free programming environment right in your browser (they also offer some free plans at the time of writing). It’s not a way to go if you’re looking to be a great software engineer, but it’s definitely a great backup plan! If nothing works on your computer, go the cloud and try setting things up locally later!
File System Navigation
File system navigation is just switching between directories to see what’s inside, to get the list of files, their names and other attributes. In Far Manager you can use Up, Down keys and Enter to switch into directory (we often say “to change directory”). Use Tab to switch from left to right panel and vice versa.
In bash (zsh or other type of shell) there are commands you need to type from your keyboard in terminal to change directories and get the list of files. It’s much easier to do in file managers, but you will definitely remember these shell commands over the time:
$ cd my_dir # switch to directory my_dir (or "change directory") $ cd .. # go one level up $ ls # list of files
Note: pound sign
# (or hash) indicates the comment. Everything after this sign will be ignored by your shell.
cd command is pretty straightforward and easy to remember (Change Directory). But
ls (list) is a bit tricky. It turns out that there’s something else in the file system: hidden files! On Linux and MacOS file names of hidden files start with
. (dot), so they are often referred as dot-files. To show all the files, including hidden you need to type
ls -a (
-a here is parameter to
ls command and means “all”). But the most useful command is
lstands for “long format” or “long listing”. This long listing has file attributes, file owner information, size in bytes and modification date.
atells that we want to show all files.
his the flag to show meta information in human-readable format. For example, instead of “1000000000 bytes” we’ll see “1GB”.
Note that Far Manager also has the setting to show/hide hidden files, you can reach it by going to Options (F9), Panel Settings, Show Hidden and System Files.
By the way,
h flag is quite useful and in addition to
ls, is often used in other commands. For example,
df -h stands for “disk filesystem in human readable format” and will display statistics about free space on your hard drives and mounted volumes (like USB stick):
Hit Ctrl+L in Far Manager to get the same useful system information about free and used disk space, memory, and so on. To hide this panel click Ctrl+L again:
Quite often you can switch on and off (toggle) different pieces of functionality by pressing the same key. For example, hit F3 to view a file in Far Manager and open built-in file viewer. To exit “view file” mode press F3 again. The same trick works in Midnight Commander.
We’ve gotten familiar with few shell commands (you don’t need to remember all of them, but may want to write down in your notepad or add a bookmark), but in file manager it’s all achievable with hotkeys. As you can see, hotkeys are much easier to remember, and result is more clear. Beginners not only able to see the contents of a file system without a need to remember
ls command and its parameters, but also can navigate file system easily with arrow keys.
Despite all the experience with shell commands, author of this book often jumps back to the file manager when necessary. This tool is also quite useful on new projects with directory structure you’re not familiar with.
Exercise Walk around file system on your computer with file manager and look at directories and files you have. Use F3 key to toggle the file view. Use Tab to switch between left and right panel.
Some useful file manager hotkeys (also see the bottom bar on screenshot above):
F3- toggle view file mode
F4- edit file
F5- copy file or directory from current panel to another
F6- move file or directory from current panel to another
F7- create directory
F8- remove file or directory
Tab- switch from one panel to another
Ctrl+L- toggle system information panel
As you can see, basic file operations are achievable with F-keys. If you’re planning to buy a new laptop, make sure your new laptop has physical F1, F3, F4… keys (for example, some newer Macs have touchbars instead of F-keys).