I need help creating a script allowing one file to move to another folder and when moved the date and time should be appended to the end of the file name.

Please help thank you
FS

mv /from/file.txt to/file.txt`date +'+%Y'`

Note the difference between back-ticks(`) and single-quotation marks(')

Thanks.

Also the number of files applied in the argument need to be limitless there must be 2 options ([-i] ans [-h]) the first being a print confirmation asking if you really want to move the file. The second being a print helper function that tells you how to use the programme.

Thank you

I just cant figure out how to turn this into a code.

Thanks.

Also the number of files applied in the argument need to be limitless there must be 2 options ([-i] ans [-h]) the first being a print confirmation asking if you really want to move the file. The second being a print helper function that tells you how to use the programme.

Thank you
I just cant figure out how to turn this into a code.

Well you said "help", so I took it you wanted a few clues and then you would post some kind of attempt.

IMO, the essence of the problem is solved. All you need to do is tart it up a bit and the job is done.

its the tarting up bit i dont get.

when it comes to writing code i just go blank. i can read it and understand it fine but i can't write it... it just gets confusing.

Thanks for what you've helped me with anyway.

Yeah. Programmers write programs. Newbies modify programs. Students write snippets. I'm feeling bored, so I wrote this snippet. I use bash. If you use ksh, change all the [ ... ] to [[ ... ]] , and see what happens. You may also need to declare local variables some other way? My ksh is getting rusty.

There are a couple of pretty easy ideas here:
Idea 1: Use functions, just like you would in other programming languages (but, bash is interpreted, not compiled, so you can only use them after they are seen)

Idea 2: Allow the user to customize the behavior. In this case, I've deliberately reversed a standard unix best practice that programs should be quiet unless requested otherwise: If you want things quiet, invoke the program with the --quiet or -q flag.

To play with this, copy the code into a file named 'mover' on your PATH; and chmod +x that file.

Enough with the editorial.

#!/bin/bash
# This function is called as needed from other places
function mover_help() {
  cat<<EOF 
Usage: mover [-i][-q][-h] -t directory files to move
Moves specified file(s) to specified directory and appends a date-time string to the moved file.
 -i or --inquire: Inquire whether to actually move each file
 -q or --quiet: Just quietly do it if possible, else quietly do not
 -h or --help: Print this and quit
 -t directory or --target=directory: Move files to this directory
 files to move: one or more files (wild cards accepted)
EOF
}

# dump failure messages onto stderr
function fail() {
  echo "$*" 1>&2
  mover_help 1>&2
}

# we call this from moveone in a couple of places
function domove() {
  # man date looking for 'format' to see what this does
  local target=$(date +$2/$1_%Y-%m-%d_%H-%M-%S)
  mv $1 $target
}

# the heart of the program  
function moveone() {
  if [ $# -lt 2 ]; then
    fail "moveone requires source file and target directory"
  fi
  local source=$1
  local target=$2
  shift; shift
  local inquire=''
  local quiet=''
  while [ $# -gt 0 ] ; do
    if [ 'i' = "$1" ] ; then
      inquire='t'
    elif [ 'q' = "$1" ] ; then
      quiet='t'
    else
      fail "moveone does not understand parameter value of $1"
    fi
    shift
  done
  if [ ! -f $source ] ; then
    if [ -z "$quiet" ] ; then
       echo "Not moving non-existent file $source" 1>&2
    fi
  elif [ -n "$inquire" ] ; then
    echo -n "Move $source to $target? "
    read ans
    case $ans in
    [yY]*) domove $source $target ;;
    *) if [ -z "$quiet" ] ; then echo "Not moving $target" ; fi ;;
    esac
  else
    domove $source $target
  fi
}

## "main" starts here
target=$HOME
# target not checked below. In the real world it would be via [ -d $target ] etc
quiet=''
inquire=''
sources=''
while [ $# -gt 0 ] ; do
  case $1 in
  -h*|--h*) mover_help; exit 0 ;;
  -i|--i*) inquire=i; shift ;;
  -q|--q*) quiet=q; shift ;;
  -t) shift; target=$1; shift ;;
  --targ*) target=$(echo $1|cut -d'=' -f2); shift ;;
  *) sources="$sources $1" ; shift ;;
  esac
done
for source in $sources ; do
  moveone $source $target $inquire $quiet
done
Comments
Nice categorisation :)

> its the tarting up bit i dont get.
Well since you've failed to make any code contribution at all (no matter how confused you might be), I can only conclude your definition of "help" was to get someone else to do all the work for you, while you rolled around and played dumb.

But if you've any ambition of actually learning how to do this, you need to actually start trying and failing (then posting on a forum to learn).

This article has been dead for over six months. Start a new discussion instead.