Rename multiple files in one go

OS : Oracle Linux 6.8
shell : bash

As shown below, I have multiple files like below (query1-extract_aa, query1-extract_ab, query1-extract_ac, ....)

$ ls -l
total 235680
-rw-rw-r-- 1 reportusr reportusr  30M May  3 11:25 query1-extract_aa
-rw-rw-r-- 1 reportusr reportusr  30M May  3 11:25 query1-extract_ab
-rw-rw-r-- 1 reportusr reportusr  30M May  3 11:25 query1-extract_ac
-rw-rw-r-- 1 reportusr reportusr  30M May  3 11:25 query1-extract_ad
-rw-rw-r-- 1 reportusr reportusr  30M May  3 11:25 query1-extract_ae
-rw-rw-r-- 1 reportusr reportusr  30M May  3 11:25 query1-extract_af
-rw-rw-r-- 1 reportusr reportusr  30M May  3 11:25 query1-extract_ag
-rw-rw-r-- 1 reportusr reportusr  22M May  3 11:25 query1-extract_ah

I want to append .txt to the end of these filenames , so that

query1-extract_aa will be renamed to query1-extract_aa.txt
query1-extract_ab will be renamed to query1-extract_ab.txt
query1-extract_ac will be renamed to query1-extract_ac.txt
    .
    .
    .
etc...

How can I do this in bash in one go ?

You can't.
In bash you'll need a loop to rename every single file.
You may want to consider the (perl?) rename tool.

1 Like

My for loop (shown below) deleted all the files instead of renaming it :o

Any idea how I can do this correctly using a for loop (or any loop) ?
And how did my files get removed with the below mv command ?

$ ls -l
total 235680
-rw-rw-r-- 1 reportusr reportusr 31259250 May  3 11:25 query1-extract_aa
-rw-rw-r-- 1 reportusr reportusr 31198180 May  3 11:25 query1-extract_ab
-rw-rw-r-- 1 reportusr reportusr 31264556 May  3 11:25 query1-extract_ac
-rw-rw-r-- 1 reportusr reportusr 31246721 May  3 11:25 query1-extract_ad
-rw-rw-r-- 1 reportusr reportusr 31215875 May  3 11:25 query1-extract_ae
-rw-rw-r-- 1 reportusr reportusr 31172285 May  3 11:25 query1-extract_af
-rw-rw-r-- 1 reportusr reportusr 31189621 May  3 11:25 query1-extract_ag
-rw-rw-r-- 1 reportusr reportusr 22774687 May  3 11:25 query1-extract_ah
$
$
$ for i in query1-extract* ; do mv $i ${1}.txt; done
$ ls -lh
total 0
$

You used ${1} instead of ${i}

Andrew

1 Like

This is why you always do 'echo mv' on the first try. Better yet, take a backup before you do a batch rename. Consequences can be large.

1 Like

after the first for...mv run when all files disappeared there should be a file in the directory called .txt (since $1 was not set) which is a copy of the last file on the list ( query1-extract_ah ). ls -al should show it.

1 Like