Practical Linux Commands with Options for Real Life

Hands On Linux Commands for Real Life

Practical Linux Commands
In this series I will be discussing practical Linux commands you use in real life with it's parameters and options, I will
go through the options and clarify them without digging deep and turn simple stuff into rocket science.

Also will not go through the command in specific order because this course is meant to be for people who are
refreshing their memory or starting to learn the commands so it will be good to go back to the command and see different
options from time to time. Continue reading

Allow Specific User a Permission to Certain Folders

setfacl command

To give a user access to a certain folder in Linux you don't have to change the permissions globally for that folder, instead you can use 'setfacl' for that.

Give user mike a full access to dotsway folder:

[root@hostname ~]# setfacl -m u:mike:rwx /home/dotsway/

Check users and groups access permissions to certain folder:

[root@HOSTNAME ~]# getfacl /home/dotsway/
getfacl: Removing leading '/' from absolute path names
# file: home/dotsway
# owner: dotsway
# group: it

Remove user access from folder

setfacl -x u:mike /home/dotsway


Add Path to Linux Environment

Add PATH to Linux Environment
You can always check what are in your path environment by running:

echo $PATH

Below example will add /usr/local/go/bin to environment, open now your own bashrc and add the following at the end of the file:

vi ~/.bashrc
export PATH=$PATH:/usr/local/go/bin

To reload that environment file you can run:

source ~/.bashrc

Now if you echo again the path go should be at the end.

echo $PATH

Find oldest files recursively in Linux

Find Files in Linux

Find oldest files within the same folder:

ls -lat | tail -n5

Above command will list all the files in a specific folder while sorting it by modified time using 'ls -lat', the using the pipe symbol to send the output to another command.

Tail -n5 will show only the last 5 results.

How to find oldest files recursively ? (Through the chain of folders' tree/hierarchy)

Your bet is using 'find' command as it searches recurrsively by default, but let us see how to use it's parameters

-type f is used to specify the search type which is file in that case, so in other cases you need to use -type d for directories only
-printf is used to output, and we will use %t to print time and %p to prnt filename while '\n' means new line
sort yes you got it right it's going to sort the output
head -n5 now you can use head to get the top files in the output or use 'tail -n5' or get the opposite while -n5 is the number of output files you want to show, so if you use 'head -n1' it will show only one file.

find /home/dotsway/ -type f -printf '%t+ %p\n' | sort | head -n 5
Fri Apr 29 10:45:15.0891747285 2016+ /home/dotsway/php.ini
Fri Aug 12 12:19:55.0110672762 2016+ /home/dotsway/php-modules-AfterNaviSiteRecompile.txt
Fri Aug 19 11:21:06.0000000000 2005+ /home/dotsway/perl/MKDoc-Text-Structured-0.83/README

Fixing 'Incompitable GLIBC' Libraries Installation


Ever installed any incompatible libraries to your RedHAT/CentOS system and got stuck ?! Don't worry we all did in some certain points, you can fix it by following the below method:

Reboot the machine into rescue mode

Unset the LD_LIBRARY_PATH so system not to use the third party libraries or the current ones.


Then install the right libraries for your system flavor and edition (below example is for glibc) :
(It's better to use --test for testing and check for errors before applying to system)

rpm -ivh file.rpm --test --force --noscripts --root=/mnt/sysimage

rpm -ivh glibc-<version>.rpm --force --nodeps --root=/mnt/sysimage


CentOS/Linux : Rescue Mode

RedHAT Emergency Mode

If your system is still running and you are using CentOS 7 then just type the below command, if you cannot access the command line then proceed to #2:

systemctl rescue

This will reboot the main system to rescue mode and ask you for the root password.
First reboot your system using either hard reboot or soft reboot, then pause the menu by moving up and down with cursor, then you need to hit 'e' to edit the parameters, you will see similar screen to below:

CentOS Rescue Mode

Type at the end of the line and before the initrd 'rescue' like below and then press or if you are using CentOS 7 then type '' then hit 'ctrl+x' to continue with modified parameters

RedHAT Rescue Mode


What does 's' attribute in file permissions mean ?!


Ever seen 's' or 'S' in the Linux file permissions ?

'S' = The directory's setgid bit is set, but the execute bit isn't set.
's' = The directory's setgid bit is set, and the execute bit is set.

What is setgid ?
When user creates a folder or a file under a setgid folder, the new file/folder will inherit  its group as the group of the folder's owner, instead of the group of the user who creates it.

So in above screenshot, all files/folders created inside /screen will have same 'screen' group.

How to change 's' permissions using chmod ?

chmod g-s /folder/file "remove" 
chmod g+s /folder/file "add"


Failed to connect to database DB Error: extension not found Array, oh Sweet!

Today while running one of the php scripts which connects to external database server on Linux machine and connects to MSSQL using Pear extension i got the below error:

Failed to connect to database 'database_name' on 'x.x.x.x' DB Error: extension not found Array

First of all always start by confirming that you can access the destination server and the port is open, this can be done on Linux box using below command:

nmap -sS -O server_name or IP_Address

You expect to see output similar to following:


Based on what Database is the destination and what port you should get something like 3306 for MySQL or 1433 for MS SQL like above, if the port is there and shows open like above example then you are good to go, otherwise you need to check firewall on both your server and the destination one.

Second are you using PHP Pear extension ? which was my case, then you need to confirm that you have php5-mysql or php-mssql package installed in the system ,depends on what SQL flavor you are connecting to,  to check for installed packages in CentOS or RedHAT you can use below command:

rpm -qa | grep -i php-mysql

If it's not there then you need to install it using either:

1- Yum

yum search php-mysql or yum php-mssql
yum install freetds "for MSSQL"
yum install freetds-devel.x86_64
yum install unixODBC unixODBC-devel
yum install package_name

2- RPM , by searching online for the required package rpm and install it using following method: (Search for the right rpm for your OS version) (Ex.: Don't install a php-mssql of CentOS 7 on CentOS 6)

rpm -i  downloaded_file_name.rpm

In some cases you will get errors asking for missing dependencies like below error example:


For such case you need to google/yum the missing package and install it, most of the RPMs needed can be found in here.

Upgrade glibc 2.12 to 2.14in some cases to install above modules the glibc 2.14 will be needed, i don't recommend that just find the right packages for your OS.

php -m | grep -i mssql

Still not there ? maybe you need to reconfigure your php, so run the following from the php folder:

./config.nice  --with-mssql=shared,/usr
make install

Now be sure that the php.ini file includes the extension , to know the location of the original php.ini file you can use below command:

php -i | grep -i php.ini
Configuration File (php.ini) Path => /usr/local/web/php/lib
Loaded Configuration File => /usr/local/web/php/lib/php.ini

Confirm that mssql or mysql is loaded by using:

php -i | grep -i mssql
MSSQL Support => enabled
mssql.allow_persistent => On => On
mssql.batchsize => 0 => 0
mssql.charset => no value => no value
mssql.compatability_mode => Off => Off
mssql.compatibility_mode => Off => Off
mssql.connect_timeout => 5 => 5
mssql.datetimeconvert => On => On
mssql.max_links => Unlimited => Unlimited
mssql.max_persistent => Unlimited => Unlimited
mssql.max_procs => Unlimited => Unlimited
mssql.min_error_severity => 10 => 10
mssql.min_message_severity => 10 => 10
mssql.secure_connection => Off => Off
mssql.textlimit => Server default => Server default
mssql.textsize => Server default => Server default
mssql.timeout => 60 => 60

Third , if still no luck then check if you have the required pear extension, for example if you are trying to connect to MSSQL then you need the required Pear extension, to list installed packages you can use:

pear list

To install the MSSQL Pear then use below command:

pear install MDB2_Driver_mssql

If other then it's always helpful to google "install pear mysql" or whatever the needed pear extension.

How to connect or test MSSQL connection from Linux Command Line ?

There are several ways to do so but for me i am using the following:

First install FreeTDS and UnixODBC:

yum search unixODBC
yum install unixODBC
yum install freetds

Then you can use "tsql" , below is an example and you can use also -h for help:

tsql -H  ServerIP_HERE  -U userName  -P passWord  -p 1433

FreeTDS(May 14, 2011)                                    

       tsql - utility to test FreeTDS connections and queries

       tsql   { -S servername [-I interface] | -H hostname -p port }
              -U username [-P password] [-o options]

       tsql   -C

       tsql  is  a diagnostic tool provided as part of FreeTDS. It uses the TDS protocol directly to con-
       nect to Sybase or Microsoft SQL Servers, and allows the user to issue queries that test the  capa-
       bilities of FreeTDS.

       tsql  is  *not* a replacement for a complete isql, such as sqsh (  It is designed to
       rely on the lowest level FreeTDS library, tdslib, as a way to isolate potential bugs in the proto-
       col implementation.

Below is a very simple php script which can test a MSSQL Connection from Linux server to external server to see if it's successful or not:

<?php $server = ''; $link = mssql_connect($server, 'dd_ods_ro', 'd4r.Qod5'); if (!$link) { die('Something went wrong while connecting to MSSQL'); } else { echo "connected successfully\n"; } ?>[/code]

Hope any worked for you.