The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Directory Creation problem namishtiwari Shell Programming and Scripting 1 02-05-2008 08:04 AM
VI questions : mass changes, mass delete and external insert Browser_ice AIX 1 12-13-2006 04:20 AM
creation of unwanted directory mridula High Level Programming 6 10-05-2005 11:40 AM
directory creation sveera UNIX for Advanced & Expert Users 4 05-17-2005 01:06 PM
Remote directory creation srinivay UNIX for Advanced & Expert Users 5 11-05-2004 05:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-20-2005
AeroEngy AeroEngy is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 9
Question Mass directory creation?

I have a couple thousand data files that all have to have there own directory named exactly the same as the file name. Then the file needs to be moved to that directory. For example files test1.mat, test2.mat, test3.mat in directory X need to have directories test1, test2, test3 created underneath X and then the corresponding files moved into there respective directory. Just for you information the file names are much more complicated than just test1 so typing them all out would be time consumming. Is there a way that I could write a script or something that could read the file names, create all the directories, and move all the files at once.

I am not very experienced in the Unix enviroment so any help would be appreciated. If it helps Matlab is installed on the Unix Machine.
  #2 (permalink)  
Old 07-20-2005
becket's Avatar
becket becket is offline
Registered User
  
 

Join Date: Jul 2005
Location: Liege / Belgium
Posts: 36
with bash :

Code:
for file in `ls`
do 
mkdir -p $file 
mv $file $file/
done
  #3 (permalink)  
Old 07-20-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
Code:
#!/bin/ksh

for file in *.mat ; do 
   directory=${file%.mat}
   mkdir $directory > /dev/null 2>&1
   mv $file $directory
done
  #4 (permalink)  
Old 07-20-2005
AeroEngy AeroEngy is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 9
Ok I tried both of your suggestions and neither worked exactly. There were a couple of problems with
Code:
for file in `ls`
do 
mkdir -p $file 
mv $file $file/
done
First, it tried to make a directory called ls. So I replace for file in 'ls' with for file in *.mat. Then it didn't work because it wouldn't let me create a directory with exactly the same name as an existing file name in the parent directory. So I modified it to the following which works almost perfectly.
Code:
for file in *.mat
do 
mkdir -p destination/$file
mv $file destination/$file
done
However, the problem with this is that it makes the directories with the file extensions on them. For example it creates the directory test1.mat in the destination directory instead of just test1. How can i get rid of the .mat in the directory name.

Thanks in advance.
  #5 (permalink)  
Old 07-20-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
You didn't say what problems you had with my original post, which does exactly what you originally requested for me.

This is the output from running my script on a test machine

Code:
# cat test.sh
#!/bin/ksh

for file in *.mat ; do
   directory=${file%.mat}
   mkdir $directory > /dev/null 2>&1
   mv $file $directory
done

# ls
1.mat     2.mat     3.mat     4.mat     5.mat     6.mat     7.mat     8.mat     9.mat     file.mat  test.sh
# ./test.sh
# find .
.
./2
./2/2.mat
./3
./3/3.mat
./4
./4/4.mat
./5
./5/5.mat
./6
./6/6.mat
./7
./7/7.mat
./8
./8/8.mat
./9
./9/9.mat
./file
./file/file.mat
./test.sh
./1
./1/1.mat

Last edited by reborg; 07-20-2005 at 06:43 PM.. Reason: add session output
  #6 (permalink)  
Old 07-20-2005
AeroEngy AeroEngy is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 9
Quote:
You didn't say what problems you had with my original post, which does exactly what you originally requested for me.
Sorry about that. Looking at your code again, I think I may have had a syntax error. I was getting a couple different error messages when I tired it earlier today but I can't remember them right now. I think one of them might have been unexpected '&'. I don't have a Unix machine at home so I will have to wait until tomorrow to try again. I appreciate your help and will let you know tomorrow how it turns out.
  #7 (permalink)  
Old 07-21-2005
AeroEngy AeroEngy is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 9
I retried you script and it works perfectly. I just slightly moddified it so that it would create the new directories in a different path so that I wouldn't have to move them all later.

Thanks reborg
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0