Mass directory creation?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Mass directory creation?
# 1  
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  
with bash :

for file in `ls`
mkdir -p $file 
mv $file $file/

# 3  

for file in *.mat ; do 
   mkdir $directory > /dev/null 2>&1
   mv $file $directory

# 4  
Ok I tried both of your suggestions and neither worked exactly. There were a couple of problems with
for file in `ls`
mkdir -p $file 
mv $file $file/

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.
for file in *.mat
mkdir -p destination/$file
mv $file destination/$file

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  
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

# cat

for file in *.mat ; do
   mkdir $directory > /dev/null 2>&1
   mv $file $directory

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

Last edited by reborg; 07-20-2005 at 07:43 PM.. Reason: add session output
# 6  
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  
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

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #427
Difficulty: Easy
JavaScript supports only explicit delegation.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reason for no directory creation date

i read here that linux provides no way to determine when a directory was created. have a directory /home/andy/scripts that had a README file in it. That file says I put the script in that directory and... (3 Replies)
Discussion started by: drew77
3 Replies

2. UNIX for Beginners Questions & Answers

UNIX directory creation issues

Hi All, I am facing a strange scenario. I upgraded my server version to new version of linux. After the when I create a directory it is created with . in the the permission . What is the reason and how to fix . $ mkdir test $ ls -l total 0 drwxr-xr-x. 2 uk01 users 6 Dec 7 03:49 test ... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

3. Cybersecurity

Mass account creation

By the company winning business from another outsource provider, I've suddenly inherited towards 300 servers and all accounts are local. One of the immediate tasks is to set up all the OS, DB, and app support staff on all of the servers operating systems. I've slapped together a crude script... (10 Replies)
Discussion started by: rbatte1
10 Replies

4. Shell Programming and Scripting

Creation date of a directory

what's the command to find the creation date of a certain dirctory? (1 Reply)
Discussion started by: miss_dodi
1 Replies

5. Shell Programming and Scripting

Directory Creation problem

Hiiii, here is my script-- BackupLocation="$OPTARG" if ]; then echo "Either option l or L should be given to $Programname" echo "$Usage" echo "$Programname is terminated" ... (1 Reply)
Discussion started by: namishtiwari
1 Replies

6. AIX

VI questions : mass changes, mass delete and external insert

Is it possible in VI to do a global change but take the search patterns and the replacement patterns from an external file ? I have cases where I can have 100,200 or 300+ global changes to do. All the new records are inside a file and I must VI a work file to change all of them. Also, can... (1 Reply)
Discussion started by: Browser_ice
1 Replies

7. AIX

Filesystem creation over existing Directory

Is it possible to create a Filesystem with the mount point over an existing Directory, without loosing the data in the Directory? eg:- /u01 -> /pmmpd/u01 (Directory with soft link) /pmmpd/u01 (Need to create this filesystem, without loosing data) Thanks TheDoc (0 Replies)
Discussion started by: TheDoc
0 Replies

8. Programming

creation of unwanted directory

hi all i have a file with server id's and I need to create a directory corresponding to each server id listed in the text file. eg: id directory name 1 node_1 2 ... (6 Replies)
Discussion started by: mridula
6 Replies

9. UNIX for Advanced & Expert Users

directory creation

Hi Friends, I am trying to move some files from one directory to another. but if the destination directory doesnt exist then i have to create one and then move files to that. For this i have to write a script. please help. thanks in advance Veera (4 Replies)
Discussion started by: sveera
4 Replies

10. UNIX for Advanced & Expert Users

Remote directory creation

Is there a way to create a directory in some remote location from a local location? I understand that we can use 'rcp' to copy a local file to a remote location. I am looking for a command in similar lines which lets us create directories in remote locations. Could someone help? (5 Replies)
Discussion started by: srinivay
5 Replies

Featured Tech Videos