Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: For Filename do

Tags:

bash

filenames

Hi I'm not used to do things in Bash, so I'm having some problems:

My goal is to look for special files in a folder, and if found generate some other files with the same filenames, but different extension.

Something like:

For Files-that-are-called-"SysBackup*.Now" 
do
  NewFileName = ChangeFileExt(FoundFilename),BK
  GenerateNewfile(NewFileName)
done

The above is of course dummycode, I won't bother you with the code I have done, as it is not working :-)

So the goal should be:

If the folder contains Sysbackup123.now and Sysbackup666.now, I will end up with the files Sysbackup123.bk and Sysbackup666.bk

Thank you for any help

Michael

like image 920
Reiler Avatar asked Dec 14 '22 01:12

Reiler


2 Answers

Fairly straightforward:

for a in Sysbackup*.now;
do
  [ -f $a ] && touch $(basename $a .now).bk ;
done
like image 170
Stephen Darlington Avatar answered Dec 25 '22 22:12

Stephen Darlington


This is how I do it with sh:

#!/bin/sh

for file in SysBackup*.now; do
  if [ ! -e "$file" ]; then
    continue
  fi

  base=${file##*/}
  bk=${base%.now}.bk

  touch $bk
done
like image 35
Gregory Pakosz Avatar answered Dec 25 '22 22:12

Gregory Pakosz