Linux Commands With Examples

Regardless the experience, every Linux user must has, at least, a basic knowledge about the terminal and its usage. This tutorial will explain and show with examples the most used and important commands, after a brief explanation of how the commands work.

For this tutorial, Linux Mint 18 has been used.

1. What are the commands? Where do they come from?

The concept of the commands is very easy: there are just binaries (we will also see that, actually, they can also be scripts) that are placed in a directory where the terminal knows it has to look when we execute something. This is not only applicable for Linux but for other operating systems, such us Windows, without going further.

So, the next question is: how does our system know where to look? This is achieved thanks to an environmental variable named PATH. This environments stores the locations where the terminal should look when we type a command, as explained above.

We can see what’s stored in our path executing the following command:

echo $PATH

For, in my case, the output is:

/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games /usr/local/games

(The output formatting may vary depending on the shell that is being used; in this case, zsh has been used).

So, whenever we execute a command, the system will look in those directories trying to find an executable with that name.

You may have already noticed that the echo command is actually an executable file stored in one of those directories (concretely, in /bin directory).

1.1. What happens if we have different commands in different places?

Perhaps you have already thought about what happens if we have executables with the same name, in locations that are added to the path. Don’t worry, nothing bad will happen to your system. But, in any case, this is something that has to be taken into account since we may get unexpected results when we execute a command. Sometimes, specially for new Linux users, this can be a headache.

The “priority” is defined by the value itself of the PATH variable. So, for the value above, the first place the system would look would be the /usr/local/sbin directory; then, /usr/local/bin, and so on.

1.2. Knowing what we are executing

Another question we can make to ourselves many times, related to the previous one, is which executable are we actually executing. Of course, we can search in the directories defined in the path looking for the first coincidence, but this is a considerable overhead.

For this, there’s an executable called which. This executable script returns the path of the given executable name that would be executed:

which <executable>

For example, if we want to know which Python binary we would execute, we would execute:

which python

Which would generate the following output:


1.3. Looking for executables in the path

Another interesting thing can be to know all the existing locations for the given executable, besides the “first” executable, the one returned by which.

The command for this is called whereis. Actually, this command does more than what said above: it looks not only for executables, but also for source code and manual pages.

It works the same as with which:

whereis <executable>

So, following with the previous example of Python:

whereis python

The output, in this case, would be:

python: /usr/bin/python2.7 /usr/bin/python3.5m /usr/bin/python3.5 /usr/bin/python /usr/lib/python2.7 /usr/lib/python2.6 /usr/lib/python3.5 /etc/python2.7 /etc/python3.5 /etc/python /usr/local/lib/python2.7 /usr/local/lib/python3.5 /usr/include/python2.7 /usr/include/python3.5m /usr/share/python /usr/share/man/man1/python.1.gz

2. Most important Linux commands

Now that we know how the Linux commands mechanism works, it’s time to see which are the must-know commands.

2.1. User management

User management is a task every systems administrator has to deal with quite usually. Let’s see which are the most important commands for this.

2.1.1. Creating users

For adding users, we have two commands: useradd and adduser. The first one if the low level, native Linux binary; and the second, a friendly wrapper built on the top of this first one.

Native binary: useradd

To create a user with useradd, in its most basic way, it’s just about executing:

sudo useradd john_doe

This method has some downsides: no home directory has been created or the shell is sh instead of bash. And even worse, the user has not a password. Using this command, it’s always advisable to use the following options:

sudo useradd john_doe -m -s /bin/bash
# And, then, set a password!
sudo passwd john_doe

Friendly wrapper: adduser

In any case, generally, this is not the advised way to create users. adduser is much more friendly. For example, just executing the following:

sudo adduser john_doe

We can interactively set the password, personal information, creation of the home directory, etc.

2.1.2. Removing users

As same as for adding users, for deleting them, we have two options: the native binary, and a friendlier script.

Native binary: userdel

For deleting users, just execute:

sudo userdel john_doe

For deleting the user as long with its home directory:

sudo userdel -r john_doe

Friendly wrapper: deluser

For just deleting the user:

sudo deluser john_doe

And with its home directory:

sudo userdel --remove-home john_doe

And also every file owned by him:

sudo userdel --remove-home --remove-all-files john_doe

2.1.4. List all the users

The users are defined in the /etc/passwd file, so we can check them with

cat /etc/passwd

2.1.5. List all the groups

The same as with the users, but in this case with /etc/group file:

cat /etc/group

2.1.6. List the groups a user belongs to

For this, we can use the groups command, specifying the user:

groups <user>

If we don’t specify any user, the current one ($USER) will be used.

2.2. Permissions

Another completely essential task is to manage the file permissions. As you probably already know, the permissions in Linux are configured for three “groups”: the owner of the file, the group the owner of the file belongs to, and the rest of users.

This section will show how to use the commands regarding the permissions.

2.2.1. Changing files permissions: chmod

For changing the permissions within files and directories, chmod command is used. For this, we have two options: the octal representation, or the symbolic one.

Octal representation

For using the octal representation, we have to know the following:

  • 0 for no permission.
  • 1 is for execution permission.
  • 2 is for write permission.
  • 4 is for read permission.

Knowing this, is just a matter of combining the digits to set the permission. For example:

sudo chmod 750 foo.txt

Would result in:

Read, write and execute permission for the user owning the file (7).

Read and execution permission for the users belonging to the group the owner of the file belongs to (5).

No permission for any others (0).

Symbolic representation

In the symbolic representation letters and arithmetic operators are used:

For the users, the symbols are the following:

  • u: file owner.
  • g: file owner’s group.
  • o: other users.
  • a: all users.

The actions are represented with the same symbols that we have seen before:

  • r: read.
  • w: write.
  • x: execute.

And the operators to set the permissions are:

  • =: sets the permissions as specified, overwriting any other previous permissions.
  • +: adds permissions.
  • -: removes permissions.

Taking this into account, the equivalent for the example seen for the octal representation, would be:

sudo chmod u=rwx,g=rx,o-rwx foo.txt

For this specific case, we have to type much more than with the octal representation. But for same cases is more useful. For example, to edit the permissions for all, or for a specific group:

sudo chmod a+w foo.txt

The previous command will set write permissions for all the users.

Note: if we are changing the permissions for directories, we can use the -R option to apply those specified permissions to all the objects inside the directory.

2.2.2. Changing files ownerships: chown

On the other hand of the permissions, we have the group assignment, which is actually easier than the file permissions. It’s just about:

sudo chown <owning-user>[:<owning-group>] <file|directory>

For example, to change just the owning user of a file:

sudo chown john_doe foo.txt

And, for changing also the group owner:

sudo chown john_doe:john_doe foo.txt

Note: if we are changing the permissions for directories, we can use the -R option to apply those specified permissions to all the objects inside the directory.

2.2.3. Login as other user: su

If at any moment we want to login with another user, we will have to use the su command. This is actually very easy: for example, to login as root command:

su - root

Then, we will be asked for the password of the given user.

2.2.4. Executing a command as superuser: sudo

This command, one of the first things learned when starting to use Linux, is for executing a certain command as superuser, as we already have seen before in this tutorial.

2.3. Networking

From while to while, we also have to deal with network related stuff. Let’s some commands for this.

2.3.1. Interface configuration: ifconfig

Probably the first network command one learns. ifconfig stands for interface configuration. It’s easiest usage is without any option, to show the information regarding every interface present in the system:


To show the information related to a specific interface, we can specify it. For example, for eth0:

ifconfig eth0

We can also set the interfaces down/up (this action requires root permissions):

sudo ifconfig eth0 up
sudo ifconfig eth0 down

The remaining useful command would be for assigning IP addresses to the given interface, e.g.:

sudo ifconfig eth0

2.3.2. Network statistics: netstat

This is probably the most used command in this scope. Let’s see the most useful examples:

List all the ports

netstat -a | more

List just TCP ports

netstat -at

List just UDP ports

netstat -au

List listening TCP ports

netstat -lt

List listening UDP ports

netstat -lu

Show the process associated to the port

Just adding -p:

netstat -p
netstat -ltp

2.4. Navigation and file management

These are probably the first learned commands. In any case, let’s see them.

Navigating through directories: cd

Just for changing the directory. We can specify relative or absolute paths:

cd foo
cd /home/julen/foo
cd # For going to $HOME.

Deleting things: rm

For both files and directories:

rm foo.txt
rm -d foo    # -d option is needed for directories.
rm -d -r foo # -r to remove recursively, if the directory is not empty.

Creating files: touch

Just for creating a files:

touch foo.txt
touch foo.txt foo2.txt

Creating files with content in one command: echo >

Useful for when we want to create files with contents in the fly:

echo 'file content' > foo.txt

Files and directories listing: ls

One of the most used Linux commands. For example:

ls     # Simple output, just list the files in the current directory.
ls -a  #  List also hidden files (starting with '.').
ls -l  #  Detailed output (permissions, owner, etc.).
ls -lh # List also the size.

Creating links: ln

For creating links between files. In most of the cases, it’s for creating just symbolic links (a pointer to another file, with a different i-node). The usage is the following:

ln -s <source-file-or-directory> <dest-link>

For example:

ln -s a b

b will be a symbolic link to a.

2.5. Finding things

A very typical scenario is that when we have to look for something that we don’t know where it is. In this section we will see commands that will make our life easier in this aspect.

2.5.1. Finding inside files: grep

When we need to look for string inside logfiles, source code, etc. grep will make the work for us. The syntax is the following:

grep "<string>" <file>

For example:

grep "bar" foo.txt

Looking recursively in every files of the given directory

grep -R "bar" foo/

Showing the line number where each coincidence is

grep -n "bar" foo.txt

Case insensitiveness

grep -i "BaR" foo.txt

2.5.2. Finding files in the system: find

On the other hand, for finding files, we have the powerful find command, with the following syntax:

find <path> <expression>

Finding by literal name

find /home -name foo.txt

Case insensitiveness

find /home -iname foo.txt

Finding by file extension

find /home -name *.txt

Finding by owner user

find /home -user john_doe

Finding by owner group

find /home -group john_doe_group

Finding by permissions (octal notation)

find /home perm 777

Finding by permissions (symbolic notation)

find /home perm a=rwx

Finding by file type

find /home -type d foo # Directories.
find /home -type f foo # Files.

Finding by size

find /home -size +10M # Files bigger than 10 megabytes.

3. Summary

This tutorial has shown the most useful and important Linux commands, with examples for each one. We have started understanding how the commands work, and, then, diving into these commands.

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button