- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- [Q] How can I rename all files with partially repe...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 02:11 AM
02-04-2006 02:11 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 02:51 AM
02-04-2006 02:51 AM
Solutioncd /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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 02:53 AM
02-04-2006 02:53 AM
Re: [Q] How can I rename all files with partially repeated filename ??
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 02:54 AM
02-04-2006 02:54 AM
Re: [Q] How can I rename all files with partially repeated filename ??
echo mv $MYFILE "${CHR7}00"
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 03:17 AM
02-04-2006 03:17 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 08:34 PM
02-04-2006 08:34 PM
Re: [Q] How can I rename all files with partially repeated filename ??
Try the awk construct below:
# ls -1
hope it helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2006 12:31 AM
02-05-2006 12:31 AM
Re: [Q] How can I rename all files with partially repeated filename ??
# 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 ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2006 04:56 AM
02-05-2006 04:56 AM
Re: [Q] How can I rename all files with partially repeated filename ??
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