Linux Dirty Tricks

How to find files or directories over 1GB of size?

[root@localhost]# du -h / | grep ^[0-9.]*G

OR

[root@localhost]#find / -type d -size +1G

WITH EXLUDES

[root@localhost]# du -h / --exclude=/var/local/www/mysite/default/images | grep ^[0-9.]*G

How to check how long a process has been running?

[root@localhost]# ps -p $$ -o etime=

# Where $$ is the PID of the process you want to check. This will return the elapsed time in the format [[dd-]hh:]mm:ss.

How do I list the contents of a tar or tar.gz file?

To List the contents of a tar file:

$ tar -tvf file.tar

To List the contents of a tar.gz file:

$ tar -ztvf file.tar.gz

To List the contents of a tar.bz2 file:

$ tar -jtvf file.tar.bz2

Where,
t: List the contents of an archive
v: Verbosely list files processed (display detailed information)
z: Filter the archive through gzip so that we can open compressed (decompress) .gz tar file
j: Filter archive through bzip2, use to decompress .bz2 files.
f filename: Use archive file called filename

Changing TOP text Output

* run top
* press shift + z
* select colors 
Select color as number:
   0 = black,  1 = red,      2 = green,  3 = yellow,
   4 = blue,   5 = magenta,  6 = cyan,   7 = white
* Press "a" or "w" to change colour scheme

How to find the MySQL Database size?

To find the database size of all your MySQL databases, this little SQL query might be of use:

mysql> SELECT table_schema "Database Name", sum( data_length + index_length ) / 1024 / 1024 "Data Base Size in MB" FROM information_schema.TABLES GROUP BY table_schema ;

+--------------------+---------------------+
| Database Name      | Database Size in MB |
+--------------------+---------------------+
| database1          |        513.71875000 |
| database2          |          2.47160511 |
| database3          |          5.96250000 |
| database4          |         1.238483250 |
| database5          |          8.49138069 |
| information_schema |          4.00781250 |
| mysql              |          6.29175186 |
+--------------------+---------------------+
7 rows in set (2.05 sec)

Database size of all your MySQL databases, including free space

mysql> SELECT table_schema "Data Base Name", sum( data_length + index_length ) / 1024 / 1024 "Data Base Size in MB", sum( data_free )/ 1024 / 1024 "Free Space in MB" FROM information_schema.TABLES GROUP BY table_schema;
+--------------------+----------------------+------------------+
| Data Base Name     | Data Base Size in MB | Free Space in MB |
+--------------------+----------------------+------------------+
| database1          |         513.71875000 |   53676.00000000 |
| database2          |           2.47160511 |       0.17476654 |
| database3          |           5.96250000 |   12993.00000000 |
| database4          |          1.238483250 |    6390.00000000 |
| database5          |           8.49138069 |    3408.00000000 |
| information_schema |           4.00781250 |       0.00000000 |
| mysql              |           6.29175186 |       0.01016235 |
+--------------------+----------------------+------------------+
7 rows in set (2.83 sec)

Finding out the largest tables on MySQL

SELECT CONCAT(table_schema, '.', table_name),
       CONCAT(ROUND(table_rows / 1000000, 2), 'M')                                    rows,
       CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G')                    DATA,
       CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G')                   idx,
       CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size,
       ROUND(index_length / data_length, 2)                                           idxfrac
FROM   information_schema.TABLES
ORDER  BY data_length + index_length DESC
LIMIT  10;
+--------------------------------------+--------+--------+--------+------------+---------+
| concat(table_schema,'.',table_name)  | rows   | data   | idx    | total_size | idxfrac |
+--------------------------------------+--------+--------+--------+------------+---------+
| prod87.link_out37                    | 37.25M | 14.83G | 14.17G | 29.00G     |    0.96 |
| prod87.article47                     | 12.67M | 15.83G | 4.79G  | 20.62G     |    0.30 |
| prod116.article416                   | 10.49M | 12.52G | 3.65G  | 16.18G     |    0.29 |
| prod84.article34                     | 10.10M | 10.11G | 3.59G  | 13.70G     |    0.35 |
| prod104.link_out504                  | 23.66M | 6.63G  | 6.55G  | 13.18G     |    0.99 |
| prod118.article198                   | 7.06M  | 10.49G | 2.68G  | 13.17G     |    0.26 |
| prod106.article126                   | 9.86M  | 10.19G | 2.76G  | 12.95G     |    0.27 |
| prod85.article35                     | 6.20M  | 9.82G  | 2.51G  | 12.33G     |    0.26 |
| prod91.article71                     | 8.66M  | 9.17G  | 2.66G  | 11.83G     |    0.29 |
| prod94.article14                     | 5.21M  | 10.10G | 1.69G  | 11.79G     |    0.17 |
+--------------------------------------+--------+--------+--------+------------+---------+
10 rows in set (2 min 29.19 sec)

Cat a file and exclude the commented lines

[root@localhost]# cat /etc/squid/squid.conf | egrep -v "(^#.*|^$)"

Where,
egrep -v means leave the following out
^#.* means patterns that begin with a #
| means or
^$ means patterns that are empty

Here’s a version that would also filter comments with spaces before the #, such as comments that are indented with code blocks:

$ cat /etc/squid/squid.conf | egrep -v "^\s*(#|$)"

Find and Replace String/Text

find /path/to/files -type f -exec sed -i 's/oldstring/new string/g' {} \;
Example:
[root@localhost]#  find /etc/httpd/conf.d -type f -exec sed -i 's/192.168.2.24/10.95.80.2/g' {} \;

Wherein,
/etc/httpd/conf.d – path to files
192.168.2.24 – old string
10.95.80.2 – new string

OR

$ sed -i -e 's/old-string/new-string/g' filename

One Liner Renaming Multiple Files in a Directory

$ for i in oldfilename.*; do mv -- "$i" "${i/oldfilename/newfilename}"; done

 

Spread the love

Leave a Reply

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