Linux CLI 101

Written by Ken Gypen

September 6, 2007 | 09:53

Tags: #cli

Useful stuff

The next chapter of our short CLI introduction will help you with some useful tips and tricks, or just some plain 1337 stuff that will aid you in the utter geek part of your life that you spend behind a Linux terminal.

First we start with the famous bash completion. In my opinion this is the most useful CLI aid ever invented. But what does it do? At it's most basic usage, it completes commands and directory names for you. At higher levels (which we won't be discussing in this 101), it can be fully adapted to fill in command switches and the likes.

A short example will illustrate how it works. Let's say that I'm like Brett, and thus am incredibly forgetful. But, my failing memory tells me the command I need starts with a 't', so I type t and press the 'TAB key' two times. Now BASH will show me a list of all the commands starting with the letter t. Hmm, I suddenly recall the command really started with 'to' so I add in a o. This shortens the list, and I see the command I needed, 'touch' so I add in a u and hit 'TAB' and 'touch' apprears.

Now, the same thing happens with finding directory paths and names. I recall that the file I want was listed under "/" (remember, that means root) somewhere (all files are listed under / in Linux, so that was a good guess). So I start with / and hit 'TAB' twice again. Now I get a list of all possible directories and files under /. Of course I want it in /home/glider, so I enter h after the / and hit tab. BASH completes my path to '/home/' (the only directory starting with 'h' under /), and when I hit tab again it completes to '/home/glider/' (glider is currently the only subdirectory of /home).

Linux CLI 101 Useful File Stuff Linux CLI 101 Useful File Stuff
Left - Bash Complete can save you some serious time buried in reference books;
Right - It's easy to look at just what is in a file straight from the CLI - and often with better results than a GUI browser.

All those empty files seem, well, useless, so lets move on to some useful files. First, we are going to read the actual contents of a existing file - '/etc/passwd'. Contrary to its name, this file doesn't contain passwords...but it does contain some great details for all the users that are on a system. To show the contents of the file we'll use “cat”. cat /etc/passwd does the magic. If you want to know what all that gibberish it just spat back at you means, read up here.

Less” is another, more advanced tool to show the contents of a file, but it's not installed by default on most distros. For Ubuntu or Debian users, you can grab it with apt-get install less (assuming you're a root privilege user), though other distros will differ slightly. If you ever wondered where the Linux phrase “Cat is better then dog and less is better then more” came from, you now have your answer. Of course, this is only the tip of the iceberg with displaying files - there's also handy things like "tail -n", which displays the last n lines of a file.

Finally, if you want to edit the contents of files yourself, there's nano for the beginners, emacs for the wannabes and vim for the real men (Just kidding, there have been plenty of flamewars already over this subject).

Linux CLI 101 Useful File StuffNow, onto some more advanced stuff - user rights. When you create a file, it will be owned by that user. When any other user (like root) creates a file in the same directory, that file will be owned by the creating user. “Ls” can also list the user rights among the files by using the '-l' switch. A full explanation about user, and group rights would be beyond the scope of this introduction, but there are plenty of guides out there.

You can also see the use of the “su” command in this guide and others (for ubuntu, usually you will see sudo su). This is used to become the SuperUser, named root. You cango back to your own user by entering exit. Beware, root is immensley powerful. The command “rm -rf /” executed as root will wipe your entire system clean, without even asking for confirmation (note: do this at your own risk!).

What you can also see in the screenshot to the left is that the '-a' switch for the "ls" command (here, it's combined with the '-l' to become '-al'). This shows more files then normal “ls”, incuding the hidden ones (those who start with '.'). You also notice 2 special directories, “..” and “.”. These are crucial ones, the “.” directory relates to the current directory, and “..” is for 1 directory closer to the root of the directory structure. This means that to go one directory back towards the root of the file system, you can simply type "cd ../"

A short example will make this clear. Say, we have a file named “file” and it is one directory up from the one we are currently in, and we want to move it to our current directory. This is easily done with a simple mv ../file .. Now, compare that with having to do a bunch of copying and pasting between two Explorer windows in WinXP or Vista. Suddenly, CLI is losing a lot of its geekyness isn't it?
Discuss this in the forums
YouTube logo
MSI MPG Velox 100R Chassis Review

October 14 2021 | 15:04