This is bit tricky and the solution I got is below script
for i in $(ls | grep ‘[0-9]’)
eval mv $i $(echo $i | sed ‘s/[0-9]/*/g’)
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.
Latest posts by Surendra Anne (see all)
- Online training on Linux Bash shell scripting - February 8, 2018
- 15 iostat command examples in Linux/Unix - December 4, 2017
- How to Installing and Configuring Firewalld on Linux - November 29, 2017
- 4 sleep command examples in Linux/Unix - November 2, 2017
- 5 SSH alias examples in Linux - October 16, 2017