Deleting files in the tree - bash

Can you help me to find trouble with my code? It has to delete every files in the tree
function option_c {
for i in `find "$TEST_DIR" -type f | grep -E "(stdout|stderr|status)-(captured|delta)"` ; do
if [ -w $i ] ; then
rm $i
fi
exit 0
done
}
Thanks for your help.

Take out the exit, it terminates your script after the first file.
You could do all of this with find itself, though.

As tripleee mentions, you can do all in find:
find "$TEST_DIR" -type f -perm +0200 -regextype posix-awk -regex ".*st(dout|derr|atus)-(captured|delta).*" -delete
or
find "$TEST_DIR" -type f -perm +0200 -regex ".*st\(dout\|derr\|atus\)-\(captured\|delta\).*" -delete
find "$TEST_DIR" -type f -perm +0200 -exec egrep "(stdout|stderr|status)-(captured|delta)" {} ";" -delete
(at least gnu-find, which is all I have, has the -delete switch and the regex-options).

Related

find printf what is correct format in crontab?

i want to save a list of files and i want to do in cron but i dont know how to convert this command
echo $(find /tmp -type f -printf "%p||%s||||||") > /share/Public/serwer/test33.data
in terminal this command works ok but in cron is smothing wrong, file is empty, why?
i tried add slashes before variables %s %p
i tried many many other combinations:
echo $(find /tmp -type f -printf "%p||%s||||||") > /share/Public/serwer/test33.data
echo $(find /tmp -type f -printf "%p||%s||||||") > /share/Public/serwer/test32.data
echo $(find /tmp -type f) > $TMP_DIR/test31.data
echo $(find $BACKUP_DIR -type f -printf \%p) > $TMP_DIR/test30.data
echo $(find $BACKUP_DIR -type f -printf %p) > $TMP_DIR/test28.data
echo $(find $BACKUP_DIR -type f) > $TMP_DIR/test27.data
echo $(find $BACKUP_DIR -type f -printf "ab") > $TMP_DIR/test26.data
echo $(find $BACKUP_DIR -type f -printf "||") > $TMP_DIR/test25.data
echo $(find $BACKUP_DIR -type f -printf "%p||%s\r\n") > $TMP_DIR/test01.data
echo $(find $BACKUP_DIR -type f -printf "\%p||\%s\r\n") > $TMP_DIR/test02.data
echo $(find $BACKUP_DIR -type f -printf "\%p") > $TMP_DIR/test03.data
echo $(find $BACKUP_DIR -type f -printf "\%s") > $TMP_DIR/test04.data
echo $(find $BACKUP_DIR -type f -printf "\\%s") > $TMP_DIR/test05.data
echo $(find $BACKUP_DIR -type f -printf "\\%p") > $TMP_DIR/test06.data
echo $(find $BACKUP_DIR -type f -printf "\r\n") > $TMP_DIR/test07.data
echo $(find $BACKUP_DIR -type f -printf) > $TMP_DIR/test10.data
echo $(find $BACKUP_DIR -type f -printf) > $TMP_DIR/test11.data
echo $(find $BACKUP_DIR -type f -printf "%p") > $TMP_DIR/test12.data
echo $(find $BACKUP_DIR -type f -printf "\%p") > $TMP_DIR/test13.data
echo `find $BACKUP_DIR -type f -printf "%p"` > $TMP_DIR/test14.data
echo `find $BACKUP_DIR -type f -printf "\\\\%p"` > $TMP_DIR/test15.data
echo `find $BACKUP_DIR -type f -printf \%p` > $TMP_DIR/test16.data
echo `find $BACKUP_DIR -type f -printf '\%p'` > $TMP_DIR/test17.data
echo `find $BACKUP_DIR -type f -printf '\\%p'` > $TMP_DIR/test18.data
echo `find $BACKUP_DIR -type f -printf '\\\%p'` > $TMP_DIR/test19.data
echo `find $BACKUP_DIR -type f -printf '\\\\%p'` > $TMP_DIR/test20.data
echo `find $BACKUP_DIR -type f -printf \%'s` > $TMP_DIR/test21.data
echo `find $BACKUP_DIR -type f -printf \%'p` > $TMP_DIR/test22.data
echo `find $BACKUP_DIR -type f -printf \%'p'` > $TMP_DIR/test23.data
echo `find $BACKUP_DIR -type f -printf \%'s'` > $TMP_DIR/test24.data
nothing work
This crontab entry should work :
* * * * * find /tmp -type f -printf "\%p||\%s||||||\n" > /share/Public/serwer/test33.data 2>/tmp/crontab.err
i added full path to command find, now it works correctly
/share/CACHEDEV1_DATA/.qpkg/Qapache/bin/find $BACKUP_DIR -type f -printf "%p||%s\r"
thanks for help
Thanks Philippe i do that and i have smothing like this:
find: unrecognized: -printf
BusyBox v1.24.1 (2021-09-23 02:31:15 CST) multi-call binary.
Usage: find [-HL] [PATH]... [OPTIONS] [ACTIONS]
Search for files and perform actions on them.
First failed action stops processing of current file.
Defaults: PATH is current directory, action is '-print'
-L,-follow Follow symlinks
-H ...on command line only
-xdev Don't descend directories on other filesystems
-maxdepth N Descend at most N levels. -maxdepth 0 applies
actions to command line arguments only
-mindepth N Don't act on first N levels
-depth Act on directory *after* traversing it
Actions:
( ACTIONS ) Group actions for -o / -a
! ACT Invert ACT's success/failure
ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2
ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2
Note: -a has higher priority than -o
-name PATTERN Match file name (w/o directory name) to PATTERN
-iname PATTERN Case insensitive -name
-path PATTERN Match path to PATTERN
-ipath PATTERN Case insensitive -path
-regex PATTERN Match path to regex PATTERN
-type X File type is X (one of: f,d,l,b,c,...)
-perm MASK At least one mask bit (+MASK), all bits (-MASK),
or exactly MASK bits are set in file's mode
-mtime DAYS mtime is greater than (+N), less than (-N),
or exactly N days in the past
-mmin MINS mtime is greater than (+N), less than (-N),
or exactly N minutes in the past
-newer FILE mtime is more recent than FILE's
-inum N File has inode number N
-user NAME/ID File is owned by given user
-group NAME/ID File is owned by given group
-size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))
+/-N: file size is bigger/smaller than N
-links N Number of links is greater than (+N), less than (-N),
or exactly N
-prune If current file is directory, don't descend into it
If none of the following actions is specified, -print is assumed
-print Print file name
-print0 Print file name, NUL terminated
-exec CMD ARG ; Run CMD with all instances of {} replaced by
file name. Fails if CMD exits with nonzero
-exec CMD ARG + Run CMD with {} replaced by list of file names
-delete Delete current file/directory. Turns on -depth option

shell script not deleting files

I am using some shell script coding , here it is
DBHOSTNAME=********
DBUSERNAME=*****
DBPASSWORD=******
DBNAME=******
BACKUPFOLDER=$HOME/*******
DELETEFILES=Y
DAILYBACKUP=Y
NUMDAILYBACKUPS=2
WEEKLYBACKUP=Y
NUMWEEKLYBACKUPS=2
MONTHLYBACKUP=Y
NUMMONTHLYBACKUPS=2
TODATE=$(date +%d)
TOMORROW=`date +%d -d "1 day"`
TODAY=$(date +%a)
MONTH=$(date +%B)
WEEK=$(date +%U)
if [ $TODATE -gt $TOMORROW ] && [ "$MONTHLYBACKUP" == "Y" ]
then
/usr/bin/mysqldump -h $DBHOSTNAME -u $DBUSERNAME -p$DBPASSWORD $DBNAME | gzip > $BACKUPFOLDER/$DBNAME'_'`date '+%m-%d-%Y'`'_'$MONTH.sql.gz
else
if [ "$TODAY" == "Sat" ] && [ "$WEEKLYBACKUP" == "Y" ]
then
/usr/bin/mysqldump -h $DBHOSTNAME -u $DBUSERNAME -p$DBPASSWORD $DBNAME | gzip > $BACKUPFOLDER/$DBNAME'_'`date '+%m-%d-%Y'`'_'Week$WEEK.sql.gz
else
if [ "$DAILYBACKUP" == "Y" ]
then
/usr/bin/mysqldump -h $DBHOSTNAME -u $DBUSERNAME -p$DBPASSWORD $DBNAME | gzip > $BACKUPFOLDER/$DBNAME'_'`date '+%m-%d-%Y'`'_'$TODAY.sql.gz
fi
fi
fi
if [ $DELETEFILES == Y ]
then
NUMWEEKLY=$[$NUMWEEKLYBACKUPS*7]
NUMMONTHLY=$[$NUMMONTHLYBACKUPS*31]
find $BACKUPFOLDER/*Sun.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
find $BACKUPFOLDER/*Mon.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
find $BACKUPFOLDER/*Tue.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
find $BACKUPFOLDER/*Wed.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
find $BACKUPFOLDER/*Thu.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
find $BACKUPFOLDER/*Fri.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
find $BACKUPFOLDER/*Sat.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
find $BACKUPFOLDER/*Week*.sql.gz -type f -mtime +$NUMWEEKLY -delete 2> /dev/null
find $BACKUPFOLDER/*January.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*February.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*March.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*April.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*May.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*June.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*July.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*August.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*September.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*October.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*November.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
find $BACKUPFOLDER/*December.sql.gz -type f -mtime +$NUMMONTHLY -delete 2> /dev/null
fi
it is working fine, but doesnt delete old files, i taken this from following address http://www.htpcbeginner.com/automatic-mysql-database-backup-on-godaddy/3/
what i need is backup of 3 days
like
Today files in folder
03/01/2017
04/01/2017
05/01/2017
and tommorow it should be
04/01/2017
05/01/2017
06/01/2017
it should delete 3jan file
Besides what the commenters have said, I think a key issue is your find command. You have, for one example:
find $BACKUPFOLDER/*Sun.sql.gz -type f -mtime +$NUMDAILYBACKUPS -delete 2> /dev/null
But that should be
find "$BACKUPFOLDER" -name '*Sun.sql.gz' -type f -mtime "+$NUMDAILYBACKUPS" -delete
When using find, you list the paths ("$BACKUPFOLDER") and the filenames (-name '*Sun.sql.gz') separately. You also single-quote the filenames to prevent the shell from expanding them.
While debugging, don't use 2> /dev/null. That discards error messages you might otherwise find helpful in solving your problem :) .

ls command and size of files in shell script

count=0; #count for counting
IFS='
'
for x in `ls -l $input`; #for loop using ls command
do
a=$(ls -ls | awk '{print $6}') #print[6] is sizes of file
echo $a
b=`echo $a | awk '{split($0,numbers," "); print numbers[1]}'`
echo $b
if [ $b -eq 0 ] # b is only size of a file
then
count=`expr $count + 1` #if b is zero , the count will increase one by one
fi
echo $count
done
I want to find 0 size files . I do that using find command. The second thing is I want to count number of has 0 size of files using ls command and awk. But It doesn't true code . What is my mistake ?
The -s test is true if a file has non-zero size. If that test fails for file, increment your empty-file count.
empty_files=0
for f in "$input"/*; do
[ -s "$f" ] || : $(( empty_files++ ))
done
Your main mistake is that you're parsing ls!
If you want to find (regular) files that are empty, and if you have a version of find that supports the -empty predicate, use it:
find . -type f -empty
Note that this will recurse in subfolders too; if you don't want that, use:
find . -maxdepth 1 -type f -empty
(assuming that your find also supports -maxdepth).
If you only want to count how many empty (regular) files you have:
find . -maxdepth 1 -type f -empty -printf x | wc -m
and if you want to perform both operations at the same time, i.e., print out the name or save them in an array for future use, and count them:
empty_files=()
while IFS= read -r -d '' f; do
empty_files+=( "$f" )
done < <(find . -maxdepth 1 -type f -empty -print0)
printf 'There are %d empty files:\n' "${#empty_files[#]}"
printf ' %s\n' "${empty_files[#]}"
With Bash≥4.4, you could use mapfile instead of the while-read loop:
mapfile -t -d '' empty_files < <(find . -maxdepth 1 -type f -empty -print0)
printf 'There are %d empty files:\n' "${#empty_files[#]}"
printf ' %s\n' "${empty_files[#]}"
For a POSIX-compliant way, use test with the -s option:
find . -type f \! -exec test -s {} \; -print
and if you don't want to recurse into subdirectories, you'll have to -prune them:
find . \! -name . -prune -type f \! -exec test -s {} \; -print
and if you want to count them:
find . \! -name . -prune -type f \! -exec test -s {} \; -exec printf x | wc -m
and here, if you want to perform both operations (count them and save them in an array for later use), use the previous while-read loop (or mapfile if you live in the future) with this find:
find . \! -name . -prune -type f \! -exec test -s {} \; -exec printf '%s\0' {} \;
Also see chepner's answer for a pure shell solution (needs minor tweaking to be POSIX compliant).
Regarding your comment
I want to count and delete [empty files]. How can I do that at the same time?
If you have GNU find (or a find that supports all the goodies):
find . -maxdepth 1 -type f -empty -printf x -delete | wc -m
if not,
find . \! -name . -prune -type f \! -exec test -s {} \; -printf x -exec rm {} \; | wc -m
Make sure that the -delete (or -exec rm {} \;) predicate is at the end! do not exchange the order of the predicates!

find: Arguments to -type should contain only one letter

#!/bin/bash
echo "please enter a path where to search:"
read myPath
touch --date "2012-01-01" /tmp/start
touch --date "2012-01-01" /tmp/end
until [ "$myPath" = $(pwd) ]
do
echo "please enter a correct path where to search:"
read myPath
done
RESULT= "$(find $HOME "-type -f" -newer /tmp/start -not -newer tmp/end)"
echo $RESULT
When I'm trying to execute it I'm getting:
find: Arguments to -type should contain only one letter
TimeStamp: line 17: : command not found
For starters, why do you have -type -f outside the quotes? And it's
-type f not -type -f (no dash before the f)
Try this:
"$(find $HOME -type f -newer /tmp/start -not -newer tmp/end)"
I would try this:
"$(find $HOME -type f -newer /tmp/start -not -newer tmp/end)"
You have:
RESULT= "$(find $HOME "-type -f" -newer /tmp/start -not -newer tmp/end)"
find: Arguments to -type should contain only one letter
Several things: Remove the quotes around -type -f. I'm not sure why you put them there. The correct argument for the -type parameter should be f and not -f.
TimeStamp: line 17: : command not found
You did a touch on /tmp/end, but you're looking at tmp/end. This is saying the same thing as $PWD/tmp/end. You need a slash at the front to anchor this to the root of the directory structure. You need /tmp/end and not tmp/end.
Try:
RESULT= "$(find "$HOME" -type f -newer /tmp/start -not -newer /tmp/end)"

bash: How to delimit strings to find files

What syntax should I use in a bash script to list files based on 3 dynamic values:
- older than X days
- in a specified directory
- whose name contains a specified string?
FILEAGE=7
FILEDIR='"/home/ecom/tmp"'
FILESTRING='"search-results-*"'
FILES_FOR_REMOVAL=$("/usr/bin/find "${FILEDIR}" -maxdepth 1 -type f -mtime +${FILEAGE} -name "${FILESTRING}" -exec ls -lth {} \;")
echo ${FILES_FOR_REMOVAL}
If I try the above I get:
-bash: /usr/bin/find "/home/ecom/tmp" -maxdepth 1 -type f -mtime +7 -name "search-results-*" -exec ls -lth {} \;: No such file or directory
Remove superfluous quotes:
FILEAGE=7
FILEDIR='/home/ecom/tmp'
FILESTRING='search-results-*'
FILES_FOR_REMOVAL=$(/usr/bin/find "${FILEDIR}" -maxdepth 1 -type f -mtime +${FILEAGE} -name "${FILESTRING}" -exec ls -lth {} \;)
Your syntax for 'find' looks ok. Try removing the quotes around the command string, i.e.
FILES_FOR_REMOVAL=$(/usr/bin/find "${FILEDIR}" -maxdepth 1 -type f -mtime +${FILEAGE} -name "${FILESTRING}" -exec ls -lth {} \;)
FILEAGE=7
FILEDIR='/home/ecom/tmp'
FILESTRING='search-results-*'
/usr/bin/find "${FILEDIR}" -maxdepth 1 -type f -mtime +${FILEAGE} -name "${FILESTRING}" -exec /bin/ls -lth '{}' \;
There were some extra quotes that created the error. Also specify full path to /bin/ls to avoid problems with potential aliasing of ls(1). And to get filenames on a separate line, I dropped the $FILES_FOR_REMOVAL variable. You can also use
/usr/bin/find "${FILEDIR}" -maxdepth 1 -type f -mtime +${FILEAGE} -name "${FILESTRING}" -ls
(I can't add comments, but ... )
To reliably handle file names with spaces, you may want to consider storing the file list in a temp text file instead of a variable and loop through it using a while construct (instead of a for)
For example:
FILEAGE=7
FILEDIR='/home/ecom/tmp'
FILESTRING='search-results-*'
TEMPFILE=".temp${RANDOM}"
CMD="find \"${FILEDIR}\" -maxdepth 1 -type f -mtime +${FILEAGE} -name \"${FILESTRING}\" -exec /bin/ls -lth '{}' \;"
$CMD > $TEMPFILE # write output to file
while read thefile; do
do_somthing_to $thefile
done < $TEMPFILE
rm $TEMPFILE # clean up after
Or, if you're only going to use the list once, pipe the output directly to the while construct:
$CMD | while read thefile; do
do_something_to $thefile
done

Resources