unix - Moving all matching files containing spaces and appended different text with wildcards in bash -


i have directory generates report daily , i'm writing script move old report trash , rename before generating next report.

e.g. directory

file report - sat 16-01-2016.txt 

as files generated same constant 'file\ report - ' e.g.

file report - tue 12-01-2016.txt file report - wed 13-01-2016.txt file report - thur 14-01-2016.txt file report - fri 15-01-2016.txt 

i thought use bash code e.g.

mv -f ~/desktop/file\ report\*.txt ~/.trash/"old file report".txt 

however add word "old" front of file whilst keeping whatever day , date comes after. e.g.

file report - tue 12-01-2016.txt 

would become

old file report - tue 12-01-2016.txt 

i thought use variable , store file name in it. i'm not sure how code this.

old=$(echo ~/desktop/'file report - '*.txt) mv -f ~/desktop/file\ report\*.txt ~/.trash/"old "$old.txt 

i know wrong syntax. i'm reading few of man pages including find see if better grabbing file name store variable.

this basic shell loop:

for report in ~/desktop/'file report'*.txt;   mv "$report" ~/.trash/"old ${report##*/}" done 

the loop variable contain file's full path; trim directory part destination file name shell's built-in ${variable##prefix} string replacement mechanism.

as aside, if wanted assign old name variable, don't need echo that.

old=$report 

you should not use uppercase variable names, reserved shell's own variables (path, ps1, etc).


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -