![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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.. |
|
||||
|
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
|
|
||||
|
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~ |
|
||||
|
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. |
|
||||
|
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 |
|
||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| file move, solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|