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 {} \;
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 \;
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
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 {} \;
Here is my command line:
find . -type f -exec file {} \; \
| sed 's/\(.*png\): .* \([0-9]* x [0-9]*\).*/\2 \1/' \
| sed 's/\(.*jpg\): .* \([0-9]*x[0-9]*\).*/\2 \1/' \
| awk 'int($1) < 1000' \
| sed 's/^.*[[:blank:]]//' \
| tar -czvf images.tar.gz --null -T -
And the error i got is:
tar: Unix\n./test.png\n./test2.jpg\n: Cannot stat: No such file or
directory
tar: Exiting with failure status due to previous errors
What i want is to find all images in current directory, who's width less than 1000 px and tar them into archive.
to use --null, you need to convert newlines to nulls first:
...
| tr '\n' '\0' \
| tar -czvf images.tar.gz --null -T -
(tested, working.)
also, here are a number of suggestions on speed and style in decreasing order of importance.
a. don't find and run file on more files than you need to:
find . -type f -iname "*.png" -or -iname "*.jpg"
b. for commands that can run on multiple files per command, such as file, use xargs to save a lot of time:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 | xargs -0 file
c. if you put | at the end of each line, you can continue on the next line without also using \.
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file
d. you can save yourself a lot of trouble since your max width is 999 by just greping for 1, 2, or 3 digit widths, though the awk '$1<1000' is ultimately better in case you ever want to use a different threshold:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x '
e. grep and awk are faster than sed, so use them where possible:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x ' |
grep -o -i '.*\.\(png\|jpg\)'
final command:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x ' |
grep -o -i '.*\.\(png\|jpg\)' |
tr '\n' '\0' |
tar -czvf images.tar.gz --null -T -
You can also use awk only with :
find . -type f \( -name "*.png" -or -name "*.jpg" \) -exec file {} \; | awk -v width_limit=1000 '
{
match($0, /,\s+([0-9]+)\s*x\s*([0-9]+)/, items)
if (items[1] < width_limit){
match($0, /(.*):/, filename)
print filename[1]
}
}' | tar -czvf allfiles.tar -T -
The width can be configured with width_limit variable
Quick way using perl:
find . -type f -exec file {} + |
perl -ne '
print $1."\0" if /^(.*):\s*(JPEG|PNG).*,\s*(\d+)\s+x\s*\d+\s*,/ &&
$3 < 1000;
' | tar -czvf images.tar.gz --null -T -
Using + operator to find as same effect than print0 | xargs -0.
OS: Linux RedHat
Bash: 3.5
I have 2 commands below to get list of files with status of them and another command for footprint.
I want to find the way to combine them together in single line.
Here's my mentioned commands.
find "$PWD" -type f ! -iname '*thumbs.db*' -print0 | xargs -0 stat -c "%y %s %n"
find "$PWD" -type f -print0 | xargs -0 sha1sum -b
Will this work? Do a man on xargs.
find $PWD -type f ! -iname '*thumbs.db*' -print0 | xargs -0 -I '{}' sh -c 'stat --printf "%y %s %n " {} ; sha1sum -b {}'
If you do not want the file name repeated twice:
find $PWD -type f ! -iname '*thumbs.db*' -print0 | xargs -0 -I '{}' sh -c 'stat --printf "%y %s %n " {} ; sha1sum -b {} | cut -d\ -f1'
There needs to be 2 blank spaces after d\ in cut command.
You can do this with -exec in find command itself.
find $PWD -type f ! -iname '*thumbs.db*' -exec stat -c "%y %s %n" {} \; -exec sha1sum -b {} \;