I have multiple files in folder which i want to rename. hence I am using the below command in my script by I get an error:
export XXX_LOG_DIR="${LOG_DIR}/${XXX_HOST}/xxx/${REPORT_DATE}"
mv $XXX_LOG_DIR/*.audit.gz $XXX_LOG_DIR/*.audit.log.gz
But I get the below error:
mv: target `xxxxx.......' is not a directory
Can you please help?
vbe
February 9, 2016, 3:29am
2
You cant rename ( mv) multiple files in one go... the syntax says multiples files to a directory or file to newfile...
You will have to write a script using a loop...
Do I need to use for loop then ?
gull04
February 9, 2016, 3:59am
4
Hi Karan,
Yes you probably would be better using a script or loop to do this, as an aside it is possible to move multiple files using the mv
command as follows.
-bash-3.2$ ls Uploads/p*
Uploads/parser.exp Uploads/phantom.html Uploads/pr.nawk Uploads/preparser.sh
Uploads/pdf2html.pl Uploads/pr.gawk Uploads/prair
-bash-3.2$ mv Uploads/p* uploads/
-bash-3.2$ ls Uploads/p*
Uploads/p*: No such file or directory
-bash-3.2$ ls uploads
parser.exp pdf2html.pl phantom.html pr.gawk pr.nawk prair preparser.sh
-bash-3.2$ ls Uploads/h*
Uploads/hexgen.scr
-bash-3.2$
It is essential that the destination is a directory.
Regards
Gull04
drl
February 9, 2016, 8:18am
5
Hi.
Some possibilities, some of which depend on OS:
Rename multiple files, groups of files
1) rename -- Debian version and RedHat version differ, q.v.
(try package util-linux:
http://en.wikipedia.org/wiki/Util-linux)
2) ren -- RedHat relatives
3) renameutils -- package contains qmv, imv, icp, qcp, and deurlname
4) mved -- (circa 2006; good as of 2015.05), perl
http://raf.org/mved/
(An earlier shell version may be available.)
5) rename -- perl builtin library routine (DIY)
See man and web pages for details ... cheers, drl
If you download/compile/install mmv, then you can do
mmv "$XXX_LOG_DIR/*.audit.gz" "$XXX_LOG_DIR/#1.audit.log.gz"
The #1
refers to the first wildcard, here *
.
--
Here is a shell loop tailored for your specific task
for file in $XXX_LOG_DIR/*.audit.gz
do
[ -f "$file" ] || continue
chop_end=${file%.audit.gz}
dest=${chop_end}.audit.log.gz
mv "$file" "$dest"
done