Q. One of the blog follower asked this question. Write a LINUX shell script that accepts an arbitrarily long list of file names as arguments and replaces all digit’s in them with a “*”.

This is bit tricky and the solution I got is below script

#!/bin/bash
for i in $(ls | grep ‘[0-9]’)
do
eval mv $i $(echo $i | sed ‘s/[0-9]/*/g’)
done

Some of the issues I faced are
1)In sed I tried to replace numbers with just * which created an issue
2)In sed I tried to replace numbers with * which is still not working.

So I tried to use eval to get this solution. I advantage of eval is that it will try to evaluate a shell code on the fly.
ls | grep [0-9] will give you the files which contain a single number in it’s filename, this output is feed in to for loop and each value is read in to i variable.

sed ‘s/[0-9]/*/g’ Will search for a number in the filename and replace it with *.
eval command will convert mv $filename $(echo $i | sed ‘s/[0-9]/*/g’) to mv file1name file*name

Hope this helps.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.