How to use Linux Commands
Getting started
It won't be long after starting to use Linux that you ask a question and the answer begins with, "Open a terminal and..." At this point, you may be thrown into an alien environment with typed Linux commands instead of cheery-looking icons. But the terminal is not alien, it's just different, and in this guide we'll show you everything you need to know about how to use Linux commands.
While you can accomplish a lot in Linux using the graphical user interface, by being able to use Linux commands you'll be able to do more complex things faster, and in many cases, automatically.
So what are the pros of using Linux commands?
It is consistent: The commands are generally the same on each distribution while desktops vary.
It is fast: When you know what you are doing, the shell is much faster for many tasks.
It is repeatable: Running the same task again is almost instant – no need to retrace all your steps.
There is more feedback: Error messages from the program are displayed in the terminal.
Get daily insight, inspiration and deals in your inbox
Sign up for breaking news, reviews, opinion, top tech deals, and more.
Help is available: Most Linux commands provide a summary of their options, while man pages go into more detail.
You can't argue with the plus points, but what about the cons? Well, apart from not giving us pretty screenshots to brighten up the pages, the main disadvantage of the terminal is that you need to have an idea of the command you want to run, whereas you can browse the menus of a desktop system to find what you're after.
On this page, we will look at the layout of the filesystem on Linux, and the various Linux commands that you can use to manipulate it. On the following pages we will cover several other aspects of administering and using a Linux system from the command line.
What goes where?
Users coming from Windows can be puzzled by the way Linux handles separate drives and partitions. Unlike the drive letter system used by Windows, Linux mounts everything in the same hierarchy.
Your root partition, containing the core system files, is mounted at /, the root of the filesystem tree. Other partitions or drives can be mounted elsewhere at what are called mount points.
For example, many distros use a separate partition for the home directory, where users' files are kept, to make installing a new version easier. This is a completely separate partition, it can even be on a different hard drive, but it appears at /home just as though it were part of the root partition. This makes everything easier and transparent for the user.
There is another difference. Linux, in common with every operating system but MS-DOS, uses a forward slash to separate directories. The layout of directories is also different, organising files according to their type and use. The main directories in a Linux filesystem are as follows…
/ The root of the filesystem, which contains the most critical components.
/bin and /usr/bin General commands.
/sbin and /usr/sbin System administration commands for the root user.
/etc Where system configuration files are kept.
/usr Where most of the operating system lives. This is not for user files, although it was in the dim and distant past of Unix and the name has stuck.
/lib and /usr/lib The home of system libraries.
/var Where system programs store their data. Web servers keep their pages in /var/www and log files live in /var/log.
/home Where users' data is kept. Each user has a home directory, generally at /home/username.
Moving around
Now that we know where everything is, let's take a look at the common Linux commands used to navigate the filesystem. Before going anywhere, it helps to know where we are, which is what pwd does. Many Unix commands are short, often two to three characters; in this case, pwd is print working directory – it tells you where you are.
Many distros set up the terminal prompt to display the current directory, so you may not need this command often. Moving around is done with the cd (change directory) command. Run it with no arguments to return to your home directory.
Otherwise it takes one argument, the directory to change to. Directory paths can be either relative or absolute. An absolute path starts with / so cd /usr/local goes to the same place wherever you are starting from. A relative path starts at the current directory, so cd Documents goes to the Documents sub-directory of wherever you are, and gives an error if it is not there.
That sounds less than useful if you can only descend into sub-directories, but there are a couple of special directory names you can use. To go up a directory use cd .. – a single dot is the current directory. There is also a shortcut for your home directory: .
Let's say you have directories called Photos and Music in your home directory and you are currently in Photos, either of these commands will move into Music:
cd ../Music
cd /Music
You can tell where you are with pwd, but how do you know what is in the current directory? With the ls command. Used on its own, it gives a list of files and directories in the current directory.
Add a path and it lists the contents of that directory. If you want to know more about the files, use the -l (--long) option, which tells you the size and date of the file, along with information about ownership and permissions, which we will look at later.
With your permission
Every file object (that is files, directories and device nodes in / dev) has a set of permissions associated with it, as shown in the screenshot of the output from ls -l. These are normally in the form rwxrwxrwx and shown by ls, or the numeric equivalents. The three letters stand for read, write and execute, and are shown three times for the file's owner, the group it belongs to, and other users.
For example, rw-r--r-- is a common set of permissions for files; it means the owner of the file can read from or write to it, all other users can only read it. Program files usually appear as rwxr-xr-x, the same permissions as before but also all users can execute the file.
If a program does not have execute permissions, you cannot run it. This is sometimes the case with system programs owned by the root user and only executable by root.
When applied to directories, the meanings are slightly different. Read means the same, but write refers to the ability to write into the directory, such as creating files. It also means that you can delete a file in a directory you have write permissions for, even if you don't have write permissions on the file – it is the directory you are modifying.
You can't execute a directory, so that permission flag is re-purposed to allow you to access the contents of the directory, which is slightly different from read, which only allows you to list the contents (that is, read the directory).
File permissions are displayed by using the -l option with ls and modified with chmod, which can be used in a number of different ways, best shown by example:
chmod u+w somefile
chmod o-r somefile
chmod a+x somefile
chmod u=rw somefile
chmod u=rwx,go=rx somefile
chmod 755 somefile
The string following chmod has three parts: the targets, the operation and the permissions. So the first example adds write permission for the user. The next one removes read permission for other users, while the third adds execute permission for all users. + and - add and remove permissions to whatever was already set, while = sets the given permissions and removes the others, so the next example sets read and write for the file's owner and removes execute if it was previously set.
The next command shows how we can combine several settings into one, setting read, write and execute for the owner, and read and execute for the group and others. The final command does exactly the same, but using the numerical settings. Each permission has a number: 4 is read, 2 is write, and 1 is execute.
Add them together for each of the user types and you have a three-digit number that sets the permissions exactly (there is no equivalent to + or - with this method).
- These are the best Linux training providers and online courses in 2017