The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Move all files in a directory tree to a signal directory? briandanielz UNIX for Dummies Questions & Answers 2 06-15-2008 06:20 PM
Considering files within a directory?? skyineyes Shell Programming and Scripting 3 07-16-2007 04:16 AM
MV files from one directory structure(multiple level) to other directory structure srmadab UNIX for Advanced & Expert Users 4 09-13-2006 05:01 PM
copy files from one directory to another directory zip_zip UNIX for Dummies Questions & Answers 5 09-14-2003 07:16 PM
moving files from a unix directory to a windows directory gleads UNIX for Dummies Questions & Answers 2 08-29-2002 09:42 PM

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-15-2008
Bhanu72 Bhanu72 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
Smile files from directory to another.

I have 10000 files in a directory. The name is something like:

1.dat
2.dat
3.dat
4.dat
.....
.....
.....
1000.dat.
----
-----

Files are not sorted. I want to move first 500 largerst files from this directory to another directory. Next 500 largest files to another directory.

what would be the simple korn shell script for this problem?

thanks

Last edited by Bhanu72; 07-15-2008 at 08:31 PM..
  #2 (permalink)  
Old 07-15-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Here's one way. Take out the prints when you have finished testing. The tail +2 is to skip the total blocks displayed by ls -l. This will not work if any of the filenames contain spaces.


Code:
i=0
split=1000
ls -l | tail +2 | sort -rn -k 5,5 | awk '{print $NF}' | while read f
do
        (( i%${split}==0 )) && print mkdir subdir$(( i/${split} ))
        print mv $f subdir$(( i/${split} ))
        (( i=i+1 ))
done

  #3 (permalink)  
Old 07-15-2008
lramirev lramirev is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 8
Smile

First you would need your file list sorted by file size
#!/bin/ksh
SearchPath=/whereever0
LowerSizeFilePath=/whereever1
UpperSizeFilePath=/whereever2
#
cd $SearchPath
ls -e | egrep -v "/$|->" | cut -c31-41,63- | sort -n -k 1n,10 | cut -c12- > /tmp/filelist~
#^ ^ ^ ^ ^
#| | | | + get file
#| | | +-- Sort file list by file size
#| | +-- Get only file size and file name
#| +-- Get off the list soft links and directories entries
#+-- list extended $CWD
#
#You can send the list to a temp file with "> /tmp/filelist~"
#
#You need to know the number of file with
FileCount=`cat /tmp/filelist~ | wc -l`
HeadSize=`expr $FileCount / 2`
TailSize=`expr $FileCount - $HeadSize`
#
mv `head -$HeadSize /tmp/filelist~` $LowerSizeFilePath
mv `tail -$TailSize /tmp/filelist~` $UpperSizeFilePath
rm /tmp/filelist~
  #4 (permalink)  
Old 07-15-2008
Bhanu72 Bhanu72 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
Hi

thanks for the solution. But for some reason it is not working or may be I am not understanding.

Can you please give more detail :

Why are you using egrep? What is it really doing?
ls it a typo that you are using ls -e instead of ls -l?

thanks for your help.
  #5 (permalink)  
Old 07-16-2008
lramirev lramirev is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 8
Bhanu72:
I when to my server and tryed the script and it did worked. But when ran into a diffrent server "ls -e" is not supported. I look into diffrence on SunOS and notice that the server not supporting "-e" option for "ls" is has lower patche level for SunOS 5.10. So I can only assure that this script may work on Sun Solaris 5.10 Generic_125100-10.

I changed the script to use "ls -l", but need to change the "cut" setting.


Code:
ls -l | egrep -v "/$|->" | cut -c31-41,55- | sort -n -k 1n,10 | cut 
-c13- > /tmp/filelist~

When I don't understand a part of a code I run each part of the pipe command adding one part at a time so see that it is doing. With this suggested change to the script it must run. I ran it on a solaris 5.10 with lower patch level.

My best regards.
Luis Ramirez
  #6 (permalink)  
Old 07-16-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Did you try my solution? Change split=1000 to split=500 by the way, I forgot that you originally wanted to move 500 files, not 1000.
  #7 (permalink)  
Old 07-17-2008
Bhanu72 Bhanu72 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
Hi,

I did try it with small file number. split=5. It worked fine. In your code, you have created the directory within the script. But in my case the directory already exist. So I have to make simple modification.

Thanks for your help. I appreciate it.

Bhanu72
Closed Thread

Bookmarks

Tags
file move, solaris

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 11:43 PM.


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