How to copy files ending with a specific string to another file in Linux/macOS?

In Java 17, the Locale for Indonesia changed from in_ID to id_ID. In my project, the resource bundle files are named with the suffix _in.properties which is unrecognized by Java 17. To resolve this issue, I need to create copies of these resource bundle files that ends with _id.properties.

Here are solutions for both Linux/macOS (using Bash).

#!/bin/bash

# Set the root directory for the search
ROOT_DIR="/path/to/root_directory"

# Find all files ending with '_in.properties' and process each one
find "$ROOT_DIR" -type f -name "*_in.properties" | while read -r FILE; do
    # Construct file name by replacing '_in.properties' with '_id.properties'
    NEW_FILE="${FILE%_in.properties}_id.properties"
    # Copy the original file to the new file
    cp "$FILE" "$NEW_FILE"
done

Save this script as copy_properties.sh, make it executable with chmod +x copy_properties.sh, and run it with ./copy_properties.sh.

Explanation

  • find "$ROOT_DIR" -type f -name "*_in.properties": Finds all files ending with _in.properties.
  • while read -r FILE; do ... done: Loops through each found file.
  • ${FILE%_in.properties}_id.properties: Constructs the new file name by replacing _in.properties with _id.properties.
  • cp "$FILE" "$NEW_FILE": Copies the original file to the new file.

These scripts will recursively search the specified directory for files ending with _in.properties, then create a copy of each file with _id.properties in the same directory.

How do I install Calibri font in Ubuntu?

I need to create a Microsoft Word Mail Merge document in my Java Spring MVC application. But running it in Ubuntu server resulting in a document that missing the default font use in the document, which is the Calibri font. So I need to install the font in Ubuntu to make the document looks as expected.

Here what I need to do to install the font in my Ubuntu box. Starts by updating the repository package list to get latest packages information for upgrades or new package installation.

sudo apt-get update

Then install FontForge in our system. FontForge is a free and open source font editor, but in this case it will help to do the font conversion in the installation script on the upcoming step.

sudo apt-get install fontforge

Install the Microsoft Cabinet file un-packer. This is required for the next script to successfully install the fonts.

sudo apt-get install cabextract

The following script will install Microsoft Vista TrueType Fonts (TTF) in Ubuntu. It includes the following fonts, Calibri, Cambria, Candara, Consolas, Constantia, and Corbel.

wget https://gist.githubusercontent.com/maxwelleite/10774746/raw/ttf-vista-fonts-installer.sh -q -O - | sudo bash

Run the next command to see if the font successfully installed. You will see the Calibri fonts in the result if the fonts successfully installed.

fc-list | grep Calibri

Here are the list of installed Calibri fonts.

/usr/share/fonts/truetype/vista/calibriz.ttf: Calibri:style=Bold Italic
/usr/share/fonts/truetype/vista/calibrii.ttf: Calibri:style=Italic
/usr/share/fonts/truetype/vista/calibrib.ttf: Calibri:style=Bold
/usr/share/fonts/truetype/vista/calibri.ttf: Calibri:style=Regular

How do I backup MySQL databases in Ubuntu?

What is MySQL

MySQL is an open-source RDBMS (Relational Database Management System). As the name implied it uses SQL (Structured Query Language) to access and manipulate data. MySQL has been widely used to store and manage data ranging from a simple web application to an enterprise class application.

The importance of data in every application require us to regularly back up the data to prevent data loss, for example caused by hardware crashes. In this post I will show you how to back up the database manually and using a script combined with a cron job to run the process automatically.

Using mysqldump

To create a database backup in MySQL we can use the mysqldump command. The example syntax of using this command is:

mysqldump -u username -p database_to_backup > backup_file_name.sql

If you need to restore the database you can use the following command:

mysql -u username -p database_to_restore < backup_file_name.sql

Before you can execute the command you might need to create the database if you don’t already have it.

saturn@ubuntu:~$ mysql -u root -p
CREATE DATABASE database_to_restore;

Creating Backup Script

To start let’s create MySQL user account that we are going to use to do the backup process. Login to MySQL using mysql -u root -p command. Type and execute the following command to create backupuser.

grant lock tables, select, show view on kodejava.* to 'backupuser'@'localhost' identified by 'backuppasswd';
flush privileges;

Exit from the MySQL using the exit command and create the following backup script called backup.sh using your favorite editor. For example, you can use nano or vim to create the file.

#!/bin/sh
BACKUP_HOME="/home/saturn/backup"

cd $BACKUP_HOME
directory="$(date +%Y%m%d)"

if [ ! -d "$directory" ]; then
    mkdir $directory
fi

backupdir="$BACKUP_HOME/$directory"
backup="kodejava-$(date +%Y%m%d%H%M%S)"

mysqldump -ubackupuser -pbackuppasswd --opt kodejava > $backupdir/$backup.sql

cd $directory
tar -czf $backup.tar.gz $backup.sql
rm $backup.sql

To make the backup.sh file executable you need to run the chmod +x backup.sh command.

Creating Scheduler Using Crontab

The crontab command is used to schedule commands to be executed periodically at a predetermined time. It will run as a background process without needing user intervention. These kinds of jobs are generally referred to as cron jobs and the jobs will run as the user who creates the cron jobs.

In the example below we register a cron job to execute the script at 12:00AM every day. To edit the cron jobs type crontab -e, this will open the crontab file.

saturn@ubuntu:~$ crontab -e
no crontab for saturn - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/ed
  2. /bin/nano        <---- easiest
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

Choose 1-4 [2]:

Select an editor to edit the crontab, choose by entering the number of the editor. The easiest one is nano but you can also use vim if you comfortable with it.

And you will see an empty crontab file will the following commented messages:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

Go to the end of the file and write the following entry to register a cron job. In the example below we register a cron job to execute the backup.sh script at 12:00M every day.

# m h  dom mon dow   command
  0 0   *   *   *    /home/saturn/backup.sh

After you save the file you can use the crontab -l command to list the registered cron job. If you want to know more about crontab you can visit crontab guru website.

How do I clear the current command line in terminal?

Terminal

You have typed a long line of command in terminal. But now you want to clear or delete the entire line. Deleting each character in the command will take sometime and bored you. So are there any keyboard shortcuts that allow you to do this? Yes there are some hotkeys to the rescue.

Hotkeys Description
CTRL + u Delete the current command.
The deleted command will be stored into a buffer.
CTRL + w Delete a word.
CTRL + c Abort what you are typing.
CTRL + d Delete current character.

Other hotkeys that might help you work faster in the terminal.

Hotkeys Description
CTRL + e Move to the end of line.
CTRL + a Move to the start of line.
CTRL + k Cut text from the cursor to the end of line.
CTRL + y Paste the last cut text or buffer.
CTRL + - Undo.
CTRL + b Backward one character.
CTRL + f Forward one character.
ALT + Backward one word.
ALT + Forward one word.

How do I pass password to sudo commands?

If you want to run a sudo command without being prompted to input the password you can do the following command.

echo password | sudo -S rm -rf /opt/jetty/

In the command above we are trying to remove the /opt/jetty directory using the rm -rf command. The -S (stdin) option allow the sudo command to read password from a standard input instead of a terminal device.

If you want to store the password in a file you can use the cat command instead of echo like the following example.

cat password.txt | sudo -S rm -rf /opt/jetty/