Hi Gurus,
i have files like this and i want to rename it.
server1_0_Log0000597500
server1_0_Log0000597501
server1_0_Log0000597502
server1_0_Log0000597503
server1_0_Log0000597504
server1_0_Log0000597505
server1_0_Log0000597506
server1_0_Log0000597507
server1_0_Log0000597508
server1_0_Log0000597509
server1_0_Log0000597510
output should be like this:
mv server1_0_Log0000597500 server2_0_Log0000597500
mv server1_0_Log0000597501 server2_0_Log0000597501
mv server1_0_Log0000597502 server2_0_Log0000597502
mv server1_0_Log0000597503 server2_0_Log0000597503
mv server1_0_Log0000597504 server2_0_Log0000597504
mv server1_0_Log0000597505 server2_0_Log0000597505
mv server1_0_Log0000597506 server2_0_Log0000597506
mv server1_0_Log0000597507 server2_0_Log0000597507
mv server1_0_Log0000597508 server2_0_Log0000597508
mv server1_0_Log0000597509 server2_0_Log0000597509
mv server1_0_Log0000597510 server2_0_Log0000597510
can you please help be on a script to do it?
thanks
What operating system are you using?
What shell are you using?
What have you tried to solve this problem?
Aia
March 23, 2016, 11:51pm
3
Hi fedora132010,
Assuming that you have a file.list with either the full path or you are executing the following command in the directory where these files exist.
perl -MFile::Copy=move -nle '$old = $_; s/server1/server2/ and move $old, $_' file.list
Here is bash script to move those files and rename them
for i in $(ls); do mv $i ${i/${i:0:7}/server2}; done
It replaces the string between the 0th and the 7th character with "server2".
drl
March 24, 2016, 6:19pm
5
Hi.
And a laundry list of items to consider:
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)
6) mmv -- move/rename/copy/append/link multiple files by wildcard patterns
7) gprename - batch rename using a GUI
8) krename - batch rename using a GUI
Best wishes ... cheers, drl
nemesis911:
Here is bash script to move those files and rename them
for i in $(ls); do mv $i ${i/${i:0:7}/server2}; done
It replaces the string between the 0th and the 7th character with "server2".
That is a very dangerous suggestion. Note that if a directory contains files such as:
-rw-r--r-- 1 dwc staff 1082 Mar 23 21:11 _x.txt
-rw-r--r-- 1 dwc staff 1099 Mar 24 10:09 out
-rw-r--r-- 1 dwc staff 2321 Mar 23 21:29 problem
-rwxr-xr-x 1 dwc staff 806 Mar 24 10:09 tester
-rw-r--r-- 1 dec staff 1082 Mar 24 10:10 x.txt
in addition to files with names starting with the string server1
, it will execute commands like:
mv _x.txt server2
mv out server2
mv problem server2
mv tester server2
mv x.txt server2
throwing away the contents of 4 our of 5 of those files.
A much safer (and, since there is no need to invoke ls
, faster) way to do this would be:
for i in server1*; do printf '%s %s %s\n' mv "$i" "${i/1/2}"; done
to verify that it is producing the mv
commands you want and then rerun it with the printf
and its format string shown in red removed after you have verified that it will be processing the files you want it to move correctly.
kshji
March 25, 2016, 12:54pm
7
for i in server1*
do
[ "$i" = "server1*" ] && continue # there wasn't any files
# remove echo if looks good
echo mv "$i" "${i/server1/server2}"
done
If there is any possibility that there might not be any matching files, adding the test for no matching files is a great idea. But:
${i/1/2}
expands to the contents of $i
with the first occurrence of 1
changed to 2
; not all occurrences.
The syntax to replace all occurrences of a pattern with a replacement string is:
${i//pattern/replacement}
The syntax to replace an occurrence of a pattern only if it is at the start of the expansion is:
${i/#pattern/replacement}
The syntax to replace an occurrence of a pattern only if it is at the end of the expansion is:
${i/%pattern/replacement}
For example:
$ x=1x1x1
$ echo ${x/1/2}
2x1x1
$ echo ${x//1/2}
2x2x2
$ echo ${x/#1/2}
2x1x1
$ echo ${x/%1/2}
1x1x2
$
None of these parameter expansions are in the standards. But they all work in 1993 or later versions of ksh
and in recent versions of bash
.