Shell Script to compress all the log files in "my_dir" directory after the configurable number of hours or days [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I want a shell script to compress all the log files in "my_dir" directory after the configurable number of hours or days.
I have tried with the following script but it didn't work :
find /path/to/directory -mtime +2 -exec ls "{}" \
find /path/to/directory -mtime +2 -exec ls "{}" \;
The script is getting executed but no action is done.

Maybe you just want configure the logrotate on server, see this link: https://www.thegeekstuff.com/2010/07/logrotate-examples
And if you want execute at any time, you can call for an execution with this command (but it's unnecessary if the config file had correctly configurated):
#: logrotate /path/to/my/logRotateFile.conf

You can try this command for hours configuration:
find /path/to/directory/*.log -type f ! -name "*.tar.gz" -mmin +$[$COMPRESS_HOURS * 60] -exec mv '{}'
COMPRESS_HOURS are configurable
And for days
find /path/to/directory/*.log -type f ! -name "*.tar.gz" -mtime +$days -exec mv '{}'
days are configurable

Related

linux shell script to copy directory tree and link files [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to be able to create a copy of a directory tree and soft link the files on it.
For example, from
/home/user/origin/a/sub/file.txt
I would like to get
/home/user/destination/a/sub/file.txt
being this one a link to the original file.txt.
I tested with
find /home/user/origin/ -type d -printf "mkdir -vp '/home/user/destination%p'\n" -o -type f -printf "ln -vs '%p' '/home/user/destination%p'\n" | sh
but it has two problems:
I'd like to copy from origin to destination, and it copies from origin to /home/user/destination/home/user/origin. It is not a biggie, as I can move that afterwards
If the file name is something like
In Fifty Years We'll All Be Chicks.txt
It stops working because the '.
Assuming I understand what you're trying to do, it seems easier to just use -exec
find /home/user/origin/ \
-type d -exec sh -c 'mkdir -v "/home/user/destination/${0#/home/user/origin/}"' {} \; \
-o \
-type f -exec sh -c 'ln -vs "$0" "/home/user/destination/${0#/home/user/origin/}"' {} \;
Note -or having lower precedence than the implied -and's is important here.

Linux find with prune and negation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to grep all files in a directory except for
subdirectories of lib
images (png and jpg)
I'm doing it in a shell script, passing the arguments to grep, no problem.
This command excludes the subdirectories of lib
find src \
-name lib -prune -o \
-type f -exec grep -P "$#" {} +
and this one excludes the images
find src \
! -name "*.jpg" ! -name ".png" \
-type f -exec grep -P "$#" {} +
Put together as
find src \
-name lib -prune -o \
! -name "*.jpg" ! -name ".png" \
-type f -exec grep -P "$#" {} +
it fails to exclude the images. Any idea what's going on?
It fails to exclude png images because you left out the * in -name "*.png".
A generally useful approach is to filter results via grep after a pipe, this decreases complexity in the original command, so:
find [simplified find options] | egrep -v ".jpg|.png"

Deleting large number of files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I'm writing a script to delete large number of files in linux. The script will run every two minutes in crontab. I tried using the
find /mnt/md0/capture/DCN/ -maxdepth 1 -type f -name "*.pcap" -print0 | xargs -0 ls -lt | tail -$del | awk '{print $8}'
and saving this to a variable and tried to delete using rm command, but dint run. I have found out that find . -type f -delete can be used to delete the files, but I don't know how to add this part to my command. How would I do this?
Just replace -print0 with -delete:
find /mnt/md0/capture/DCN/ -maxdepth 1 -type f -name "*.pcap" -delete

chmod exclusions [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I know I should assign a group and then set an umask so that groups writable permissions persist but for whatever reason I can't do this. I need to chmod recursively a directory except one sub folder (web10), would the following work?
cd /var/www/clients/
find . -type f -not -path "*web10*" -exec chmod 777 '{}' \;
If you want to exclude files or directories, you use -prune
find /var/www/clients/ -name web10 -type d -prune -o -type f -print0 | xargs -0 chmod 0640
You should also use xargs where possible. With -exec you call the command once for every file found, whereas xargs collects as many files as possible and calls the command once for N files, resulting in a more efficient execution and better performance.

exclude directories mv unix [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
The command below moves every hidden/normal file ending with *string without . or _ before it.
mv {.,}*[!._]string /destination
How can I also exclude moving all directories in the above command?
Try
find /WHERE/TO/FIND -name '*STRING' \( ! -name '*_STRING' -o ! -name '*.STRING' \) -type f -exec mv \{\} /WHERE/TO/MOVE \;
Note, if you want to move every file from only the /WHERE/TO/FIND directory, you should add -maxdepth 1 (after e.g. the -type f part).
How about:
for file in {.,}*[!._]string; do test -f "$file" && mv "$file" /destination; done
In what shell does the [!._] glob actually work when used with {.,}? You would probably be better off avoiding the {} notation and do:
for file in .*[!._]string *[!._]string; do ... ; done

Resources