having the following bash code on Linux, how I can modify it to append the datestamp after gz accomplished his process?
DOMINIO=filenetvers
DATAORA_ATTUALI=$(date +"%Y.%m.%d")
GGZIP=1
GGRM=90
find /work/pr-${DOMINIO}-0[0-2]/servers -name "*.log*[^gz]" -type f -user bea -mtime +${GGZIP} -exec /usr/bin/gzip -9 -f {} "*.gz.$DATAORA_ATTUALI" \;
find /work/pr-${DOMINIO}-0[0-2]/servers -name "*.stdout*[^gz]" -type f -user bea -mtime +${GGZIP} -exec /usr/bin/gzip -9 -f {} "*.gz.$DATAORA_ATTUALI" \;
find /work/pr-${DOMINIO}-0[0-2]/servers -name "*.stderr*[^gz]" -type f -user bea -mtime +${GGZIP} -exec /usr/bin/gzip -9 -f {} "*.gz.$DATAORA_ATTUALI" \;
This to view your output from find:
find ./ -type f -name "nsshow*" -exec echo cp {} /tmp/{}_test \;
cp ./nsshow_SANSW06_FABB /tmp/./nsshow_SANSW06_FABB_test
cp ./nsshow_SANSW02_FABB /tmp/./nsshow_SANSW02_FABB_test
cp ./nsshow_SANSW05_FABA /tmp/./nsshow_SANSW05_FABA_test
cp ./nsshow_SANSW01_FABA /tmp/./nsshow_SANSW01_FABA_test
This to run it:
find ./ -type f -name "nsshow*" -exec cp {} /tmp/{}_test \;
The above "for" loop is nice and simpel for Advanced work:
for f in $(find /work/pr-${DOMINIO}-0[0-2]/servers -name "*.log*[^gz]" -type f -user bea -mtime +${GGZIP})
do
/usr/bin/gzip -9 -f $f
mv $f.gz $f.gz.$DATAORA_ATTUALI
done
A simple solution is to use a simple for loop and do the gzip and rename like this :
for f in $(find /work/pr-${DOMINIO}-0[0-2]/servers -name "*.log*[^gz]" -type f -user bea -mtime +${GGZIP}) ; do /usr/bin/gzip -9 -f $f ; mv $f.gz $f.gz.$DATAORA_ATTUALI; done
Related
I would like to know is there any way to show the message with "echo function" + "find command" output into log file?
Current:
/mnt/backup/XXXX/Daily/Logs/20210326.log
Code:
logfile=$(date +"%Y%m%d")
find /mnt/backup/XXXX/Daily/Logs -type f -name "*.log" -mtime +6 -print -exec rm {} \; >> /mnt/backup/XXXX/Daily/Logs/$logfile.log
Expected result:
Deleted file - /mnt/backup/XXXX/Daily/Logs/20210326.log
Remark: Bold text is belong to echo function. Normal text is belong to find command.
Use a second -exec and so:
find /mnt/backup/XXXX/Daily/Logs -type f -name "*.log" -mtime +6 -print -exec rm {} \; -exec echo 'Deleted file - {}' >> /mnt/backup/XXXX/Daily/Logs/$logfile.log \;
It's easy enough to do it with tar:
date=`date +%F-%T`;find /var/log/nginx -name "access.log" -mtime -1 -type f -print | xargs -0 tar czvf /tmp/$date-access.tar.gz
.
$ ls /tmp
2019-05-03-11:25:49-access.tar.gz
How do I do this with gzip?
find + bash + gzip solution:
$ d=$(date +%F-%T)
$ find /var/log/nginx -name "access.log" -mtime -1 -type f \
-exec bash -c 'gzip -c "$2" > "/tmp/$1-access.gz"' _ $d {} \;
How can i remove folder with crontab? i create new file called delete_old_file.sh and calling in contrab
* * * * * . ~/delete_old_files.sh
i already try with -type d but still not working
find ~/dev/test -type f \( -name "*.gz" -o -name "*.tar" -o -name "*.zip" \) -or -type -d -mtime +7 -exec rm -rf {} +
For now only zip and file can remove
find ~/dev/test -type f \( -name "*.gz" -o -name "*.tar" -o -name "*.zip" \) -mtime +7 -exec rm -rf {} +
did i missed some command?
thanks.
Use the second form and change the last + to \;
i put in two separate line:
find ~/dev/test -type d -mtime +7 -exec rm -rf {} \;
find ~/dev/test -type f ( -name ".gz" -o -name ".tar" -o -name "*.zip" ) -mtime +7 -exec rm -rf {} +
I am working on linux bash. Now I would like to clear the contents of all .txt files. However, this command "find -type f -iname '.txt' | xargs -I {} echo ""> {}" seems not to work. Any suggestions? Any ideas about better solutions?
I replaced echo with truncate in order to clear a file and used find's -exec instead of piping to xargs:
find . -type f -name "*.txt" -exec truncate -s 0 {} \;
I have a script which deletes files older than +2 days in a specific Directory.
I would like to check if there is a file with todays date created before removing the older files.
This is what I have:
#!/bin/bash
find /var/backups/server1 -type f -mtime +2 -exec rm {} \;
find /var/backups/server2 -type f -mtime +2 -exec rm {} \;
find /var/backups/server3 -type f -mtime +2 -exec rm {} \;
find /var/backups/server4 -type f -mtime +2 -exec rm {} \;
find /var/backups/server5 -type f -mtime +2 -exec rm {} \;
So Basically:
1.Check Directory with file with todays date.
2.If affirmative find /var/backups/serverX -type f -mtime +2 -exec rm {} \;
3.If not "execute scriptX" (which maybe a mail notification)
thanks!
You could do something like this
find /var/backups/ -maxdepth 1 -type d -print0 | while read -rd '' dirname
do
arry=( $(find "${dirname}" -type f -atime 0) )
#Checks if there is a file that is updated today.
[ "${#arry[#]}" -ge 1 ] && find "${dirname}" -type f -mtime +2 -exec rm {} \;
done