-1

I add the below first code to add the current date to the filename and the second one to delete the date.

Is there the possibility to merge both codes and according option execute the part of the code desided.

Like if i want to add the date to the filename choose one option and if i want to remove the date other option.

It is use in daily basis as there some changes in scripts and also to generate a daily backup for some scripts.

Code to add the date to the end of the filename

D=$(date --iso)
for F in *tt*
do 
   Dot="${F//[^\.]/}" # this removes anything but a dot
   if [ -n "$Dot" ]; then
      mv "$F" "${F%.*}-$D.${F##*.}"
   else
      mv "$F" "$F-$D"
   fi
done

Code to remove the date from the filename

for file in *tt*
do
  chmod 777 $file
  mv "$file" "${file%???????????}"
done

Actually i have in separate scripts, then i am looking the possibility to merge boths.

Thanks in advance.

jas
  • 10,715
  • 2
  • 30
  • 41
OXXO
  • 724
  • 5
  • 12
  • 1
    `chmod 777`? WTF? Generally speaking, that should **never** be done -- setting anything both world-writable and executable is an easy way to let an attacker who's taken over an intentionally-unprivileged account like `nobody` modify files that privileged users can execute. – Charles Duffy May 30 '18 at 21:56
  • 2
    ...anyhow, what part do you have a question about? Is it parsing command-line options to set a flag/variable, or running different code depending on whether a variable is set? We have lots of Q&A entries already in the knowledgebase about the former; the latter basically comes down to "how do I use `if`?". – Charles Duffy May 30 '18 at 21:58
  • ...if the question is how to check for a command-line flag, f/e, then this could be answered by https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash – Charles Duffy May 30 '18 at 21:59
  • Charles, appreciate your comments. I will figure out my issue. – OXXO May 31 '18 at 06:35

1 Answers1

2

You could use a case to check a flag, like this:

funct() {
  case "$1" in
    -a|--add)
      Dot="${2//[^\.]/}" # this removes anything but a dot
      if [ -n "$Dot" ]; then
        mv "$2" "${2%.*}-$D.${F##*.}"
      else
        mv "$2" "$2-$D"
      fi
      ;;
    -d|--delete)
      mv "$2" "${2%???????????}"
      ;;
    *)
      printf "Usage: script -a|-d file\n" > /dev/stderr
      break
      ;;
  esac
}

Please, note that this is a very static script, I just copied your two functions, even if I think there are mistakes. For example, if you find that a file contains at least a dot, you will replace his extension with the date.

This way, you will lose the extension and you have to manually put it back after removing the date.

Also note that this little function is using the first and the second parameter only: I thought you would like to write this function in your .bashrc file (or .bash_aliases if you use it!), but with some minor fix, you could use this function even in a bigger script [:

EDIT:

You could think to just add something as extension, to know that:

  • is a backup;
  • when you did it.

Preserving the rest of function I would do something like:

funct() {
  case "$1" in
    -a|--add)
      str=$(date +.bak_%Y%m%d_%H%M%S)
      mv "$2" "$2$str"
      ;;
    -d|--delete)
      mv "$2" "${2%%.bak*}" 2> /dev/null || printf "$2 is not a backup.\n"
      ;;
    *)
      printf "Usage: script -a|-d file\n" > /dev/stderr
      break
      ;;
  esac
}
ingroxd
  • 995
  • 1
  • 12
  • 28
  • Ingroxd, Tks a lot for the help. Yes, I am aware about the problem to replace the extension with date, i tried to fix it but i can't..yes i use the aliases then i will try to use this function as your advice. – OXXO May 31 '18 at 18:41
  • I think would be better to only put date in head or in tail of the filename and just delete it. Something like file.ext.date would just suffice. – ingroxd Jun 01 '18 at 16:25
  • ingroxd, yes that i think will be the best.. Kindly can u adapt your code like your suggestion. I will do it here in my side.. – OXXO Jun 17 '18 at 02:29
  • Do you mean adapt it in order to be used in another script? The form it will take depends on what you might like... If, for example, you want to provide more than a file in a single class, you'll have to cicle `${@:2}`. If you meant to adjust the extension error, I already had it fixed. – ingroxd Jun 17 '18 at 05:23
  • ingroxd, I want to adapt the code to rename all scripts in a bin folder for my user. ( all files inside of this folder are scripts) and as the main reason of the code is to save a backup with the actual date. BTW when I tried to use the script like this script -a|-d file I got an error -d: command not found. – OXXO Jun 18 '18 at 23:33
  • @OXXO I think I didn't understand your requests... Please, note that the code above is not an entire script, it's just a function. In order to use it elsewhere, you need to do some minor fix, at least – ingroxd Jun 19 '18 at 05:35
  • Ingroxd, I will try to adapt the code in one script.. But why when I try to use it as script -a|-d file. I got the msg: error -d: command not found.. – OXXO Jun 20 '18 at 03:24
  • ingroxd, i have fixed my problem.. The code works perfect now. Many thanks for your help . – OXXO Jun 21 '18 at 18:49