LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   please help script list/find move files greate than 1G to different directory. (https://www.linuxquestions.org/questions/linux-newbie-8/please-help-script-list-find-move-files-greate-than-1g-to-different-directory-4175500199/)

dotran 04-01-2014 12:45 PM

please help script list/find move files greate than 1G to different directory.
 
I have have 6 empty directory below. I would like write bash scipt if any files less "1000000000" bytes then move to "/export/home/mytmp/final" folder first and any files greater than "1000000000" bytes then move to final1, final2, final3, final4, final4, final5 and that depend see how many files, but I only want ONE file go into 1 directory. I run the command below with great than "1000000000" bytes and see 3 files then that 3 files should go 3 folders (final1, final2 and final3) only. If see more that 6 or 8 files greater than "1000000000" bytes then should go last folder "final5" . Is some kinda script loop through or command to do this? Please help with this task. Thanks

Quote:

/export/home/mytmp/final <= less "1000000000"
/export/home/mytmp/final1 <= greater "1000000000" but require only file move a
/export/home/mytmp/final2
/export/home/mytmp/final3
/export/home/mytmp/final4
/export/home/mytmp/final5

/export/home/mytmp/test1> ls -ltr test*.txt
-rwxrwxr-x 1 ca7prod ftpusers 1073741824 Mar 4 11:14 test3.txt
-rwxrwxr-x 1 ca7prod ftpusers 524288000 Mar 4 11:26 test4.txt
-rwxrwxr-x 1 ca7prod ftpusers 629145600 Mar 4 11:28 test5.txt
-rwxrwxr-x 1 ca7prod ftpusers 734003200 Mar 4 23:47 test7.txt
-rw-rw-r-- 1 ca7prod ftpusers 14 Mar 12 14:43 test.txt
-rw------- 1 ca7prod ftpusers 52428800 Mar 31 15:24 test2.txt
-rw-rw-r-- 1 ca7prod ftpusers 104857600 Mar 31 15:27 test8.txt
-rw-rw-r-- 1 ca7prod ftpusers 1178599424 Mar 31 15:31 test9.txt
-rw------- 1 ca7prod ftpusers 104857600 Mar 31 15:32 test10.txt
-rwxrwxr-x 1 ca7prod ftpusers 104857600 Mar 31 15:37 test6.txt
-rw-rw-r-- 1 ca7prod ftpusers 157286400 Mar 31 15:38 test1.txt
-rw-rw-r-- 1 ca7prod ftpusers 1335885824 Mar 31 15:40 test11.txt
/export/home/mytmp/test1> ls -ltr test*.txt | awk '{if ($5 < 1000000000) print $9}' -exec mv {} /export/home/mytmp/final \;
test4.txt
test5.txt
test7.txt
test.txt
test2.txt
test8.txt
test10.txt
test6.txt
test1.txt
/export/home/mytmp/test1> ls -ltr test*.txt | awk '{if ($5 > 1000000000) print $9}' -exec mv {} /export/home/mytmp/xxx \;
test3.txt
test9.txt
test11.txt
Please help with this script
/export/home/mytmp/test1> cat test.ksh
Quote:

#!/bin/ksh
cd /export/home/mytmp/test1
###############################################################################
# Any files less than 1G move to final
###############################################################################
ls -ltr test*.txt | awk '{if ($5 < 1000000000) print $9}' -exec mv {} /export/home/mytmp/final \;
###############################################################################
# Any files greater than 1G move to final1, final2, final3, final4, final4, final5
###############################################################################
ls -ltr test*.txt | awk '{if ($5 > 1000000000) print $9}' |sed q | xxxxx bla bla
find /export/home/mytmp/test1 bla bla

allend 04-01-2014 06:47 PM

Without writing a script for you, I will offer some thoughts on how I would go about this.

First, it is considered poor practice to parse the output from the 'ls' command in scripts. http://mywiki.wooledge.org/ParsingLs

For getting a list of files to process, use the 'find' command with the '-size' option.

Code:

# Pseudocode to show a general logic

# Find files <1G in size
find . -size -1G -type f

# Find files >1G in size
find . -size +1G -type f
test number of files found
if count=1 then
elif count=2 then
...
elif count>6 then
fi



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