Geek Culture
Published in

Geek Culture

Getting to Grips With Linux

Some tips and tricks for beginners

If you are new to Linux this series of articles can give you some quick tips to get up and running in no time. Ranging from navigating directories, editing files and some other useful system management skills.

Everything introduced will be command-line based. If you don’t have a Linux machine, the best thing to do would be to run one on a cloud provider like AWS or Azure. AWS will give you a virtual machine as part of their free-tier. Check out this article to learn how to deploy a Linux instance on AWS.

Concepts addressed here will be:

  • Navigating and managing the file system
  • Searching for files
  • Creating and editing text files
Somewhere near Motueka, NZ. Photo cred — M.Raju

File System

Everything in Linux is represented by a file, from text manuals and scripts to hardware. There is the Filesystem Hierarchy Standard (FHS) which defines where files and folders should be located on a Linux distribution. This is a good starting point to know where to look for a specific file or folder, or, where to put something.

Absolute or Relative

When addressing a file or folder you will use one of two pathnames, absolute or relative. Absolute pathways can be identified as they always start with a /. An absolute path could only possibly point to one location, as it lists the entire path to the file from the root of the system. For example, /etc/apache2/sites-enabled is the absolute path to the configuration files for enabled Apache sites.

A relative path will either not have a / in front of it, or start with ./ . A relative path points to a file or location, but relevant to your current working directory. If your command line is currently in /etc , the path to enabled Apache sites could be apache2/sites-enabled or ./apache2/sites-enabled.

With this understanding, you can now start to navigate the file system of your Linux machine. The commands that will be used to do this are: pwd (print working directory), cd (change directory) and ls for listing files and folders. For most commands, a manual can be viewed within the terminal. Enter man <command> in the terminal to pull up the manual.

Navigating the filesystem

At your Linux terminal run the command pwd . This will show you the absolute path to your current location.

To move to a different location you can use the cd command with a parameter of where you want to go. For example, cd /etc/systemd is the use of an absolute path to the systemd files. No matter where you are in the file system, an absolute path will take you to the intended location.

To move up to the enclosing directory, enter cd ../ .

Just typing cd with no parameters will take you to the current users home directory. Enter cd / , to take you to the root of the filesystem. This is the top of the tree where you can see all of the filesystem available to you.

To navigate into the var folder from the root folder, you could type cd var. This is the use of a relative path. Using relative paths can be less verbose when navigating folders.

Change directory into /etc and run the command ls . You should be presented with a list of items contained within the etc folder. As you will see from the FHS described earlier, the etc folder commonly contains system config files.

The ls command can take some options to change the layout and information provided. Type man ls to see a list of options you can use to manipulate the output. ls -lah can be a common option set to use. -l changes the output to a long listing format, useful when viewing more details about the items. -a includes all items within the current directory. Some files can be hidden when they have a full stop prefix, i.e. .hidden. -h changes the size to a human-readable annotation such as K and MB.

Try this task to test your knowledge:

  • Navigate to /var folder
  • List the contents of the /var folder showing the sizes in a human-readable format and sorted by size
  • Use a relative path to navigate to a folder within /var
  • Enter the command that will navigate to the enclosing directory of your current location
  • Use a command to check your current location

Creating files and folders

Now that you can navigate, it’s time to make some things. The commands used here will be mkdir ,touch and rm.

mkdir stands for make directory. The parameter you append to it should be the path of the directory you want to create. This can be absolute or relative. Navigate to your home directory with cd. From here enter mkdir myFolder. Run ls to see the new creation.

If you wanted to create a new folder in the /etc directory, you could use an absolute path: mkdir /etc/newFolder. If you want to create a new directory of multiple levels, you need to add the -p option to avoid any errors. mkdir -p myDocs/work/hr would create the myDocs directory, which holds work, which holds hr . Without the -p option, this would fail as myDocs does not exist yet.

To create a file, use the touch command with a parameter of your desired path, absolute or relative. touch notes will create a file called notes in the current directory.

To remove something use the rm command. rm notes will remove the notes file previously created in the current directory. rm can be used to remove directories with the option -r . Use rm -r myDocs to remove the directory and all its enclosing contents.

Try this task to test your knowledge:

  • Navigate to your home directory, and create a new folder called Docs
  • Without changing your current directory, create a folder in Docs called work and within that, a file called notes . Can you do this with just one command?
  • From your home directory, try removing just the notes file.
  • From your home directory, remove the Docs folder and everything contained within it in one command.

Searching the filesystem

The find the command can be combined with many different options to suit your filesystem searching needs. The simplest implementation could be find -name filename . This would search the entire system for anything called filename.

You can add a directory to search within to narrow the search area. For example, to search for files named notes in the home directory, you would use find /home -name notes .

The search term can use the asterisk to find all files with a specific suffix. For example find /etc -name ".conf" to find all the .conf files in the /etc folder.

Editing Text Files

There are several terminal-based text editors available, all with different features and ways of working. This section will introduce the Vim editor. This editor is a long way from the slick and simple interfaces of modern word processing applications but is still incredibly powerful if you take the time to learn it.

Using the command vim with a parameter of the path to the file, you want to edit will either open it if it exists or create it if not. Type vim text to launch the editor.

Vim has multiple modes of working, but they boil down to command, command-line or insert mode. Press esc to enter command mode. In this mode, you will be able to use various keystrokes to edit, navigate and search the document. Press i to enter insert mode. Here you will be able to type away in your document. Add a few lines of text to your document, and switch to command mode with esc

In command mode, there is a huge amount of keystrokes available to you. Here are some to try:

  • Skip your cursor to the last line, enter G .
  • Delete the word the cursor is currently on withde . Or D for the entire line.
  • Undo the last action with u
  • v and the arrow keys to highlight a selection, or V and arrow keys to highlight lines at a time
  • Copy current selection with yy
  • Past below the cursor with p

There are many more including searching which should be learnt to harness the power of the editor properly.

When you're done with your edits, it’s time to save and quit. Move into command line mode with esc then : . You’ll see a colon appear at the very bottom of your screen. You could just save the file with w or you could save and quit with wq .

With these skills in hand, you are ready to explore the world of Linux and all the things you can do with it.

Coming soon part two of this series will look at handling the input, output and error streams as well as some useful commands such as xargs for repetitive tasks and grep for searching inside files.

If you have any question or comments, let me know!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store