megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Count the number of matches of a pattern in VIM

You can the count the number of matches of a pattern by using the n flag while using the substitute command. Try the following command:

:%s/something//gn

If you want to know on how many lines it matches, just omit the g flag:

:%s/something//n

Hope this tip helped you out!

Count the number of word occurences in a text file using grep

If you are using a GUI based application like Microsoft Word of Google Docs, it would be easier for you to know the number of word occurences in a file. But what if you are in a terminal? That's where both grep and wc comes handy tools.

Let's say you have the following text:

The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary.

Now, you can use the grep and wc tool to count the number of times "the" appears in the file:

grep -o -i "the" file.txt | wc -l

Hope this helps you out!

Install MariaDB 10.3 on Void Linux

I tried to find a proper guide on how to install MySQL 8.0 on Void Linux but I couldn't really find any. So, I thought of installing MariaDB 10.3 on my laptop instead.

Don't worry about using MariaDB as it meets the same standard enterprised requirements as MySQL. The only difference is that MySQL belongs to Oracle and MariaDB is for people who wanted to get out of Oracle's hands.

Anyway, let's get started.

Prerequisites

  • Need to have root privileges in order to install packages.
  • Basic know-how of Void Linux and it's xbps package manager.

Alright, go ahead and follow these steps one-by-one:

1. Install MariaDB

Type the following command to install the package:

sudo xbps-install -S mariadb

2. Initialize MySQL data directory

The following command will initialize a MariaDB data directory and create system tables in the MySQL database, if they are not present.

mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

The options used:

  • --user: The login user name to use for running the mysqld process.
  • --basedir The path to the MariaDB installation directory.
  • --datadir: The path to the MariaDB directory.

To know more about it, you can refer to MariaDB's documentation.

3. Enable MySQL service

If you are used to Ubuntu or CentOS, enabling services in Void Linux is a bit different but quite simple. Once a process is symlinked, it will start on boot and restarts if it stops unless you stop the service deliberately.

Type the following command to enable the mysqld service:

sudo ln -s /etc/sv/mysqld /var/service/mysqld

4. Start MySQL service

Type the following command to start the mysqld service:

sudo sv start mysqld

To confirm, if the service is running, type the following:

sudo sv status mysqld

And you'll see something like this:

run: mysqld: (pid 15136) 1116s; run: log: (pid 15025) 1172s

5. Secure MySQL installation

It's recommended to secure your installation, so type the following command:

sudo mysql_secure_installation

Once the command is executed, you'll be prompted with a few questions, respond according to your needs and you're done!

6. Test MariaDB connection

Now, all you have to do is, try to log in to your database by typing the following:

mysql -u root -p

Once executed, it would prompt you for your password (which you must have set in the previous step and if you are able to log in, you are good to go!

Hope this guide helps you out!

How to create an ISO image from CDs/DVDs?

I used to create ISO images using DAEMON Tools but I wanted to try something different and see if there's a way to create it using the Linux CLI.

Turns out, you can do in just a single line using the dd utility like so:

dd if=/dev/cdrom of=/path/to/your/directory/image.iso

The if stands for input file and of stands for output file.

Looks like, I don't have to use DAEMON Tools for stuff like this, I guess.

Hope you found this tip useful!

Truncate a file using redirection in Linux

Simply put, sometimes, there are situations in where you just want to clear the contents of a file without deleting it.

This could be for many reasons like to avoid permission related issues, or maybe the file could be having useless logs that amasses to a size that measures in GBs.

So, the easiest solution is to clear it away from a terminal is by shell redirection like so:

:> filename

Let me break down the command here:

  • The : symbol means true and doesn't produce any output.
  • The '>' symbol is used for redirecting the output of the preceding command (in this case, it's empty!)
  • filename is the file that you want to truncate. If it doesn't exist, the file will be created.

Alternatively, you can do the same by using the cat command to output the contents of the /dev/null device (which only contains a EOL character) to empty a file:

cat /dev/null > filename

Hope this comes in handy!

How to identify which Linux distribution is running in your system?

If you've read my earlier post, I was using Ubuntu, at that time, and I thought that was how you identify which distro is running in your system but using lsb_release -a is not always going to work as some distributions may not have it installed.

Try the following command to identify the distribution you are running in your system:

cat /etc/os-release

And you'll get the following output:

NAME="XXXX"
ID="XXXX"
DISTRIB_ID="XXXX"
PRETTY_NAME="XXXX"

Hope this helps you out!

Display list of files with their extension and file sizes

Last month, I was trying to free up some space in my company servers and I realized that there were a lot of .zip files taking up a lot of space.

So, I wrote the following command to list files by their extension:

find . -iname \*.extension -exec du -sh {} \; > file-list.txt

And later to determine which files are the largest, I executed this command to sort the list by file size:

sort -rh file-list.txt > newfile.txt

Hope this tip helps you too! 😄

View your wget progress even after closing your SSH session

Few days back, I ran a wget command to download a file and accidentally closed the SSH client.

I logged in again and checked the list of active processes and thankfully, the wget process was still running except that I wasn't able to know the current progress of it.

So, I did a little research and found a way to view the progress, so I tried the following command:

tail -f wget_log

And, I was able to view it's current download progress again.

Hope this helps you out!