LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-11-2009, 05:07 AM   #1
EddieAdams
LQ Newbie
 
Registered: Mar 2006
Posts: 2

Rep: Reputation: 0
bashing files for folder names


hi,

long time listener, first time caller...

i'm nearly at the end of an extremely painful rescue of an lvm meltdown that was lost to hardware failure. on running a final e2fsck after removing the bad disk all the remaining files were promptly removed to the lost+found folder.

for the most part i can figure out what the folders should be called, based on the names of the files in the folders. for instance, folder lost+found/#50922532 contains a file called somethingawesome.avi, so the folder should be called lost+found/somethingawesome/

i'm in the midst of creating a bash script that does a 'find' on the lost+found subdirectories for certain file extensions. now, i can pull back some info with 'basename' and 'dirname' but i'm trying to figure out the best way to rename the folders once i know what they are to be called. all the folders in lost+found are given a name similar to #50922532.

any suggestions? a demonstration of the code i'm testing is below

Code:
#!/bin/sh

extension="mkv mpg avi"

for ext in $extension
do
	for thefile in `find . -maxdepth 4 -mindepth 2 -type f -iname *.$ext`
	do
		echo $thefile
		echo filename `basename $thefile .$ext`
		echo dirname `dirname $thefile`
	done
done
eddie.
 
Old 07-11-2009, 10:46 AM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Let's see if I understand your problem: You have one file in each directory and you want to rename the directory to the file name without the extension?

If that is the case, why not copy the file to a new location? Aren't you are going to do that eventually anyway? Something like:
Code:
newdir=/path/to/new_dir/$(basename $thefile .$ext)
mkdir $newdir
cp $thefile $newdir
 
Old 07-11-2009, 10:49 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello EddieAdams

I read that several times and still didn't gain a clear idea of what you want to do or what your specific question is.
Quote:
Originally Posted by EddieAdams View Post
on running a final e2fsck after removing the bad disk all the remaining files were promptly removed to the lost+found folder.
Maybe the details don't matter but if you think they do it would be helpful to explain what the LVM setup was, how the bad disk figured in it, how the bad disk failed, what file system you e2fscked and how that figured in it.

Quote:
Originally Posted by EddieAdams View Post
for the most part i can figure out what the folders should be called, based on the names of the files in the folders. for instance, folder lost+found/#50922532 contains a file called somethingawesome.avi, so the folder should be called lost+found/somethingawesome/
So what if the folder contains more than one file? How is it that, in general, programmatically, you can determine directory names from the name(s) of the files they contain?

Quote:
Originally Posted by EddieAdams View Post
i'm in the midst of creating a bash script that does a 'find' on the lost+found subdirectories for certain file extensions. now, i can pull back some info with 'basename' and 'dirname' but i'm trying to figure out the best way to rename the folders once i know what they are to be called. all the folders in lost+found are given a name similar to #50922532.

any suggestions? a demonstration of the code i'm testing is below

Code:
#!/bin/sh

extension="mkv mpg avi"

for ext in $extension
do
	for thefile in `find . -maxdepth 4 -mindepth 2 -type f -iname *.$ext`
	do
		echo $thefile
		echo filename `basename $thefile .$ext`
		echo dirname `dirname $thefile`
	done
done
eddie.
OK -- so you have a script that finds the full paths of every file with the listed extensions and parses out their directory path and basename.

Would I be right in wildly guessing that when you find a full path like ./lost+found/foo_dir/larkstonguesinaspic.avi that you want to rename directory ./lost+found/foo_dir/ as ./lost+find/larkstonguesinaspic/ ?

If that's the case then something like this (not tested) should do the trick
Code:
dirname="${dirname%/*}$basename
If you're looking for reference info on what that's (intended to be) doing, see http://www.gnu.org/software/bash/man...eter-Expansion

Best

Charles

Last edited by catkin; 07-11-2009 at 11:15 AM. Reason: Fix typos
 
Old 07-11-2009, 04:20 PM   #4
EddieAdams
LQ Newbie
 
Registered: Mar 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the replies.

I apologise if my original question seemed vague, I didn't want to get people side tracked on the LVM rescue.

Quote:
Would I be right in wildly guessing that when you find a full path like ./lost+found/foo_dir/larkstonguesinaspic.avi that you want to rename directory ./lost+found/foo_dir/ as ./lost+find/larkstonguesinaspic/ ?
Yes, that is what I am trying to do. However, it is not always the folder that the file is in that needs renaming though, it can sometimes be up the chain, e.g. /lost+found/foo_dir/bar_dir/larkstonguesinaspic.avi, /lost+found/foo_dir/bar_dir/ to be renamed /lost+found/larkstonguesinaspic/bar_dir/

In this respect, the one guarantee I have is that the folder to be renamed will always be in the form of
Code:
/#somenumber/
Quote:
So what if the folder contains more than one file?
The folder will probably contain more than one file, but if it contains a media file it will most probably be the name I am looking for. If there is more than one media file then any will do, I just need a better guide than /#12345.

Quote:
How is it that, in general, programmatically, you can determine directory names from the name(s) of the files they contain?
Before the great crash of '09 I had a very structured LVM with a hierarchy something like this:
mediashare/tv/someshow/someshow.avi
mediashare/tv/someshow/someshow.nfo
mediashare/tv/someshow/somefile.huh
mediashare/music/sometunes/CD1/sometune.mp3

After the crash, and if I am lucky, it now looks something like this:
lost+found/#123456/someshow.avi
lost+found/#123456/someshow.nfo
lost+found/#123456/somefile.huh
lost+found/#654321/CD1/sometune.mp3

Quote:
...you want to rename the directory to the file name without the extension?

If that is the case, why not copy the file to a new location? Aren't you are going to do that eventually anyway?
That's right, but as there is generally more than one file I want to keep the files together. I've already copied the folders to a new location, this is where the bash script will run.

Quote:
If that's the case then something like this (not tested) should do the trick
Code:
dirname="${dirname%/*}$basename
Yes, with a bit of refining that is what I am after.

Eddie.
 
Old 07-12-2009, 11:05 AM   #5
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
You're probably way past needing anything and there are probably better ways to do this, but I enjoy problems like this. Here is what I came up with for the data that you posted:
Code:
extension="mp3 avi"
for ext in $extension
do
	for filename in `find . -maxdepth 4 -mindepth 2 -type f -iname "*.$ext"`
	do
		dir=`dirname $filename` # obtain the directory path
		dest_dir=$dir"/"               # Target directory
		dest_dir=${dest_dir#*\/}   # remove ./ 
		dest_dir=${dest_dir#*\/}   # remove lost+found/
		dest_dir=${dest_dir#*\/}   #remove #654321/
		case $ext in
			mp3)
				dest_dir="./mediashare/music/"`basename $filename .$ext`"/"${dest_dir};;
			avi)
				dest_dir="./mediashare/tv/"`basename $filename .$ext`"/"${dest_dir};;
		esac
		mkdir -p $dest_dir    # -p create parent directories if not existing
		dir+="/*"       		 
		cp ${dir} $dest_dir	# expansion necessary because of the *	
	done
done
And the result:
Code:
$ \ls -lRA mediashare/
mediashare/:
total 8
drwxr-xr-x 3 norm norm 4096 2009-07-12 10:42 music
drwxr-xr-x 3 norm norm 4096 2009-07-12 10:42 tv

mediashare/music:
total 4
drwxr-xr-x 3 norm norm 4096 2009-07-12 10:42 sometune

mediashare/music/sometune:
total 4
drwxr-xr-x 2 norm norm 4096 2009-07-12 10:42 CD1

mediashare/music/sometune/CD1:
total 0
-rw-r--r-- 1 norm norm 0 2009-07-12 10:42 sometune.mp3

mediashare/tv:
total 4
drwxr-xr-x 2 norm norm 4096 2009-07-12 10:42 someshow

mediashare/tv/someshow:
total 0
-rw-r--r-- 1 norm norm 0 2009-07-12 10:42 somefile.huh
-rw-r--r-- 1 norm norm 0 2009-07-12 10:42 someshow.avi
-rw-r--r-- 1 norm norm 0 2009-07-12 10:42 someshow.nfo
I hope this will be of some use.

Norm

Last edited by norobro; 07-12-2009 at 11:37 AM. Reason: correct typo
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
removepkg problem - cutoff folder names hellbillyJoker Slackware 5 01-11-2009 09:23 PM
Shell Script to Get Folder Names firefoxlinux Programming 6 09-27-2007 02:56 PM
long folder names opsraja Fedora 2 09-23-2004 01:56 PM
how to ban certain file or folder names Goma_2 Linux - General 0 06-03-2004 03:54 AM
SAMBA: display of share names is OK but files names are wrong superandrzej Linux - Software 5 02-02-2004 09:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:18 PM.

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
Open Source Consulting | Domain Registration