Operating System - HP-UX
1844839 Members
2750 Online
110233 Solutions
New Discussion

[Q] How can I rename all files with partially repeated filename ??

 
SOLVED
Go to solution
Tony, Lim
Frequent Advisor

[Q] How can I rename all files with partially repeated filename ??

I am usually gathering license usage log file using cronjob.
So those files have partially repeated file name like this..

./0126_1105
./0126_1405
./0126_1605
./0127_0905
./0127_1005
./0127_1105
...

this file is text format without any extension.

As you can see, the end of file is xxxx-xxx05. And now I should replace that name 05 to 00.

How can I rename all files in same directory without manually renaming one by one to use "mv" command.

It is alright using awk, sed or whatever.

Maybe using shell is better. I guess it is more comvenient. but I'm not good at shell programming.

Any method is OK. And I will try to solve this rename issue.

please let me know how can I successfully rename all files.

thanks in advance
7 REPLIES 7
Bill Hassell
Honored Contributor
Solution

Re: [Q] How can I rename all files with partially repeated filename ??

If and only if all the files are 9 characters and always have the underscore _ at character position 5, you can do it quite easily with a shell script. First, verify that this command exactly matches all the needed filenames:

cd /whatever_directory
echo ./????_??05

What this does is to match all 9 character filenames with _ in the middle and 05 on the end. If correct, make a backup of all the files in the directory, then use this script:

for MYFILE in ./????_??05
do
CHR7=$(echo $MYFILE | cut -c 1-7)
echo mv $MYFILE "${CHR7}05"
done

Now the above script has echo in front of the mv for testing. Run the script and check the output. It should produce a list of all the changes. If OK, remove the echo and let it run. NOTE: this works on a reasonable number of files (several dozen). If there are thousands of files, the technique will need modification to handle extremely long lists.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: [Q] How can I rename all files with partially repeated filename ??

Hi Tony:

If you file of filenames consists of one filename per line, then this will change the name of any file ending with "05" to one with the "05" changed to "00".

The change is only made at the end of the filename.

# cd your_directory
# perl -ne 'chomp;next unless m%05$%;$old=$_;s%05$%00%;system("mv $old $_")' files_to_rename

If your 'files_to_rename' list of names specifies absolute path names, then there is no need to do the 'cd your_directory'.

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: [Q] How can I rename all files with partially repeated filename ??

Whoops, one typo. The mv line should end with 00 and not 05. This is the reason to run a test before the actual change. It should read:

echo mv $MYFILE "${CHR7}00"


Bill Hassell, sysadmin
Hein van den Heuvel
Honored Contributor

Re: [Q] How can I rename all files with partially repeated filename ??



Variation on the perl solution using an 'implied' GLOB function:

perl -e "while (<*-*05>) {$old=$_; s/05$/00/; rename $old, $_}"


fwiw,
Hein.
Sandman!
Honored Contributor

Re: [Q] How can I rename all files with partially repeated filename ??

Hi Tony,

Try the awk construct below:

# ls -1 | awk '{l=length()-2;x=substr($0,1,l);system("mv "$0" "x"00")}'

hope it helps!
Tony, Lim
Frequent Advisor

Re: [Q] How can I rename all files with partially repeated filename ??

As Bill hassell mentioned before, belows are helpful to me.

# for MYFILE in ./????_??05
>do
>CHR7=$(echo $MYFILE | cut -c 1-7)
>mv $MYFILE "${CHR7}00"
>done
#

But, ./????_??05 and cut -c 1-7 have some troubles.

character 1-7 included "./" so it resulted like this.

./????_00 not ./????_??00

What command is being needed to change without previous troubles ??
Bill Hassell
Honored Contributor

Re: [Q] How can I rename all files with partially repeated filename ??

Sorry about that. I just copied your filename. Remove the ./ from the for statement as in:

for MYFILE in ????_??05

./ is normally used for executables in the current directory. The complete script should look like this:

cd /your_directory
for MYFILE in ????_??05
do
CHR7=$(echo $MYFILE | cut -c 1-7)
echo mv $MYFILE "${CHR7}00"
done

As mentioned, you can also use the full pathname (such as /var/opt/my_program/????_??05) but you would then change the cut command to specify all but the last 2 characters. A production script would accept the directory on the command line, validate that the directory exists, then validate that there is at least one file that matches the 9-character mask for the filename, and so on...


Bill Hassell, sysadmin