Register
It is currently Thu Aug 21, 2014 2:13 am

Naming directory based on file name


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Fri Jun 11, 2010 12:02 pm   

Joined: Fri Jun 11, 2010 12:00 pm
Posts: 1
Hi,

I am trying to loop though each files in a directory, and for each file create its own directory and move the file to that directory.

If the name of the file is <something.sdp>, I need to name the directory "something".

this is the code I have tried so far:

Code:

#!/bin/bash

#move each file each tar file in

for x in ' /proj/ims_dumps/dumps/Aquarium/FT_applications/mmas2-1/*.sdp'
do
#if [ $x == *.sdp ]
# then


  $y =  `sed 's/.sdp//' $x`
   echo $y
   cp $x $y
   cd $y
   tar -xvf $x
   echo "Done!"
# fi
done



i dont think my sed expression is correct, but I am getting an error on the
$y declaration line stating:

line 11: =: command not found


Top
 Profile  
 PostPosted: Fri Jun 11, 2010 11:37 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 534
Parameter Expansion can do it:
Code:
#!/bin/bash

for x in /proj/ims_dumps/dumps/Aquarium/FT_applications/mmas2-1/*.sdp
do
   y="${x%.sdp}"


don't quote the in part of the for loop, otherwise the asterisk won't get expanded.
no dollar sign, and no spaces around equal sign; when assigning a variable


Top
 Profile  
 PostPosted: Sun Jun 13, 2010 6:06 pm   

Joined: Thu Apr 08, 2010 2:12 pm
Posts: 7
Quote:
by akaballa
$y = `sed 's/.sdp//' $x`

Sed command is not intended to change the name of the file, but the data inside the file.
On defining a variable, like 'y' above the dollar is not put in front of it. The dollar is used after, when the variable is to be expanded.
There must be no space between the equal sign.
Thus, the line above should be written like that
Code:
y=`<some command>`

In this way, the output of <some command> will be passed to the "y" variable.

In the script written above it is missing the creation of the directory, something like "mkdir $y" before "cp $x $y"


Top
 Profile  
 PostPosted: Tue Jun 15, 2010 7:57 am   

Joined: Thu Apr 08, 2010 2:12 pm
Posts: 7
That is a way you can use sed to change the file name and not the file data.
Code:
y=$(echo $x |sed 's/\.dsp//')

You must use the backslash in front of the dot '.' because dot is a metacharacter, meaning every single character in bash


Top
 Profile  
 PostPosted: Tue Jun 15, 2010 8:50 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 534
Quote:
because dot is a metacharacter, meaning every single character in bash
in sed

another way:
Code:
y=$(sed 's/\.dsp//' <<< "$x")
;)


Top
 Profile  
 PostPosted: Wed Jun 16, 2010 6:46 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Watael wrote:
another way:
Code:
y=$(sed 's/\.dsp//' <<< "$x")
;)

Or, without sed:
Code:
y=${x/.dsp/}

or, if we know that we want to remove just the file extension:
Code:
y=${x%.dsp}

No $y is needed therefore:
Code:
cp $x ${x%.dsp}

If your filenames may contain spaces or other strange characters, double quote each argument of cp.
B-)


Top
 Profile  
 PostPosted: Sat Jun 11, 2011 7:38 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Did you mean « copy » by « move »? Well, if the answer is yes, replace « mv » by « cp ».

Code:
#!/bin/bash

path=/proj/ims_dumps/dumps/Aquarium/FT_applications/mmas2-1

for f in $path/*.sdp; do
    dir=${f%.*}
    file=${f##*/}
       
    mkdir "$dir"
    mv "$f" "$dir"

    (   
        cd "$dir"/
        tar xf "$file"
    )
done

printf 'Done\n'


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP