Recursive Find and Replace using Git Bash on Windows 10

In my previous post I setup virtual TV Shows in Kodi for a couple of YouTube channels that I downloaded videos from.

I copy and pasted a template file to create multiple .nfo files,  but I made a mistake – all of the files have 2019 in one of the text fields instead of 2018!

Fortunately I found a nice quick solution that used Git Bash on Windows 10.

I simply mapped my file share as network drive Y, opened Git Bash,  and issued a couple of commands:

$ cd Y:
$ find . -type f -name "*.nfo" -exec sed -i'' -e 's/2019/2018/g' {} +

Done!

A quick run-down of the commands:

  • change directory to Y: (cd Y:)
  • find
  • file (-type f)
  • named *.nfo
  • run sed (-exec sed)
  • edit in-place (-i)
  • add script (-e ‘)
  • substitute 2019 (s/2019)
  • for 2018 globally (2018/g)

Very nice to be able to use bash inside Windows 10!

 

Notepad ++ Unix Shell Scripts and the ‘No such file or directory’ error on Ubuntu Server

ubuntu-server-logo

I first encountered this issue a long time ago and forgot to document it – so now I had to resolve it again.

I’m running Notepad ++ on Windows and using WinSCP to transfer bash scripts to Ubuntu Server. My script is saved as an Unix script file (*.sh, *.bsh) in Notepad++ and WinSCP is set to transfer files in binary mode.

When I run the script on Ubuntu Server I get the following error:

: No such file or directory

I could see the problem in Notepad++ and in nano on Ubuntu Server when I wanted to save the file – the file was formatted for Dos\Windows.

dos-windows
Notepad ++ document format set to Dos\Windows
dos-format
Nano saving .sh as Dos Format

The fix is to change the formatting to UNIX. In Notepad ++ click Edit, EOL Conversion and UNIX and save the file.

Easy when you remember how!

Ubuntu – Change Hostname Permanently Using the Command Line

ubuntu-logo

On Ubuntu the hostname is stored in both the /etc/hosts and /etc/hostname files. There are several ways that we can change the hostname in these files.

1. Manually Edit the hostname

We can manually edit these files using a basic text editor like nano:

sudo nano /etc/hosts
sudo nano /etc/hostname

In /etc/hostname simply overwrite the existing hostname with a new one. In /etc/hosts you will find the hostname on the line beginning 127.0.0.1 – overwrite only the hostname with the new one, and then reboot.

Editing /etc/hosts using nano
Editing /etc/hosts using nano
sudo reboot

2. Use sed to change the hostname

Another way to achieve the same goal is to use the sed command to replace the existing hostname with a new one.

For example, my Ubuntu Server has the default hostname of ‘ubuntu’.

Use the hostname command to check what your hostname is.

With sed we can look for our hostname (in /etc/hosts and /etc/hostname) and then replace it with the desired new-hostname:

sudo sed -i 's/ubuntu/new-hostname/g' /etc/hosts
sudo sed -i 's/ubuntu/new-hostname/g' /etc/hostname

Reboot:

sudo reboot

3. Write a Bash Script

It’s always handy to have a script to do things – so here is a quick bash script that I put together that uses sed to change the hostname and then reboot:

#!/bin/bash
#Assign existing hostname to $hostn
hostn=$(cat /etc/hostname)

#Display existing hostname
echo "Existing hostname is $hostn"

#Ask for new hostname $newhost
echo "Enter new hostname: "
read newhost

#change hostname in /etc/hosts & /etc/hostname
sudo sed -i "s/$hostn/$newhost/g" /etc/hosts
sudo sed -i "s/$hostn/$newhost/g" /etc/hostname

#display new hostname
echo "Your new hostname is $newhost"

#Press a key to reboot
read -s -n 1 -p "Press any key to reboot"
sudo reboot

Ubuntu – Enabling Respositories Using The Command Line

Installing software or making system changes from the command line can be a great time saver. Editing the repositories list though has always been something that I have done manually – either opening /etc/apt/sources.list with nano or making the necessary changes via the GUI.

Today, for example, I wanted to install Skype in Ubuntu 11.4 which meant enabling the “partner” repository. In /etc/apt/sources.list this would mean manually removing the # from the following line:

# deb http://archive.canonical.com/ubuntu natty partner

With a bit of searching though I found a solution that can be adapted to enable any of the existing entries in sources.list.

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
STOP="deb "
REPO="partner"
sudo sed -i "/^# $STOP.*$REPO/ s/^# //" /etc/apt/sources.list

The first command simply makes a backup copy of your sources.list file.

In the second line we define the parameter STOP as “deb ” so that we will only edit deb repositories (and not deb-src repositories which are not needed to install Skype).

The third line defines the REPO parameter which in this case contains a character string that is unique to the ubuntu natty partner repository.

The last line uses the sed command to remove the # according to the parameters that we have set.

This script gives a great and easily customizable framework to work from as STOP will either be “deb ” or “deb-src” and REPO will always be something unique to the repository to be enabled.

Kudos to Franklin52 on the The Unix and Linux Forums for the script.