Replacing .dat.gz to .txt.gz in filename

Hi,

I tried below method;

mv -v /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz \
$(echo  /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz | tr 'dat' 'txt');

nfame variable has the string "dat" .

I need to rename files like below;

ASIS: 20140902103700_100319.dat.gz
NEEDED: 20140902103700_100319.txt.gz

how can I do this?
thanks in advance;

Hello,

Welcome to the forum, please use code tags for commands and codes.
Following example may help you in same.

Let us say we have a file named filedat , then we have saved dat value in a variable named nfame .

nfame="dat"
cat file$nfame
23
44
21
 
mv file$nfame $(echo file$nfame | awk -vfame="txt" '{gsub(/dat/,fame,$0); print $0}')
 
file named filedat has been changed to filetxt now. 
ls  "filetxt"
filetxt

Thanks,
R. Singh

1 Like

with keep the logic of snr_silencer, you forget the double quote around command echo

"$(echo $nfname.gz | tr 'dat' 'txt')"
1 Like

I see no reason why it should not work except for in your code you declared nfname but then you say:

which is not quite the same...

thanks for your answers. I found an interesting result.

Now, I wonder why am I getting this error below. I give the path /oracle but it sees the path /orxcle1;

+ echo /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_20140902112833_110365.dat.gz
+ mv -v /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_20140902112833_110365.dat.gz /orxcle1/scr/tilki/willsenttilkiNew/VOICE-MO_20140902112833_110365.txt.gz
`/oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_20140902112833_110365.dat.gz' -> `/orxcle1/scr/tilki/willsenttilkiNew/VOICE-MO_20140902112833_110365.txt.gz'
mv: cannot move `/oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_20140902112833_110365.dat.gz' to `/orxcle1/scr/tilki/willsenttilkiNew/VOICE-MO_20140902112833_110365.txt.gz': No such file or directory

When I remove full path, and using cd command, it did not give error.

cd /oracle1/scr/tilki/willsendtilkiNew
gzip VOICE-MO_$nfname
mv -v VOICE-MO_$nfname.gz $(echo VOICE-MO_$nfname.gz | tr 'dat' 'txt');
cd -

If you are trying to change the string ".dat" to ".txt" in a filename, tr is NOT the way to do it. The command:

tr 'dat' 'txt'

doesn't just change the string dat to the string txt , it changes every occurrence of d to t and every occurrence of a to x .

If you want to move all files in the directory /oracle1/scr/tilki/willsendtilkiNew/ whose names end with .dat.gz to have names with the same base but ending in .txt.gz , try something like:

for fn in /oracle1/scr/tilki/willsendtilkiNew/*.dat.gz
do      mv -v "$fn" "${fn%.dat.gz}.txt.gz"
done
1 Like

thanks for all answers. Especially to Don Cragun for tr command...

But I still wonder how to do it one by one. I mean not using a for/while loop.

mv -v "$fn" "${fn%.dat.gz}.txt.gz"

For example; how can I put full path into the code above?

If you're unwilling to use:

fn="/oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz"
mv -v "$fn" "${fn%.dat.gz}.txt.gz"

you could use something like:

mv -v /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz \
"$(echo  "/oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz" |sed 's/\.dat\.gz$/.txt.gz/')"

but, instead of just needing your shell and invoking mv ; it uses your shell, a subshell (to run echo and sed ) in the pipeline to process the destination pathname and invocations of both mv and sed .

1 Like

that's the answer. Thanks