Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-23-2008, 06:23 PM
|
#1
|
|
Member
Registered: Oct 2006
Location: California, USA
Distribution: Antix 12, Mythbuntu 11.10, Kubuntu 11.10, but mostly Lubuntu 12.10
Posts: 476
Rep:
|
How can I rename a folder's content to the name of the folder itself?
Hello, i have one folder with about 500 subfolders, and each subfolder has one file in them. I want to make it so that each file is named the same as it's folder, but retain it's extention, be it .jpg, .exe, .rar, etc.
There is only one file per folder in every case.
Can someone point me to a quick bash script i could run on this?
Thanks so much!
|
|
|
|
07-24-2008, 03:55 AM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Using parameter substitution in BASH, you can loop over the list of files and build the mv command line, piece after piece:
Code:
#!/bin/bash
while read file
do
extension=.${file##*.}
dirname=${file%/*}
newname=${dirname##*/}
echo mv "$file" "$dirname/$newname$extension"
done < <(find folder -type f)
where folder is the path of the parent directory containing the subdirs. The code above works if all the files inside the subdirectories have an extension. If some file does not have an extension, you have to slightly modify the code, for example:
Code:
#!/bin/bash
while read file
do
basename=${file##*/}
if [ $(expr index "$basename" .) -gt 0 ]
then
extension=.${file##*.}
else
extension=""
fi
dirname=${file%/*}
newname=${dirname##*/}
echo mv "$file" "$dirname/$newname$extension"
done < <(find folder -type f)
I put an echo in front of the mv commands for testing purposes. If the echoed commands satisfy your needs, strip out the echo and launch the script again to actually rename the files.
Also look at the Advanced Bash Scripting Guide, in particular parameter substitution and process substitution for future reference. Feel free to ask any further explanation about my code.
|
|
|
|
07-24-2008, 11:24 AM
|
#3
|
|
Member
Registered: Oct 2006
Location: California, USA
Distribution: Antix 12, Mythbuntu 11.10, Kubuntu 11.10, but mostly Lubuntu 12.10
Posts: 476
Original Poster
Rep:
|
I changed "folder" to the folder path and the script took the files out of their folders and renamed them to the same name, deleting them as it went.
I don't want to risk losing any more files, i'll just move them by hand.
Thanks for trying.
|
|
|
|
07-24-2008, 11:40 AM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Quote:
Originally Posted by xmrkite
I changed "folder" to the folder path and the script took the files out of their folders and renamed them to the same name, deleting them as it went.
|
The scripts - exactly as I posted them - do nothing, unless you have stripped out the echo command before testing. They simply calculate some shell variables and print the resulting command line to the standard output, without actually execute it.
|
|
|
|
07-25-2008, 12:37 AM
|
#5
|
|
Member
Registered: May 2007
Posts: 141
Rep:
|
No need to write such a long script! Just a single cmd!
Requirement: All your file names DO NOT contain the character dot, "." except before the file extension.
If this is satisfied, go to the directory which contains all your 500 subfolders, and type the following,
Quote:
|
ls */* | sed 's@\(.*\)/\([^.]*\)\(.*\)@mv \0 \1/\1\3@'
|
NOTE: This does not performing the renaming at once. It shows only the mv cammands! So, don't worry that anything would be changed!
You should then see 500 commands of mv something into what you want. If no problems for the 500 commands, type
Quote:
|
ls */* | sed 's@\(.*\)/\([^.]*\)\(.*\)@mv \0 \1/\1\3@' | sh
|
Piping this into "sh" will execute all the above commands!
Hope this help!
|
|
|
|
07-25-2008, 01:53 AM
|
#6
|
|
Member
Registered: May 2007
Posts: 141
Rep:
|
By the way, you better copy and paste the command. A single missing spacebar may cause it not work. Besides, if your file names contain dot except before the file extension, please post here and I may solve your problem.
|
|
|
|
07-25-2008, 03:33 AM
|
#7
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Quote:
Originally Posted by lawrence_lee_lee
No need to write such a long script! Just a single cmd!
|
Yeah, maybe you're right. But a little longer shell script is easier to understand and to modify. On the other hand, the OP accidentaly removed some files while using a script which actually did nothing. Using your command line he could delete the root filesystem! 
|
|
|
|
07-25-2008, 12:28 PM
|
#8
|
|
Member
Registered: Oct 2006
Location: California, USA
Distribution: Antix 12, Mythbuntu 11.10, Kubuntu 11.10, but mostly Lubuntu 12.10
Posts: 476
Original Poster
Rep:
|
OK, here's why the files got removed. I had some files in the main directory which had all the subfolders. I ran the original script, which didn't do anything but give me output on the command line to what commands would be done. I guess I missed a few lines, cause i didn't notice that the command would rename all my files in the original directory too. I then removed the echo command and ran the script.
Those original files all got deleted because they were renamed to the same thing, and as the process happened, they just overwrote each other. The files in the subdirectories worked out great, those filenames are fine.
So after looking it over a little more, i got what i wanted, and because i didn't think about the files that were in the main directory, i got what i deserved
I did not have a backup of those files in one place, so i'll have to spend some time to figure out what is now missing and then go and get them from past backup files...sucks, but i learned a good lesson.
-Thanks
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:14 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|