HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- shell script to copy and change the pattern of the...
Operating System - HP-UX
1839589
Members
2402
Online
110151
Solutions
Forums
Categories
Company
Local Language
back
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
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- 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
11-04-2003 08:21 AM
11-04-2003 08:21 AM
shell script to copy and change the pattern of the filename
I 'm new to shell scripting . I would like to do the following . There are 2 files in a directory like
2003_4_56_20:1:test.txt, 2003_10_23_16:1:2_test.txt. I would like all the colons to change to underscore, ie for 2003_4_56_20:1:test.txt it should be 2003_4_56_20_1_test.txt . The original file should not be overwritten, only the filename has to change. How do I go about doing this?
John
2003_4_56_20:1:test.txt, 2003_10_23_16:1:2_test.txt. I would like all the colons to change to underscore, ie for 2003_4_56_20:1:test.txt it should be 2003_4_56_20_1_test.txt . The original file should not be overwritten, only the filename has to change. How do I go about doing this?
John
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2003 08:26 AM
11-04-2003 08:26 AM
Re: shell script to copy and change the pattern of the filename
ls | while read OLDNAME
do
NEWNAME=$(echo "${OLDNAME}" | tr ":" "_")
if [[ "${NEWNAME}" != "${OLDNAME}" ]]
then
mv ${OLDNAME} ${NEWNAME}
fi
done
do
NEWNAME=$(echo "${OLDNAME}" | tr ":" "_")
if [[ "${NEWNAME}" != "${OLDNAME}" ]]
then
mv ${OLDNAME} ${NEWNAME}
fi
done
If it ain't broke, I can fix that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2003 08:32 AM
11-04-2003 08:32 AM
Re: shell script to copy and change the pattern of the filename
How about-
#!/usr/bin/ksh
for x in *:* ; do
y=`echo $x | sed -e 's/:/_/g'`
if [[ -a $y ]] ; then
echo "not renaming $x - new name already exists"
exit
fi
mv "$x" "$y"
done
HTH
-- Rod Hills
#!/usr/bin/ksh
for x in *:* ; do
y=`echo $x | sed -e 's/:/_/g'`
if [[ -a $y ]] ; then
echo "not renaming $x - new name already exists"
exit
fi
mv "$x" "$y"
done
HTH
-- Rod Hills
There be dragons...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2003 11:26 PM
11-04-2003 11:26 PM
Re: shell script to copy and change the pattern of the filename
The attached script renames all files in a directory whose names match a given pattern (even one including blanks). In your case you might use something like
rename ".*:.*" "s/:/_/g"
to select all files with a colon in their name and to replace all colons by underscores.
rename ".*:.*" "s/:/_/g"
to select all files with a colon in their name and to replace all colons by underscores.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP