Linux Essentials; Want to Learn Linux? Start With File System Operations

Table of Contents

Introduction

Put the terms ‘free’, ‘open source’ and ‘software’ in one sentence, talk of tech-gasm! Linux has attracted a lot of users with genuine love for it because of this nature thanks to it’s founder Linus Torvalds and it’s open source community that continue to support and add developments.
Linux is a family of open-source Unix-like operating systems based on the Linux kernel. It is typically packaged in a Linux distribution. Popular Linux distributions include Debian, Fedora Linux, and Ubuntu.
In this article I share with you how to get started learning linux, kicking your journey off with basic file system operations like traversing through directories, listing files etc using the terminal.

Linux terminal

A terminal is a command line interface (CLI) where you run your commands to perform specific tasks. The kernel acts as the middle man between you and the operating system, when you issue the command, it gets interpreted to the OS in appropriate format by the kernel where the OS then performs the intended task as directed by you.
In Linux, you can either run commands as the root user (admin) or as a normal user. The root user has all the privileges to execute any command while a normal user has limited access to some operations. It is however advisable to run commands as a normal user even when you are the administrator because things can and do go wrong sometimes.

Commands for basic file system operations

We’ll go over basic linux commands to help you get started with traversing through directories and performing basic file system operations. These include:

  • printing the current working directory
  • listing files in a directory
  • moving into a directory
  • creating directories
  • creating empty files
  • appending to and over-writing files
  • displaying file contents
  • copying files
  • moving files to another directory
  • renaming files
  • deleting files
  • deleting directories

Once you fire up a terminal, type the following command to print current working directory:

1
pwd
/home/lyrax/Projects/blog/Downloads

This is the path to the directory we are currently in.
To list files in the directory, we use the ls command:

Linux commands have parameter options associated with them, you can use these to dictate how you want a command to function. For example there may be hidden files in the directory and we want to print every file. In that case we pass the -a option to ls as follows:

We see that the three hidden files have now been output.
Let’s move into the ‘bets ml’ directory. In linux we say changing directories and we use cd command (change directory).

To ascend back to our previous directory, we use two dots and do cd this way:

Creating a directory:
We use mkdir command (make directory) followed by the name we want to call give the new folder.
Let’s call our new directory ‘movies’ :

We have created a new directory ‘movies’
Let’s create an empty text file. This comes in handy when you want to create a file fast without having to open a file reader application.
We use the command ’touch’ followed by the name we want to give our file:

We have created an empty text file that we have called ‘army’.

Let’s write to the empty file. We want to add a line ’this is a text file’ into it. We use the ’echo’ command to write and the ‘cat’ command to view file contents as follows:

To add another line without over-writing the file contents (known as to append), we use double ‘»’ signs. Using a single ‘>’ rewrites the file contents thus overwriting it. Study how the file contents change when we do the following:

How about copying files? We use the ‘cp’ command along with the file path and destination path to copy to as follows:

We have copied the text file to the ‘bets ml’ directory.
We may also want to move a file instead of copying it. We use the ‘mv’ command:

Now our text file is no longer in the movies directory but in the ‘bets ml’ folder.
We can also specify the name we want to give our moved file in the new directory. Say for example we want to call our text file navy.txt once it is moved, we specify the absolute path this way:

That is also how we rename files in the same directory, using the mv command.

To delete files in a directory, we use the ‘rm’ command (remove).

We have deleted the text file.
To delete directories, we use ‘rmdir’ (remove directory) for empty directories or ‘rm -r’. We specify the option ’-r’ to mean recursively, to remove non-empty directories.

Getting help on the terminal

Linux comes with pre-built-in documentation for its commands. You can access the command help information using the ‘man’ command.
Say for example we want to learn more about the ‘wc’ command. We can run ‘man wc’ or ‘wc –help’ from the command line to view the documentation or help information for this command:

The output on the right is the detailed documentation for ‘wc’ command.

Man is the command to turn to when you’re stuck or when you want to learn more about a command, Man!