-3

I want to move(archive) the multiple files from say directory /users/wahasan to /users/wahasan/old. The archive files should be autodated.

files are of the format CHKBOI.pos, CHKUTI.pos, CHKSBI.pos,so on. here CHK and .pos are common in every file name.

I need a single shell script that should move all the files from source directory to destination with date-stamp.

3
  • And the date is the date of creation? today? date of last modification? Have you tried something? Commented Dec 16, 2014 at 10:21
  • Hi waseem hasan. Please edit your question to add some details on what you have tried, what the results were, how those results differed from what you want, and as specifically as possible what you need to know in order to proceed. At the time it is hard to tell exactly what you are asking for. Note that the Unix & Linux Stack Exchange is not a script writing service; we aim to help people coming across these questions in the future as well, potentially years from now, which means that we prefer questions which are likely to help future visitors as well as the original asker. Commented Dec 16, 2014 at 10:30
  • Yes fredtantini. If i move the file today, then file should be appended with today's date. i am a beginner in Unix shell Scripting. Commented Dec 16, 2014 at 12:00

1 Answer 1

0

Pure BASH version:

dir='/users/wahasan';stamp=$(date +%Y%m%d_%H%M%S); for file in $dir/CHK*.pos; do nn=${file/CHK/old/CHK}; nn=${nn/pos/pos_$stamp}; echo "$file  --->  $nn";mv $file $nn; done

try it, should work

ED:

dir='/users/wahasan';          ## path to work directory
stamp=$(date +%Y%m%d_%H%M%S);  ## date_time stamp
for file in $dir/CHK*.pos; do  ## getting list of all files to be moved
    nn=${file/CHK/old/CHK};    ## generating new absolute path [1] (adding '/old/')
    nn=${nn/pos/pos_$stamp};   ## generating new absolute path [2] (adding stamp)
    echo "$file  --->  $nn";   ## showing what will be moved to what (can be removed)
    mv $file $nn;              ## moving
done
1
  • Thanks Netikras, i used the below script and it worked fine: #!/bin/sh TODAYS_DATE=date +%Y%m%d; export TODAYS_DATE for file in ls /u001/users/autosys/wahasan/CHKNB*.pos #while [ "$file" != "CHKNB*.POS" ] do mv $file /u001/users/autosys/wahasan/old/basename $file.$TODAYS_DATE done Commented Dec 16, 2014 at 14:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.