LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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



Reply
 
Search this Thread
Old 07-23-2008, 07:23 PM   #1
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 542

Rep: Reputation: 30
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!
 
Old 07-24-2008, 04:55 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 07-24-2008, 12:24 PM   #3
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 542

Original Poster
Rep: Reputation: 30
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.
 
Old 07-24-2008, 12:40 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by xmrkite View Post
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.
 
Old 07-25-2008, 01:37 AM   #5
lawrence_lee_lee
Member
 
Registered: May 2007
Posts: 141

Rep: Reputation: 16
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!
 
Old 07-25-2008, 02:53 AM   #6
lawrence_lee_lee
Member
 
Registered: May 2007
Posts: 141

Rep: Reputation: 16
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.
 
Old 07-25-2008, 04:33 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by lawrence_lee_lee View Post
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!
 
Old 07-25-2008, 01:28 PM   #8
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 542

Original Poster
Rep: Reputation: 30
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
 
  


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
Rename Folder in numerics infonlinebr Linux - Newbie 7 06-10-2008 03:02 PM
A script within a folder to delete the folder, script, and the folder's contents Cyberman Programming 15 10-17-2007 08:32 AM
rename last character in file/folder name BoraX Programming 9 10-26-2006 11:01 AM
ls content of a folder lonecrow Linux - Newbie 2 09-28-2006 07:12 PM
rename file/folder command hct224 Linux - Newbie 4 09-19-2003 10:44 AM


All times are GMT -5. The time now is 07:25 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration