Linux Commands With Examples - 2016 Pardo, Julen
Linux Commands With Examples - 2016 Pardo, Julen
Posted by:
Julen Pardo
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.
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
(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).
For example, if we want to know which Python binary we would execute, we would execute:
1 which python
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
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
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
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>
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
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
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 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
For example:
1 grep "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>
Case insensitiveness
1 find /home -iname foo.txt
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.