LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-20-2011, 06:26 PM   #1
ytyyutianyun
Member
 
Registered: Nov 2011
Posts: 63

Rep: Reputation: Disabled
How to add a folder directory to rename the file


First ,I want to search the file I wanted
Then,rename the file according to the folder directory.
for example

./jizhen/wujizhen:
171
80
hupu.txt

./jizhen/wujizhen/171:
0000.txt
000100.txt
000101.txt
000102.txt
hupu.txt

then rename the "hupu.txt" to "hupujizhenwujizhen.txt" and "hupujizhenwujizhen171.txt"


Thanks
 
Old 11-20-2011, 06:50 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
Using Bash and GNU find:
Code:
find . -type f -name 'hupu.txt' -print0 | while read -rd "" old ; do
    new="${old%/*}"
    new="${new#./}"
    new="hupu${new//\//}.txt"
    mv -vi "$old" "$new"
done
The find . -type f -name 'pattern' -print0 will find all files in current directory and subdirectories that match glob pattern pattern, and output them using NUL (zero byte) as a separator. In this case, it looks for files named hupu.txt only.

The while read -rd "" old ; do ... done loop will read each NUL-separated file name in turn, into variable old. (Most shells do not support that read syntax, but Bash does.)

The new="${old%/*}" line copies everything from old up to but not including the last slash, into new. This copies the path part of old into new.

The new="${new#./}" line removes the leading ./ from new.

The new="hupu${new//\//}.txt" adds hupu to the start and .txt to the end, and removes all slashes. You can use e.g. The new="hupu${new//\//_}.txt" if you want to convert the slashes to underscores instead of just removing them.

The -v argument to mv makes it display what it is doing, and the -i argument makes it ask if you want to overwrite the target if it already exists.

If you do not want to move/rename the file but copy it, use cp instead of mv.

Last edited by Nominal Animal; 11-20-2011 at 06:52 PM.
 
Old 11-21-2011, 09:00 PM   #3
ytyyutianyun
Member
 
Registered: Nov 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Nominal Animal View Post
Using Bash and GNU find:
Code:
find . -type f -name 'hupu.txt' -print0 | while read -rd "" old ; do
    new="${old%/*}"
    new="${new#./}"
    new="hupu${new//\//}.txt"
    mv -vi "$old" "$new"
done
The find . -type f -name 'pattern' -print0 will find all files in current directory and subdirectories that match glob pattern pattern, and output them using NUL (zero byte) as a separator. In this case, it looks for files named hupu.txt only.

The while read -rd "" old ; do ... done loop will read each NUL-separated file name in turn, into variable old. (Most shells do not support that read syntax, but Bash does.)

The new="${old%/*}" line copies everything from old up to but not including the last slash, into new. This copies the path part of old into new.

The new="${new#./}" line removes the leading ./ from new.

The new="hupu${new//\//}.txt" adds hupu to the start and .txt to the end, and removes all slashes. You can use e.g. The new="hupu${new//\//_}.txt" if you want to convert the slashes to underscores instead of just removing them.

The -v argument to mv makes it display what it is doing, and the -i argument makes it ask if you want to overwrite the target if it already exists.

If you do not want to move/rename the file but copy it, use cp instead of mv.
Thanks,it's very helpful.Thanks

Last edited by ytyyutianyun; 11-21-2011 at 09:04 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add a folder/directory in the iso image of linux cd. udaypratapyati Linux - Newbie 5 02-25-2011 05:27 AM
Get first file of directory then copy to other directory andd rename the file Faye Linux - Software 4 01-30-2011 10:16 AM
Rename file to parent directory name ExWizzard Linux - Newbie 2 12-11-2009 05:19 PM
rename last character in file/folder name BoraX Programming 9 10-26-2006 11:01 AM
rename file/folder command hct224 Linux - Newbie 4 09-19-2003 10:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration