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
  • 56,709
  • 26
  • 150
  • 232

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
  • 78,579
-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,684
  • 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