0

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

6
  • 1
    Welcome to unix.SE. I suggest you take a look at the find command. You'll find many similar questions (with answers) here. Commented Oct 27, 2015 at 20:46
  • I tried this but is not doing anything [code] find /tmp/sappodb/ -type f -mtime +2 -exec mv -v {} /tmp/sappodb1/ \; Commented Oct 27, 2015 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. Commented Oct 27, 2015 at 21:46
  • The asker is inconsistent with the comments given to the accepted answer and the question. Commented Oct 28, 2015 at 4:39
  • 1
    Possible duplicate of Can the "find" command work more efficiently to delete many files? Commented Oct 28, 2015 at 8:26

2 Answers 2

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/ \; 
0
-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

10
  • 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 Commented Oct 27, 2015 at 21:20
  • then just change rm "$(ls -1t folder/* | tail -1)"; to mv "$(ls -1t folder/* | tail -1)" /otherdir/ ; Commented Oct 27, 2015 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. Commented Oct 27, 2015 at 21:33
  • i already said that :) Commented Oct 27, 2015 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. Commented Oct 27, 2015 at 21:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.