LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-10-2004, 03:45 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Directory contains 26,000 files. How to split into sub-dirs?


I have a directory that contains nearly 26,000 files that have been created over the last couple of years. What is a good way to move them into subdirs with some meaning, i.e. all files up to a certain date get moved into a subdir with a name of that date (or something along those lines)?

I could do it by hand, but I'm sure there's a better way.

If doing it by date is too difficult, I don't mine moving them into subdirs with say, 1500 files each - as long as the files remain in chronological order, in other words, all files in dir 'A' are older than all files in dir 'B' are older than all files in dir 'C' etc.etc.

Thanks
 
Old 05-10-2004, 04:11 PM   #2
iainr
Member
 
Registered: Nov 2002
Location: England
Distribution: Ubuntu 9.04
Posts: 631

Rep: Reputation: 30
Here's an idea.

Get the filenames into a big file in the order you want.

e.g. if you want to split on name :
ls > file
on date
ls -t > file

Then use the split command to create smaller files.

e.g. split -l 1000 file
to create 26 files, each of 1000 lines.

Now you can move all the files listed in each of the 26 files into a separate directory.

e.g
while read FILE < xaa
do
mv $FILE newdir
done
 
Old 05-10-2004, 04:15 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
ok, i've not really tested squat, but something like this should suffice.
Code:
for i in *
do
   filemonth=`stat $i | grep Access | cut -c 13-15,31-32`
   if [ ! -d $filemonth ] 
      mkdir $filemonth
   fi
   mv $i $filemonth\$i
done
have a bash! This should move all files into directories such as "apr04" or "oct03"

Last edited by acid_kewpie; 05-10-2004 at 04:16 PM.
 
Old 05-10-2004, 09:24 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally posted by acid_kewpie
ok, i've not really tested squat, but something like this should suffice.
...
This should move all files into directories such as "apr04" or "oct03"
good idea! this is the way I did it:
Code:
#!/bin/bash

for i in *
do
  filemonth=`stat --format=%y $i | cut -c 0-7`
  mkdir -p $filemonth
  mv $i $filemonth/$i
done
evidently, your stat and my stat return different info. This script creates directories that look like this:
2003-09
2003-10
2003-11
2003-12
2004-01
2004-02



Thanks for the suggestion!
 
Old 05-12-2004, 04:37 PM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
hehe.. you mean you actually read the stat manpage unlike me... plus points there for sure!
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
100,000,000 Firefox downloads! Eerath General 6 10-22-2005 11:20 PM
1,000,000,000 PCs by 2010 masand Linux - News 4 11-01-2004 01:55 AM
LinuxQuestions.org Surpasses 1,000,000 Posts jeremy LQ Suggestions & Feedback 15 07-21-2004 12:52 AM
copying 30,000 files amadkow Linux - Software 10 06-09-2004 12:12 PM
ls, dirs first, files later TroelsSmit Linux - Newbie 4 05-31-2004 11:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

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
Open Source Consulting | Domain Registration