0% found this document useful (0 votes)
26 views

Linux Commands With Examples - 2016 Pardo, Julen

Linux Commands With Examples - 2016 Pardo, Julen

Uploaded by

verosaly
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Linux Commands With Examples - 2016 Pardo, Julen

Linux Commands With Examples - 2016 Pardo, Julen

Uploaded by

verosaly
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Linux Commands With Examples

Posted by:

Julen Pardo

Linux December 7th, 2016

https://www.systemcodegeeks.com/linux/linux-commands-examples/#section_2_1
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.


Tabla de Contenido
Linux Commands With Examples.............................................................................................................1
1. What are the commands? Where do they come from?..........................................................................5
1.1. What happens if we have different commands in different places?..........................................5
1.2. Knowing what we are executing................................................................................................6
1.3. Looking for executables in the path...........................................................................................6
2. Most important Linux commands.................................................................................................6
2.1. User management......................................................................................................................6
2.1.1. Creating users................................................................................................................6
Native binary: useradd...................................................................................................7
Friendly wrapper: adduser.............................................................................................7
2.1.2. Removing users.............................................................................................................7
Native binary: userdel....................................................................................................7
Friendly wrapper: deluser..............................................................................................7
2.1.4. List all the users.............................................................................................................7
2.1.5. List all the groups..........................................................................................................7
2.1.6. List the groups a user belongs to...................................................................................8
2.2. Permissions................................................................................................................................8
2.2.1. Changing files permissions: chmod..............................................................................8
Octal representation.......................................................................................................8
Symbolic representation................................................................................................8
2.2.2. Changing files ownerships: chown...............................................................................9
2.2.3. Login as other user: su..................................................................................................9
2.2.4. Executing a command as superuser: sudo.....................................................................9
2.3. Networking................................................................................................................................9
2.3.1. Interface configuration: ifconfig...................................................................................9
2.3.2. Network statistics: netstat............................................................................................10
List all the ports...........................................................................................................10
List just TCP ports.......................................................................................................10
List just UDP ports......................................................................................................10
List listening TCP ports...............................................................................................10
List listening UDP ports..............................................................................................10
Show the process associated to the port.......................................................................10
2.4. Navigation and file management.............................................................................................10
Navigating through directories: cd..............................................................................10
Deleting things: rm......................................................................................................10
Creating files: touch.....................................................................................................11
Creating files with content in one command: echo >..................................................11
Files and directories listing: ls.....................................................................................11
Creating links: ln..........................................................................................................11
2.5. Finding things..........................................................................................................................11
2.5.1. Finding inside files: grep.............................................................................................11
Looking recursively in every files of the given directory............................................11
Showing the line number where each coincidence is..................................................11
Case insensitiveness.....................................................................................................11
2.5.2. Finding files in the system: find..................................................................................12
Finding by literal name................................................................................................12
Case insensitiveness.....................................................................................................12
Finding by file extension.............................................................................................12
Finding by owner user.................................................................................................12
Finding by owner group...............................................................................................12
Finding by permissions (octal notation)......................................................................12
Finding by permissions (symbolic notation)...............................................................12
Finding by file type......................................................................................................12
Finding by size.............................................................................................................12
3. Summary..............................................................................................................................................12
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:

1 echo $PATH

For, in my case, the output is:

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


1 /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:
1 which <executable>

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

Which would generate the following output:


1 /usr/bin/python

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:
1 whereis <executable>

So, following with the previous example of Python:


1 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
1
/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:
1 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
1
-s /bin/bash
# And, then, set a
2
password!
3 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:
1 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:
1 sudo userdel john_doe

For deleting the user as long with its home directory:


1 sudo userdel -r john_doe

Friendly wrapper: deluser


For just deleting the user:
1 sudo deluser john_doe

And with its home directory:


1 sudo userdel --remove-home john_doe

And also every file owned by him:


1 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
1 cat /etc/passwd

2.1.5. List all the groups


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

2.1.6. List the groups a user belongs to


For this, we can use the groups command, specifying the user:
1 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:
1 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:
1 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:
1 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:
1 sudo chown <owning-user>[:<owning-group>] <file|directory>

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


1 sudo chown john_doe foo.txt

And, for changing also the group owner:


1 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:
1 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:
1 ifconfig

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

We can also set the interfaces down/up (this action requires root permissions):
1 sudo ifconfig eth0 up
2 sudo ifconfig eth0 down

The remaining useful command would be for assigning IP addresses to the given interface,
e.g.:
1 sudo ifconfig eth0 192.168.1.30

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
1 netstat -a | more

List just TCP ports


1 netstat -at
List just UDP ports
1 netstat -au

List listening TCP ports


1 netstat -lt

List listening UDP ports


1 netstat -lu

Show the process associated to the port


Just adding -p:
1 netstat -p
2 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:
1 cd foo
2 cd /home/julen/foo
3 cd # For going to $HOME.

Deleting things: rm
For both files and directories:
1 rm foo.txt
2 rm -d foo # -d option is needed for directories.
3 rm -d -r foo # -r to remove recursively, if the directory is not empty.

Creating files: touch


Just for creating a files:
1 touch foo.txt
2 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:
1 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
1
the current directory.
ls -a # List also hidden files (starting
2
with '.').
ls -l # Detailed output (permissions,
3
owner, etc.).
4 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:
1 ln -s <source-file-or-directory> <dest-link>

For example:
1 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:
1 grep "<string>" <file>

For example:
1 grep "bar" foo.txt

Looking recursively in every files of the given directory


1 grep -R "bar" foo/

Showing the line number where each coincidence is


1 grep -n "bar" foo.txt

Case insensitiveness
1 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:
1 find <path> <expression>

Finding by literal name


1 find /home -name foo.txt

Case insensitiveness
1 find /home -iname foo.txt

Finding by file extension


1 find /home -name *.txt

Finding by owner user


1 find /home -user john_doe

Finding by owner group


1 find /home -group john_doe_group

Finding by permissions (octal notation)


1 find /home perm 777

Finding by permissions (symbolic notation)


1 find /home perm a=rwx

Finding by file type


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

Finding by size
1 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.

You might also like