Author: ZIADI Mohamed Ali

How to find a file in Linux?

In this tutorial, we will use some commands that help us to find files in Linux. These commands are: echo ls find whereis locate List files using echo command Many people are not aware of echo command potential. This will come very handy when listing files in a given directory where ls, find and other commands are not available in rescue mode. This is because echo is a built-in command and available in any basic Linux machine. The echo command uses Bash shell expansion to list files in a given directory. To list files in a given directory use below command. [10:23:38] [VPS1-centos7] root:/bin # echo *test gr2fonttest grub2-fstest mysqltest sctp_test View files using ls command The simplest way is to use ls command if you are looking inside a directory of files [10:23:38] [VPS1-centos7] root:/bin # ls *test* gr2fonttest grub2-fstest gtester gtester-report mysqltest sctp_test snmptest test testgdbm testparm varnishtest You can use several options with ls. The most used options are : -l : list -a : all files even hidden ones -h : human readble -r : reverse order while sorting -t : sorts output based on the date of modification of the file [10:28:07] [VPS1-centos7] root:/bin # ls -ltr *test* -rwxr-xr-x. 1 root root 30488 Jun 9 2014 testgdbm -rwxr-xr-x. 1 root root 32520 Jun 10 2014 sctp_test -rwxr-xr-x. 1 root root 147576 Mar 16 2015 varnishtest...

Read More

Mysql: How to find table and database size?

Sometimes you need to know the size of each mysql table to optimize them and gain some free space in the disk. To do so, you can check manually the size of each table in the mysql datadir (generally it is /var/lib/mysql), or you can do even better by asking Mysql to do it for you. Listing Mysql tables size You can use the command below when connected to the mysql shell: SELECT table_schema AS DB_NAME, TABLE_NAME, (DATA_LENGTH+INDEX_LENGTH)/1024 AS TABLE_SIZE_in_KB FROM information_schema.TABLES; To list above tables size in MB: SELECT table_schema AS DB_NAME, TABLE_NAME, (DATA_LENGTH+INDEX_LENGTH)/1024/1024 AS TABLE_SIZE_in_MB FROM information_schema.TABLES; In this blog, I am using KB as the unit because I have small tables. So depending on the size of your data you can adjust the unit. To list all tables having size bigger than 1 KB SELECT table_schema AS DB_NAME, TABLE_NAME, (DATA_LENGTH+INDEX_LENGTH)/1024 AS TABLE_SIZE_in_KB FROM information_schema.TABLES WHERE (DATA_LENGTH+INDEX_LENGTH)/1024 > 1; Example: mysql> SELECT table_schema AS DB_NAME, TABLE_NAME, (DATA_LENGTH+INDEX_LENGTH)/1024 AS TABLE_SIZE_in_KB FROM information_schema.TABLES WHERE (DATA_LENGTH+INDEX_LENGTH)/1024 > 1; Listing databases size To list each database size you can use the following command: SELECT table_schema "DATABASE_NAME", SUM(data_length + index_length) / 1024 "DATABASE SIZE IN KB" FROM information_schema.tables GROUP BY table_schema; Example: mysql> SELECT table_schema "DATABASE_NAME", SUM(data_length + index_length) / 1024 "DATABASE SIZE IN KB" FROM information_schema.tables GROUP BY table_schema; Or in MB: mysql> SELECT table_schema "DATABASE_NAME", SUM(data_length + index_length) / 1024 /...

Read More

Linux Bash History: Replace occurrences in last command

Sometimes we use Linux commands with many long arguments, and to use the same command with a little modification we are obliged to go through these args and modify them one by one. Example : touch /home/user1/test/scenario.txt /etc/my-server/users/user1.cnf /var/log/my-server/user1.log What will I do if I want to do the same to user2? To replace all occurrences in the last command you can use the command below: !:gs/old_occurrence/new_occurrence !refers to the last command gs refers to global substitute To replace a word you can use the command below: ^old_word^new_word Examples: [www1.linuxnix] root:~ # touch test1 scenario1 result1 [www1.linuxnix] root:~ # !:gs/1/2 touch test2 scenario2 result2 [www1.linuxnix] root:~ # ls -l test* scenario* result* -rw-r--r-- 1 root root 0 Nov 20 16:45 result1 -rw-r--r-- 1 root root 0 Nov 20 16:45 result2 -rw-r--r-- 1 root root 0 Nov 20 16:45 scenario1 -rw-r--r-- 1 root root 0 Nov 20 16:45 scenario2 -rw-r--r-- 1 root root 0 Nov 20 16:45 test1 -rw-r--r-- 1 root root 0 Nov 20 16:45 test2 [www1.linuxnix] root:~ # mkdir -p tests/exp/scenario1/tools/ [www1.linuxnix] root:~ # ^exp^lab mkdir -p tests/lab/scenario1/tools/ [www1.linuxnix] root:~ # tree tests tests ├── exp │   └── scenario1 │   └── tools └── lab └── scenario1 └── tools 6 directories, 0 files Note: when you use ^^ with multiple occurrences, only the first occurrence is replaced. Example: [www1.linuxnix] root:~ # ls -l test1 scenario1 result1 -rw-r--r-- 1 root...

Read More

Over 16,000 readers, Get fresh content from “The Linux juggernaut”

Email Subscribe

ABOUT ME..!

My photo
My name is Surendra Kumar Anne. I hail from Vijayawada which is cultural capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. At present I work at Bank of America as Sr. Analyst Systems and Administration. You can contact me at surendra (@) linuxnix dot com.