Linux 4 All

Best Linux tricks source

Posts in the shell category

Here is a little curl magic to get you how long it it will take to connect to a site. It will display time to connect, time to namelookup, time to first byte and so on.

curl -s -w "\ntime connect: %{time_connect}\ntime namelookup: %{time_namelookup}\ntime pretransfer: %{time_pretransfer}\ntime redirect: %{time_redirect}\ntime to first byte: %{time_starttransfer}\ntime total: %{time_total}\nhttp code: %{http_code}\n\n" -o /dev/null http://linux4all.uk

time connect: 0.165
time namelookup: 0.133
time pretransfer: 0.166
time redirect: 0.000
time to first byte: 0.392
time total: 0.432
http code: 200

rm -rf is gone

It looks like you cannot run rm -rf as root on CentOS or RHEL6 boxes.

# cat /etc/redhat-release 
CentOS release 6.5 (Final)
# rm -vrf /               
rm: it is dangerous to operate recursively on `/'
rm: use --no-preserve-root to override this failsafe
# 

Don’t try this on a RHEL5 boxes.

I admit that is pretty lame, but until today I had no idea how to kill a specific MySQL query. It is quite easy.

First we need to get the queries:

mysql> show processlist;
+------+------+-----------+------+---------+------+-------+------------------+
| Id   | User | Host      | db   | Command | Time | State | Info             |
+------+------+-----------+------+---------+------+-------+------------------+
| 5585 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |
+------+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

Then kill the id:

mysql> kill 5585;
ERROR 1317 (70100): Query execution was interrupted
mysql>

A little “find and sort” magic to find largest 50 files on a server:

find / -type f -size '+1024k' -printf "%s %h/%f\n" | \
sort -rn -k1 | \
head -n50 | \
awk '{ printf("%5dMB\t%s\n", $1/1048576, substr($0, index($0, " ")+1, length($0))) }' 

For this, we are going to use egrep with -o option to print only the matched parts and not the entire line.

-o, –only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

Below there are 2 examples:

egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" $LOGFILE
egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" $LOGFILE

There are multiple ways of removing a file that starts with a dash sign (-).

1. Use — option to signal the end of options and disable further options processing

[root@linux4all test]# ls -l
-rw-r--r-- 1 root root 0 Nov  5 23:35 -rf file with space

[root@linux4all test]# rm -f -- -rf\ file\ with\ space

[root@linux4all test]# ls -l
total 0
[root@linux4all test]#

2. Put the full file path between single quotes.

[root@linux4all test]# ls -l
total 0
-rw-r--r-- 1 root root 0 Nov  5 23:43 -rf file with space

[root@linux4all test]# rm -f './-rf file with space'

[root@linux4all test]# ls -l
total 0
[root@linux4all test]#

This is one question you are most likely to get in a job interview, so next time you’ll be prepared.

When troubleshooting a server that is slow, sometimes you have to see top something CPU consuming processes.

The command to see Top 10 CPU processes is:

ps auxf | sort -nr -k 3 | head -10

When troubleshooting a server that is slow, sometimes you have to see top something memory consuming processes.

The command to see top 10 is:

ps auxf | sort -nr -k 4 | head -10