We help IT Professionals succeed at work.

find and delete

sam2929
sam2929 asked
on
Medium Priority
130 Views
Last Modified: 2020-01-02
I have files in unix path
/test1/dev/etl/log

AA_RES_PERSON_20191124.CSV
AA_RES_PERSON_20191124.LOG
KKK_AAA_BB.CSV
AA_RES_TEST1_20191222.CSV

AND SO ON


What i want is find all files with aa_res just for yesterday and then delete it

i want to write it as unix script with number of days to delete as parameter and  /dev path enviorment as variable as code will run in uat and prod
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
find /test1/dev/etl/log -name 'aa_res*' -mtime -1 -mtime 0  -ls
if that returns the list that matches your interest replace -LS with -exec  rm ()\;
to delete

You could use perl with stat to be more precise since the find and mtime is covering 24 hours and will vary based on when the script runs

to cover only yesterday, you may have to run it at 1 minute before midnight and use -2 and 1 to capture yeasterday.....
nociSoftware Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Searching files:    find  

#!/bin/bash
XPATH="$1"
find "$XPATH" -iname 'aa_res*' -daystart -mtime +0 -mtime -2  -print0 | xargs -0 echo

Open in new window


replace echo with rm to really remove files
-daystart make the find command process date & time as 00:00 today offset instead of now.

Author

Commented:
If I want to delete anything last 7 days starting from time they run the script
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
then do not use the -daystart option that noci provided in the comment.
test first to make sure the pattern finds what you are looking for.
nociSoftware Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
#!/bin/bash
XPATH="$1"
find "$XPATH" -iname 'aa_res*' -mtime -7  -print0 | xargs -0 echo

Open in new window

replace  echo with rm if you are satisfied.

Author

Commented:
When I replace -Ls with -rm exec()\; it don’t like (
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
you need a space and you have it in the wrong order but I used () versus curly {}.
-exec rm {} \';
nociSoftware Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
With -print0 and xargs -0 no () or {} are needed.  And it is more efficient because only a few exec's are done by xargs. and it preserved blanks in filenames correctly.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
to noci's point
the -exe rm {} \;
the list of found files is part of the set {}
the xargs will run the rm command for each file..
in the list it appears to be a few and non space including names, so the {} \; is ...
if there are many files, it may run out of room on the line
...... and generate an error.
nociSoftware Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
xargs does know how long a command can be. It not creates ONE command, but the least amount you need to execute.
(The xargs build job determines the max size of the command line during compilation, so it will workout the right size anyway).
if the commandline is full, run one command, then start building the next... on end of input execute the remaining command.
and if needed it can be forces to a max number of arguments / execute.

man xargs   can be your friend as well

Author

Commented:
find  /test   -name  '*RES_ND*'  –mtime +1 -ls

I get error find: 0652-009 There is a missing conjunction
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
@noci
periodically I have to relook at things as bad habits may have become built-in over time.


+1 means older than one 24 hour period.

could you post the output of ls  /test

Trying to see what you have there and what the issue is
find /test -ls

Author

Commented:
find: 0652-019 The status on –ls is not valid.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
please just output the line
find /test
see what gets printed out

Author

Commented:
that works

Author

Commented:
whats issue with is then?

Author

Commented:
-exec rm {}\'; <
find: 0652-018 An expression term lacks a required parameter.
nociSoftware Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
AIX that makes a difference. (unless you install gnu tools)
 -imtime should be valid, not sure if aix knows -daystart.
Try:     -exec rm {} \;
Mind you there are spaces between each argument. and no  single '

Author

Commented:
find /test1/dev/etl/log -name 'aa_res*'

Issue with this find is in some cases we need to go under 1 more sub directory

Can we just do search till dev and then some kind of depth search after that the find that name
find /test1/dev/etl/ -name 'aa_res*'
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
-depth 1
Is an option.
Find traverses through unless restricted, if there is a oath to which the user running find lacks access, .....
Perl is a scriptable process that can be customized by including rules for your directory traversal.....
Creating function that goes through .....
CERTIFIED EXPERT
Commented:
Hi,

like noci stated, xargs is your friend in AIX so don't use -exec rm {} \;

To list the files with a modification date within the last 7 days that you want to delete:
# cd /test1/dev/etl/log
# find . -name *AA_RES* -mtime -7 -print | xargs ls -l 

Open in new window


If  the output is correct then just remove with xargs rm:
find . -name *AA_RES* -mtime -7 -print | xargs rm 

Open in new window


Cheers