Bash script to delete files

Need a command OR script to delete old files from date 01 to 26 of every month exclude last 02 months

The find and stat commands could be your best option in a bash script
Something like the following, but tested and with safety bars and drop the echo if the output makes sense.

for file in $(find ./ -mtime +60 -type f ); do # assuming a 60 day 2 month is dangerous here
   if  [ $(stat $file | grep Modify | cut -d\  -f2| cut -d\- -f3 <27 ] ;  then
      echo rm $file
   fi
done

Welcome @refra1 !

I think it should be
) -lt 27
But there are still some flaws in it: locale must be "C" or "en", file names must not have special characters.
The following is safer (still cannot handle a newline in a file name):

find . -type f -mtime +60 -exec stat --format=\%y" "\%n {} + | while read -r date time tz fn; do if [ "${date##*-}" -le 26 ]; then echo rm "$fn"; fi; done

Remove the echo to really run the rm
I use standard shell and \% and one line, so it is fit for crontab.

The following script is 100% safe:

#!/bin/sh
find "${@:-.}" -type f -mtime +60 -exec /bin/sh -c '
for fn
do
  dt=$( stat --format=\%y "$fn" )
  date=${dt%%" "*}
  if [ ${date##*-} -le 26 ]; then 
    echo rm "$fn"
  fi
done
' script.sh {} +

Script arguments are the start directories.

Showing following error
./del-files.sh: line 7: [: missing `]'
./del-files.sh: line 7: 27: No such file or directory
cut: the delimiter must be a single character

See explanation from @MadeInGermany below @Skrynesaver's post, using <27 is incorrect and causes the errors.

So what is correct code or script

Firstly, this line has a missing ) after the -f3, needed to match the opening $(.

if [ $(stat $file | grep Modify | cut -d\ -f2| cut -d\- -f3 <27 ]

You should consider running all your scripts through shellcheck. This is available to download, or as a web service.

The script with the error throws:

$ shellcheck kFoo

In kFoo line 2:
   if  [ $(stat $file | grep Modify | cut -d\  -f2| cut -d\- -f3 <27 ] ;  then
       ^-- SC1009: The mentioned parser error was in this test expression.
         ^-- SC1073: Couldn't parse this command expansion.
                                                                              ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.

$ 

There is online help for every error code. For example,

 https://github.com/koalaman/shellcheck/wiki/SC1000

which also explains how to obtain and use ShellCheck.

Without that ), the <27 is part of the cut command, not part of the test. It then becomes a redirection of stdin to a file whose name is 27, which does not exist.

Also, arithmetic comparisons use -lt, not <.

@MadeInGermany already posted both those corrections (9 days ago), which you seem to have skipped. I'm just explaining the details, and suggesting you use the diagnostic tools available.

Could you expand on what you mean by "Last two months"? If you ran this script on 10th November, would you expect it to partially delete files from September 01 to 09, or prefer it to only deal with complete months (i.e. delete August 01 to 26, but leave September and October untouched until you run the script in December?

for file in $(find ./ -mtime +60 -type f );
do

if [ $(stat $file | grep Modify | cut -d\ -f2| cut -d "-" -f3) lt <27];
then
echo "rm $file"
fi
done

Give error like:
./1.sh: line 11: 27]: No such file or directory
cut: the delimiter must be a single character
Try `cut --help' for more information.

Please read my post #3. And take one of my suggestions.

I request you to plz help me and correct my script

Most likely,

#!/usr/bin/env bash
while read -r filepath; do
    if (( $(date -d @"$(stat -c '%Y' "$filepath")" +%_d) < 27 )); then
        echo rm "$filepath";
    fi;
done < <(find ./ -type f -mtime +60)

or

#!/usr/bin/env bash
while read -r filepath; do
    if [[ $(date -d @"$(stat -c '%Y' "$filepath")" +%_d) -lt 27 ]]; then
        echo rm "$filepath";
    fi;
done < <(find ./ -type f -mtime +60)

would do what you want. But remember to remove the echo ONLY AFTER you made sure, that listed are only the files you actually wanted to remove. Try to understand the code BEFORE running it. Do not copy-paste anyone's code mindlessly and avoid running it without complete comprehension. You've been warned :wink:

FP="/DIRA/WD"

while read -r $FP; do
    if [[ $(date -d @"$(stat -c '%Y' "$FP")" +%_d) -lt 27 ]]; then
        echo rm "$FP";
    fi;
done < <(find ./ -type f -mtime +60)


./1.sh: line 8: read: `/DIRA/WD': not a valid identifier

Not quite. Make sure you understand parameter / variable expansions (and what it's used for, how to use it correctly and where it can be applied).

#!/usr/bin/env bash

# next line forces the script to exit immediately if it failed to cd into /DIRA/WD
cd "/DIRA/WD" || exit 1;

while read -r FP; do
    if [[ $(date -d @"$(stat -c '%Y' "$FP")" +%_d) -lt 27 ]]; then
        echo rm "$FP";
    fi;
done < <(find ./ -type f -mtime +60)

or

#!/usr/bin/env bash

SD="/DIRA/WD" # SD as in "Search-through Directory"
# exit immediately if "$SD" doesn't point to (correct path of) an existing directory
if [[ ! -d "${SD}" ]]; then exit 1; fi

while read -r FP; do
    if [[ $(date -d @"$(stat -c '%Y' "$FP")" +%_d) -lt 27 ]]; then
        echo rm "$FP";
    fi;
done < <(find "${SD}" -type f -mtime +60)