Splitting a folder containing different files into subfolders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting a folder containing different files into subfolders
# 1  
Old 09-21-2017
Splitting a folder containing different files into subfolders

I have a folder with 4000 (*3) files like
Code:
gr_q4_gb-1.anc
gr_q4_gb-1.anc_cdr_st.txt 
gr_q4_gb-1.anc_cdr_tr.txt
gr_q4_gb-2.anc
gr_q4_gb-2.anc_cdr_st.txt 
gr_q4_gb-2.anc_cdr_tr.txt
gr_q4_gb-3.anc
gr_q4_gb-3.anc_cdr_st.txt 
gr_q4_gb-3.anc_cdr_tr.txt
.
.
gr_q4_gb-4000.anc
gr_q4_gb-4000.anc_cdr_st.txt 
gr_q4_gb-4000.anc_cdr_tr.txt

I want to split this into 150 subfolders. The folder containg 3 types of related files, for example gr_q4_gb-2.anc, gr_q4_gb-2.anc_cdr_st.txt and gr_q4_gb-2.anc_cdr_tr.txt are related and shoud go into a same folder. Roughly every subfolder would have 27 (*3) files.
# 2  
Old 09-21-2017
Any attempts / ideas / thoughts from your side?

Do all those file names follow the same structure, i.e. they all have gr_q4_gb and .anc as substrings? Are the number in strict sequence?
What should the subdirectories' names look like?
# 3  
Old 09-21-2017
Quote:
Originally Posted by RudiC
Any attempts / ideas / thoughts from your side?

Do all those file names follow the same structure, i.e. they all have gr_q4_gb and .anc as substrings? Are the number in strict sequence?
What should the subdirectories' names look like?
I did this after separating the three types of files in three folders
Code:
i=0; 
for f in *; 
do 
    d=dir_$(printf %03d $((i/150+1))); 
    mkdir -p $d; 
    mv "$f" $d; 
    let i++; 
done

Then merged the folders together

Yes, the files have same structure and gr_q4_gb and .anc used as substrings and the mumber sre in strict sequence.

the subdirectories names look like MLtr_1, MLtr_2,.... MLtr_150

Last edited by sammy777888; 09-21-2017 at 04:57 AM.. Reason: missing text
# 4  
Old 09-21-2017
And where and how does that fail?
# 5  
Old 09-21-2017
Quote:
Originally Posted by RudiC
And where and how does that fail?
That worked, but I don't think it i good solution.
# 6  
Old 09-21-2017
Not sure if this is any better:
Code:
for i in {1..27}; do echo mkdir dir_$i; done
for FN in *.anc; do TMP=${FN##*-}; echo mv $FN* dir_$((${TMP%.*}/150+1))/; done

Remove the echo if you like what you see.
# 7  
Old 09-21-2017
Quote:
Originally Posted by RudiC
Not sure if this is any better:
Code:
for i in {1..27}; do echo mkdir dir_$i; done
for FN in *.anc; do TMP=${FN##*-}; echo mv $FN* dir_$((${TMP%.*}/150+1))/; done

Remove the echo if you like what you see.
it gives
Code:
bash: */150+1: syntax error: operand expected (error token is "*/150+1")

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Splitting multiple files in a folder

Hello, I am new to UNIX etc and am trying to split a large number of files, all with the extension .fas in the same folder, into smaller files wherever a string of 5ns occurs. So this file: >File1.fas nnnnnaaaaaattgattttctcagtatcgacgaatatggcgcagaaagttgaataa ... (1 Reply)
Discussion started by: Bryony
1 Replies

2. UNIX for Dummies Questions & Answers

Search current folder and subfolders with grep

Hello, Neither ‘Grep -r' nor ‘grep -R' is working in my environment. (Searching for a text pattern in the files) Any suggestions... Using SunOS 5.9 Thanks, Trinanjan. (1 Reply)
Discussion started by: bhanja_trinanja
1 Replies

3. Homework & Coursework Questions

unique words in files of folder and its subfolders

Hello, I tried to count all unique words of all files in one folder and its subfolders. Can anybody say me, why this doesnt work: ls| find -d | cat | tr "\ " "\n"| uniq -u | wc -l ??? Cat writes only the names of those files, but not the wors, which should be in them. Thanks for any advice. ... (9 Replies)
Discussion started by: Dworza
9 Replies

4. Shell Programming and Scripting

Search in folder and subfolders

How can this be done? I mean, I want to search for all *png *jpg *bmp files in my ~/Pictures/ folder....How can I list them? Thank you geeks :) :b: (2 Replies)
Discussion started by: hakermania
2 Replies

5. UNIX for Dummies Questions & Answers

How to obtain a count of files in a folder and it's subfolders

First of all, the extent of my unix knowledge is next to nil. I've been able to telnet to a unix box, and thanks to the Computer Hope website, I've been able to learn a few basic commands to navigate from folder to folder, and view contents. What I really need to do is obtain a count of all... (2 Replies)
Discussion started by: scarfinv
2 Replies

6. Shell Programming and Scripting

Shell script delete log files from folder & subfolders on space usage

Hi, I am trying to write a shell script to delete logs generate by db when space in the folder reaches 70%. i am getting space values from db, find the files at OS and remove them by using a cron job runs every 5minutes. I have to keep the latest 5 files at any time, my problem is that log files... (3 Replies)
Discussion started by: saha
3 Replies

7. Shell Programming and Scripting

send a mail whenever a file is updated in certain folder or its subfolders

send a mail to a group of users whenever a file is updated in certain folder or its subfolders on an unix server (3 Replies)
Discussion started by: ashishabhishek
3 Replies

8. Shell Programming and Scripting

Replace string in all files in a folder and subfolders.

i need to change string in all files in current folder and all subfolders. i wrote the following script. It works good except it dont delete temp file from subfolders. for z in `find . -type f -name "*.html" -o -name "*.htm"`; do sed -e 's@abc@xyz@g' $z>temp; mv temp $z; done any idea?... (1 Reply)
Discussion started by: crazynups
1 Replies

9. UNIX for Dummies Questions & Answers

How to learn the number of files under a particular folder, containing subfolders

Hi ALL I would like know how many files there under a particular folder, which contains subfolders. Thanks (5 Replies)
Discussion started by: cy163
5 Replies

10. UNIX for Dummies Questions & Answers

Basic Q: getting list of all files of type within folder & subfolders

A painfully rudimentary UNIX question for somebody. I've been puzzling over this for the last hour but can't find the right command. I'm simply trying to get a list of all files - and their full paths - within a folder & subfolders which have extension .php and .js. That's it! No amount of... (1 Reply)
Discussion started by: AtomicPenguin
1 Replies
Login or Register to Ask a Question