Quickly Backing Up Files in Bash

Linux bash

Here’s a quick and simple way to backup files in Bash by using Bash’s built-in brace expansion {,}.

Let’s first create a file:

$ ls -l > listing.txt

Now let’s create a backup:

$ cp listing.txt{,.bak}

And the result is a new file with the .bak extension:

$ ls -l listing*
Permissions Size User   Group Date Modified Name
.rw-r--r--  2.5k victor users 15 Oct 14:21  listing.txt
.rw-r--r--  2.5k victor users 15 Oct 14:21  listing.txt.bak

How about getting fancy and adding a date?

cp listing.txt{,.$(date +%Y%m%d_%H%M)}

And the result:

$ ls -l listing.tx*
Permissions Size User   Group Date Modified Name
.rw-r--r--  2.5k victor users 15 Oct 14:21  listing.txt
.rw-r--r--  2.5k victor users 15 Oct 14:21  listing.txt.bak
.rw-r--r--  2.5k victor users 15 Oct 14:23  listing.txt.20181015_1423

Editing Multiple Lines in Vim

Bash Linux Vim

A simple way of to edit (like commenting or uncommenting) a block of lines/code in Vim.

The example below explains how to comment multiple lines:

  • Place the cursor on the first line that you’d like to edit
  • Press Ctrl+v
  • User the arrow keys to go down until the last line
  • Press Shift+i to go into insert mode
  • Press #
  • Press Esc and wait a second

Special Characters and Symbols in Bash

Bash Linux

Character Recognition

Shapecatcher

You can use Shapecatcher to draw a character and try to recognize it.

Other usefull sites are &what and Unicode® character table.

In Bash

If you can paste the character in Bash, you can dump the character in hex with hexdump

$ echo "✰" | hexdump -C
00000000  e2 9c b0 0a                                       |....|
00000004

Use the hex value to recreate the character:

$ echo -e "\xe2\x9c\xb0"
✰

How to Create an Init Like Script

Bash Linux

On this tutorial I will explain how to create a quick init like script to be run in the background. We will not be adding this script to /etc/init or look into how to run it at startup. Instead we will run it manually. If you are looking for a Systemd version of this tutorial, check out my previous post Creating a Simple Systemd User Service.

First let’s create our service script. This is the daemon that will be running in the background. For this example we will create a script that monitors a log file:

tail -fn0 logfile | \
while read line ; do
  echo "$line" | grep "pattern"
  if [ $? = 0 ]
  then
    ... do something ...
  fi
done

Now let’s create a control script. This script is what we will use to start/stop our daemon.

#!/bin/bash

daemon="[path_to_my_daemon_script]"
name="Name for the service/daemon"
desc="Description for the script"
pid_file="/var/run/[daemon_name].pid"

# Check whether the binary is still present:
test -x "$daemon" || exit 0

case "$1" in
start)
  [ -f "$pid_file" ] && { echo "Already running" ; exit 0 ; }
  echo "Starting $name"
  "$daemon" &
  echo $! > "$pid_file"
  ;;
stop)
  [ ! -f "$pid_file" ] && { echo "Not running" ; exit 0 ; }
   echo "Stopping  $name"
   kill "$(cat $pid_file)"
   rm "$pid_file"
   ;;
restart)
   $0 stop
   $0 start
   ;;
status)
   if [ -e "$pid_file" ]; then
      echo "$name is running, pid=$(cat $pid_file)"
   else
      echo "$name is not running"
      exit 1
   fi
   ;;
*)
   echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0

Make sure both files are executable and you are ready to start your daemon.

[controlscript] start

You can check the status, stop, etc…

[controlscript] status

A Simple Powerline Prompt for Bash

bash Linux

Powerline-simple is a simple powerline prompt for Bash, written by yours truly.

The prompt displays the following information:

  • Previous exit code
  • Username
  • Hostname (when connecting via SSH)
  • Battery status
  • sudo cached credentials
  • Current path
  • Git status

You can download it from the GitHub project page.

code with