- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- One-liner challenge: renaming an entire directory ...
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
03-29-2004 08:20 PM
03-29-2004 08:20 PM
Goal:
Suppose you have a directory tree.
Suppose you want to replace all underscore ("_") characters in file or directory names with -let's say- the commercial 'at' ("@") character.
Pitfalls:
- Best practice would be processing each directory before descending into the directory tree.
- If you don't, when renaming a file/directory inside a directory that was renamed just before that, you may run into "file not found" errors (as I did...).
Here's something to get you started:
for i in $(find . -type f -name "*_*") ; do echo $i ; mv $i $(echo $(fname -d $i)/$(fname -b $i | sed 's/_/@/')) ; done
If I was able to sort the find list by "longest string in the top of the list", the problem of "file not found" would be bypassed. Not too happy about this script though...
Next step would be to also update the contents of files that meet certain criteria.
Hope someone has encountered this problem/challenge before...
Cheers,
Harald
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2004 08:32 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2004 09:20 PM
03-29-2004 09:20 PM
Re: One-liner challenge: renaming an entire directory tree
Anyone got some good old c-scripting or perhaps throw in some awk or tcl ?
Thanks for taking the time to respond to my post...that was really quick !
Cheers,
Harald
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2004 09:21 PM
03-29-2004 09:21 PM
Re: One-liner challenge: renaming an entire directory tree
find . -type f -name "*_*"|while read FNAME; do
NNM=$(basename $FNAME|sed 's/_/@/g')
mv $FNAME $(dirname $FNAME)/$NNM
done
If you need to include directories into the procedure, use the option -depth
find . -depth -name ..... etc
-depth: A position-independent term which causes descent of the directory hierarchy to be done so that all entries in a directory are acted on before the directory itself.
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2004 09:45 PM
03-29-2004 09:45 PM
Re: One-liner challenge: renaming an entire directory tree
I like the use of variables.
I like using multiple lines over a single line.
But I definitely really like the "-depth" option for the find command.
find . -name "*_*"
./a_a
./a_a/a_a ---> file not found as dir was renamed previously ...
./a_a/a_a/a_a
./a_a/a_a/a_aa
./a_a/a_a/a_aaa
./a_a/a_a/a_aaaa
./a_a/a_aa
./a_a/a_aaa
./a_a/a_aaaa
./a_aa
./a_aaa
./a_aaaa
find . -name "*_*" -depth
./a_a/a_a/a_a
./a_a/a_a/a_aa
./a_a/a_a/a_aaa
./a_a/a_a/a_aaaa
./a_a/a_a
./a_a/a_aa
./a_a/a_aaa
./a_a/a_aaaa
./a_a
./a_aa
./a_aaa
./a_aaaa
Thank you all for your help; this has been a most enlightening scripting course.