UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2010-06-11 05:37 PM

akaballa
Member
Registered: 2010-06-11
Posts: 2

Re: Naming directory based on file name

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:

#!/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 cetting and error on the
$y declaration line stating:

line 11: =: command not found

Offline

#2 2010-06-11 08:07 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: Naming directory based on file name

First, don't surround the initial pattern with single-quotes; that will prevent the shell
from expanding it, as you want it to do...  Also, since you're using bash, you can
use the built-in shell magic to strip the suffix off without needing to resort to sed...
Also, assignment to shell variables can't have a space around the "=" sign; and,
you don't want the $ in front of the variable name during assignment, either...
Plus, you seem to have skipped the "mkdir" call entirely...  And, seem to be copying
the file rather than moving it, as you said you wanted to do...  And, what exactly are
you trying to do with that tar call??  I don't get that at all...

Anyway, here's a simplified version:

for f in *.sdp
do
    d=${f%.sdp}
    mkdir $d
    mv $f $d/
done

Offline

#3 2010-06-11 09:15 PM

akaballa
Member
Registered: 2010-06-11
Posts: 2

Re: Naming directory based on file name

Oh ok, I did not know you could do that!  I thought I could only use sed.  Its works.  Thanks!

Offline

Board footer

Powered by FluxBB