0

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

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
  • 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. – Mingye Wang Oct 28 '15 at 04:39
  • 1
    Possible duplicate of [Can the "find" command work more efficiently to delete many files?](http://unix.stackexchange.com/questions/152513/can-the-find-command-work-more-efficiently-to-delete-many-files) – G-Man Says 'Reinstate Monica' Oct 28 '15 at 08:26
  • See also [Auto-delete inactive files after *x*](http://unix.stackexchange.com/q/196022/80216);  related: [List of Recently Modified Files](http://unix.stackexchange.com/q/33850/80216). – G-Man Says 'Reinstate Monica' Oct 28 '15 at 08:26

2 Answers2

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/ \;
cas
  • 1
  • 7
  • 119
  • 185
-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

Yunus
  • 1,634
  • 2
  • 13
  • 19
  • 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/ ; – Yunus 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 :) – Yunus Oct 27 '15 at 21:35
  • find folder -type f -mtime +8 -exec rm {} \; #this command didn't work for him and according to his needs he wanted to keep the newest 7 file – Yunus Oct 27 '15 at 21:37
  • 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
  • read again his commant on the question , and read what i wrote before giving mt suggestion – Yunus Oct 27 '15 at 21:47
  • why don't you just put a good question insteed of emply talking , i'll be the first to vote ....?! – Yunus Oct 27 '15 at 21:49
  • Thks for all your help guys, the issue is i need to move all the files to another dir but just need daily to keep 7 files (the most recent) all are in .csv format – Rafael Murray Campbell Oct 27 '15 at 22:13
  • you can use find in the other question , if not working for you then just use the loop i suggested , because it'll do the same according to your case (the second loop which contain ' mv "$(....)" otherdir/ ' ) ! who knows maybe find itself uses 'while/do' !!! – Yunus Oct 28 '15 at 11:45