LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-03-2009, 04:01 AM   #1
pkacker
LQ Newbie
 
Registered: Jun 2009
Location: Genova, Italy
Posts: 13

Rep: Reputation: Disabled
Creating multiple directories based on file names


I have around 95 files in a directory and i want to move them each in a separate directory. I want to put name of the directory same as their file name. for example in my directory file names are:

abc.pdb
bcd.pdb
cdf.pdb

i want to create the directories named (directories are not nested)

abc
bcd
cdf

and want to move above three files in the newly created folder.
I want to do this by using a shell script.

thanks

Last edited by pkacker; 06-04-2009 at 03:57 AM.
 
Old 06-03-2009, 04:12 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
What have you tried so far? What does not work?
 
Old 06-03-2009, 04:17 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Hi,

Here's a one-liner that does what you want:

for THISFILE in `ls *pdb`; do DIRNAME=${THISFILE%%.*}; mkdir $DIRNAME; mv $THISFILE $DIRNAME; done

Up to you to figure out how this works and, if needed, make it into a script (I'm not sure if this is homework....)
 
Old 06-03-2009, 04:54 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by druuna View Post
Hi,

Here's a one-liner that does what you want:

for THISFILE in `ls *pdb`; do DIRNAME=${THISFILE%%.*}; mkdir $DIRNAME; mv $THISFILE $DIRNAME; done
no need ls..ls is useless here.
Code:
for THISFILE in *pdb
also, use % instead of %%, in case OP has files with more than 1 dot.
 
Old 06-03-2009, 04:57 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
@ghostdog74: Both are good, valid points!

That's what happens if you're not really awake yet and are on auto-pilot
 
Old 06-03-2009, 05:51 AM   #6
pkacker
LQ Newbie
 
Registered: Jun 2009
Location: Genova, Italy
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks for your suggestions.
I put my code like this:

#!/bin/bash

for THISFILE in *.pdb
do
DIRNAME=`echo $THISFILE | cut -d. -f1`
echo $DIRNAME
mkdir $DIRNAME
mv $THISFILE $DIRNAME
done


Thanks again
 
Old 06-03-2009, 06:07 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pkacker View Post
Thanks for your suggestions.
I put my code like this:

#!/bin/bash
...
DIRNAME=`echo $THISFILE | cut -d. -f1`
..

Thanks again
don't need to call the cut command. just use what drunna posted
Code:
DIRNAME=${THISFILE%.*}
 
  


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
Trying to create directories based on file names JackieBrown Linux - General 3 05-15-2009 10:13 PM
copying multiple different files in different directories with identical file names nickleus Linux - Software 4 03-09-2006 02:25 PM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 01:23 PM
Copy one file to multiple directories cmfarley19 Linux - General 9 11-10-2004 01:57 PM
Copying a single file to multiple directories tgolly Linux - Newbie 3 04-26-2004 04:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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