![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| separating entries by using cat command | za_7565 | Shell Programming and Scripting | 7 | 01-29-2008 05:00 AM |
| separating filename and extension | lucaspewkas | Shell Programming and Scripting | 2 | 04-06-2007 04:07 AM |
| Separating commands/programs with ; | dush_19 | High Level Programming | 2 | 06-22-2006 02:34 AM |
| separating fields | new2ss | Shell Programming and Scripting | 5 | 02-19-2006 06:02 PM |
| separating commands | mile1982 | High Level Programming | 2 | 09-13-2004 08:41 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Simple script for separating files
Hi all,
I was wondering if someone could help me writing a simple script on separating files into separate folders. I have 92 files called various things and I want to separate these folders like so: Create a folder called "1" and put files 1-23 in it Create a folder called "2" and put files 24-46 in it Create a folder called "3" and put files 47-69 in it Create a folder called "4" and put files 70-92 in it Again the files aren't names 1 thru 92, so is there a way to say take the first 23, and then the next 23 and so on.... How would I go about doing this in csh? Thanks in advance! -m |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
It all depends on how you want them sorted.
Do you have a hard limit at 23? If so, count all of your files, devide by 23, round up to the nearest whole number. You need this many directories. You can list your directory in order by date (or reverse order) / alphabetically (or reverse alpha) / etc. Pipe your list into a simple do/while script with a counter inside. While the counter is less than 23 (or whatever number you require), move the next file in the list into a subdirectory. This looks suspiciously like homework, so please don't be offended if I don't write the script for you. Give it a try, post back here with your results. At least make an effort. |
|
#3
|
|||
|
|||
|
Thanks for your suggestions. I can assure you it isn't homework. I work in a fMRI lab and we use linux/unix based software for doing analysis and I just started and am trying to slowly learn how to write scripts to separate our dicom files to make my life easier.
Most of your suggestions are still foreign to me. So I will try to work on it, but it might take me a while to even get up to that point. Thanks, Megan |
|
#4
|
|||
|
|||
|
Hi Megan,
First off, sorry for the assumption - I'm still new here, and trying hard not to break any rules myself! Here is some code that I've thrown together that should resolve your issue (no guarantees!) NOTE: I did not include directory creation to this script. You can do it, but I didn't think to include it before firing it up here. Code:
#!/usr/bin/csh
# Here are the variables that you will need to change:
################################################################################
set number_of_files = 23 # This is the number of files that you expect
# to store in each directory
set source_dir = /tmp/testing # You'll change this to your source directory
set target_dir = /tmp/folder_ # This is the prefix that you'll use for your
# folder tree. You'll likely wish to change it
# Here are the 'static' variables that you will not change:
################################################################################
#this number will increment the count cap
set count = 0
#iteration counters
set folder_counter = 1 # Starts at one, such that multiplication issues don't arise
set counter = 0
# Get your list of files - I'm taking it from an ls -tr
# This provides an historic list, with the oldest files first
foreach file ( `ls -tr $source_dir` )
@ counter = $counter + 1
@ count = $number_of_files * $folder_counter #First time 'round, this is 3, then 6
# First, check to see if you should move on to the next folder or not...
# - adding a 'move' request to ensure that the file is not skipped
if ($counter > $count) then
@ folder_counter++
mv $source_dir/$file $target_dir$folder_counter
endif
# Since you didn't need to increment the folder_counter - move the file as if nothing happened...
if ($counter <= ($count)) then
mv $source_dir/$file $target_dir$folder_counter
endif
end
edit:changed a comment (wrong comment included in paste) |
|
#5
|
|||
|
|||
|
Thanks for all your help! That did help a bit, at least got me started on the right foot. So I changed things a bit (see below). However, when I run it the first 23 files don't end up in a directory, and it all ends up in a directory called DTI5. Is there anyway to count the files and after counting them say move file #1 thru #23 to directory DTI1, move file #24 thru #46 to directory DTI2, and so on AND THEN tell it to stop counting at file number 92 if there happens to be more files in the source directory???
#!/bin/csh # Here are the variables that you will need to change: ################################################################################ mkdir DTI1 mkdir DTI2 mkdir DTI3 mkdir DTI4 set number_of_files = 23 # This is the number of files that you expect # to store in each directory set source_dir = ./ # changed this to the current directory I want it run in (has all 92 files) set target_dir = DTI # This is the prefix that you'll use for your # folder tree. You'll likely wish to change it # Here are the 'static' variables that you will not change: ################################################################################ #this number will increment the count cap set count = 0 #iteration counters set folder_counter = 1 # Starts at one, such that multiplication issues don't arise set counter = 0 # Get your list of files - (I don't want them by date, they are already in order so I just list them) foreach file ( `ls $source_dir` ) @ counter = $counter + 1 @ count = $number_of_files * $folder_counter #First time 'round, this is 3, then 6 # First, check to see if you should move on to the next folder or not... # - adding a 'move' request to ensure that the file is not skipped if ($counter > $count) then @ folder_counter++ mv $source_dir/$file $target_dir$folder_counter endif # Since you didn't need to increment the folder_counter - move the file as if nothing happened... if ($counter <= ($count)) then mv $source_dir/$file $target_dir$folder_counter endif end |
|
#6
|
|||
|
|||
|
k...
Changed it up a bit, and added the directory testing... (Remember to NOT have the script in the same directory, or it will get moved along with 91 of your 92 files) Cheers! Code:
#!/bin/csh
# Here are the variables that you will need to change:
################################################################################
set number_of_files = 23 # This is the number of files that you
# expect to store in each directory
set total_files_to_move = 92 # This is the total number of files that
# you will move with this utility
set source_dir = ./ # set to 'current' directory
set target_dir = ./DTI # This is the prefix that you'll use for
# the folder tree.
# Here are the 'static' variables that you will not change:
################################################################################
#this number will increment the count cap
set count = 0
#iteration counters
set folder_counter = 1 # Starts at one, such that multiplication issues don't arise
set counter = 0
# Get your list of files
foreach file ( `ls -tr $source_dir` )
@ counter = $counter + 1
@ count = $number_of_files * $folder_counter
# Test to see if the first target directory exists
if (! -d $target_dir$folder_counter ) then
mkdir $target_dir$folder_counter
endif
# Test to see if the maximum number of files to move have been moved
if ($counter > ($total_files_to_move)) then
exit
endif
# Check to see if you should move on to the next folder or not...
if ($counter > $count) then
@ folder_counter++
# Test to see if subsequent target directory exists
if (! -d $target_dir$folder_counter ) then
mkdir $target_dir$folder_counter
endif
mv $source_dir/$file $target_dir$folder_counter/$file
endif
# Since you didn't need to increment the folder_counter - move the file as if nothing happened...
if ($counter <= ($count)) then
mv $source_dir/$file $target_dir$folder_counter/$file
endif
end
|
|||
| Google The UNIX and Linux Forums |