LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Create a folder from a file name (https://www.linuxquestions.org/questions/programming-9/create-a-folder-from-a-file-name-943225/)

prravin1 05-04-2012 04:17 AM

Create a folder from a file name
 
I have a files called STMam.bam.

Now i want to create a directory with the same name as the file but it should exclude the .bam extension.

The folder created should be named as STMam.

acid_kewpie 05-04-2012 04:27 AM

erm... "mkdir STMam" will do that... if you're after help with scripting or programming, you need to provde more useful information here. the basename tool will probably help you a lot.

NevemTeve 05-04-2012 04:50 AM

Sg like this
Code:

FILENAME=stmam.bam; DIRNAME="${FILENAME%\.*}"; echo $DIRNAME

grail 05-04-2012 05:35 AM

While I like the solution I am curious why the escape? A period is not special when globbing nor to the shell so the escape, as far as I can tell, serves no benefit.

prravin1 05-04-2012 05:39 AM

Quote:

Originally Posted by grail (Post 4670159)
While I like the solution I am curious why the escape? A period is not special when globbing nor to the shell so the escape, as far as I can tell, serves no benefit.

FNAME=file.ext
DIRNAME=$(echo ${FNAME/.*/})
echo $DIRNAME will show you directory name


mkdir "${FN%.*}"

DIR_NAME=$(echo ${FILENAME} | awk -F. '{ print $4 }')
mkdir ${DIR_NAME}

grail 05-04-2012 07:16 AM

I am not sure what you are trying to show here? Well apart from the fact that you are using your own solution and not the example by NevemTeve

NevemTeve 05-04-2012 07:26 AM

Sure, I checked the manual again, dot doesn't have special meaning in this context, so it is unnecessary to escape it. My fault.


All times are GMT -5. The time now is 07:18 AM.