0

need to create script in KSH to mv files to other directory in same server older than 8 days they all .CSV

closed as unclear what you're asking by Archemar, taliezin, G-Man, dr01, garethTheRed Oct 28 '15 at 9:22

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Welcome to unix.SE. I suggest you take a look at the find command. You'll find many similar questions (with answers) here. – roaima Oct 27 '15 at 20:46
  • I tried this but is not doing anything [code] find /tmp/sappodb/ -type f -mtime +2 -exec mv -v {} /tmp/sappodb1/ \; – Rafael Murray Campbell Oct 27 '15 at 20:48
  • That's pretty close. You would have moved all files under /tmp/sappodb that hadn't been modified in the last two days to /tmp/sappodb1. All you were missing was the -name '*.CSV' bit. – roaima Oct 27 '15 at 21:46
  • The asker is inconsistent with the comments given to the accepted answer and the question. – Arthur2e5 Oct 28 '15 at 4:39
  • 1
    Possible duplicate of Can the "find" command work more efficiently to delete many files? – G-Man Oct 28 '15 at 8:26
-1

simply use -t (--target-directory) in the mv cmd like the folowing way :

find csvdir -type f -name '*.CSV' -mtime +8 -exec mv -t 'otherdir/' {} \;

this was my suggestion before i install ksh & test.
while [ $(ls folder/ | wc -l) -ge 8 ];
do
mv "$(ls -1t folder/*.csv | tail -1)" /otherdir/ ;
done

test

enter image description here

  • mmm I cant get it, lol seems legit that way, maybe I spell wrong my question, I just need to keep the last generated files and mv the others to other directory I dont wnat to delete them – Rafael Murray Campbell Oct 27 '15 at 21:20
  • then just change rm "$(ls -1t folder/* | tail -1)"; to mv "$(ls -1t folder/* | tail -1)" /otherdir/ ; – Jonah Oct 27 '15 at 21:25
  • 1
    That doesn't answer the question. It doesn't find the files older than 8 days, it finds (and deletes, or moves) all but the 7 most recent files. and it does it extremely inefficiently, rm-ing or mv-ing one file at a time. – cas Oct 27 '15 at 21:33
  • i already said that :) – Jonah Oct 27 '15 at 21:35
  • 1
    I have read the question and all the comments and can't see anywhere that he mentioned keeping the newest 7 files. – cas Oct 27 '15 at 21:44
0

The command you tried was almost right, but use +8 as the -mtime argument rather than +2.

You say you're using AIX so I'm going to guess that you're also using some ancient shell that requires {} to be quoted or escaped as \{\}. See gnu find and masking the {} for some shells - which?

find /tmp/sappodb/ -type f -mtime +8 -exec mv -v `{}` /tmp/sappodb1/ \;

If you want to limit it to mv-ing .csv files only:

find /tmp/sappodb/ -type f -name '*.csv' -mtime +8 -exec mv -v `{}` /tmp/sappodb1/ \;

Not the answer you're looking for? Browse other questions tagged or ask your own question.