How to get deleted files into a log file. Bash script [duplicate] - linux

This question already has answers here:
Linux find and delete files but redirect file names to be deleted
(5 answers)
Closed 1 year ago.
So im using the script
find /path/to/files/* -mtime +60 -exec rm {} \;
How can i collect the deleted files and transfer them into a logfile in Bash script

You could do something like:
find /path/... -print ... | tee -a <log.file>
The -print will print out all the hits, and the tee will append that to some log.file.
Side note: the * at the end of your /path/to/files/* seems superfluous.
Side note2: if you just want to delete the files, find has a built-in -delete.

Related

Production nearly out of space - How to clear logs from container? [duplicate]

This question already has answers here:
Docker: How to clear the logs properly for a Docker container?
(22 answers)
Closed 1 year ago.
The production environment is getting out of space and I just found out using this command that the logs are taking a ton of space:
> foo#bar:/var/lib/docker$ sudo sh -c "du -ch /var/lib/docker/containers/*/*-json.log" | grep total
80G total
My question now is how do I clear all of this logs? I'm not able to acess the /var/lib/docker/containers via cd.
Also, is there any way to check how old these logs are? I would like to keep the logs from the last 30 days.
You can delete old files with this oneliner:
find /var/lib/docker/containers/ -type f -mtime +30 \
-name '*-json.log' -prune -exec rm -vf '{}' \;
The code searchs recursively in the directory /var/lib/docker/containers/ for files whose name matches '*-json.log' and their modification is greater than 30 days and then removes them.

A script that deletes all the regular files (not the directories) with a .js extension that are in the current directory and its subfolders [duplicate]

This question already has answers here:
How to loop through a directory recursively to delete files with certain extensions
(16 answers)
Closed 2 years ago.
Write a script that deletes all the regular files (not the directories) with a .js extension that are present in the current directory and all its subfolders.
The answer should only contain one command after the shebang line, I've tried the following:
#!/bin/bash
rm -R *.js
… and:
#!/bin/bash
rm -f *.js
find . -name "*.js" -delete
Find all files in the current and child directories with the extension .js and delete the files.
The best way to achieve this remains the find command:
find . -type f -name '*.js' -exec rm -f {} \;
If however you want to stick to rm alone, it remains possible if you know exactly how many subdirectories lie under the place you're working in. For instance:
rm -f */*/*.js

Archiving Shell Script [duplicate]

This question already has answers here:
Find and xargs to correctly handle filenames with spaces in their names
(3 answers)
Closed 3 years ago.
I'm very new to shell script and I'm having issues with archiving files.
I have a folder with .xlsm files and I want those files that passed the retention period. I was able to archive except I'm having issues with those files having spaces with their filename eg. X y z.xlsm. below is my sample code.
find ${work_dir} -type f -mtime +${retention} | xargs tar -cvf ${Destination}/archive.tar
Any idea how to achieve it?
Thanks!
Use NUL as delimiter (-print0 for find, and --null for xargs).
find ${work_dir} -type f -mtime +${retention} -print0 | xargs --null tar -cvf ${Destination}/archive.tar

Deleting files that are older than one day [duplicate]

This question already has answers here:
find files older than X days in bash and delete
(3 answers)
Closed 7 years ago.
I have a server which creates several log files in the log directory. Due to this logging mechanism it eats up a lot of disk space on my server. I want to write a script that deletes all the files that are older than one day and keep the latest ones.
I am able to list the directories in sorted form using ls -trl command. But I am not able to understand how to remove these files. Please help.
You can use the following command:
/usr/bin/find <Your Log Directory> -mtime +1 | xargs rm -f
mtime - provides the file modification time.
+1 - indicates greater than one day.
Try using rm and find command like:
find . -mmin +$((60*24)) -exec rm {} \;
You don't want ls, you want find.
It has a neat argument, -mtime, that limits the results to a specific time delta, and -exec which allows you to provide a command to run on the results.
So for example,
find -mtime +10 -name "*tmp*" -exec rm {} \;
Does an rm on all files older than 10 days, with tmp in the name.
Oh, and be careful.
Very careful.
find . -mtime +1 -exec rm {} \;

what does {} \; mean in a unix command? [duplicate]

This question already has answers here:
Unix find command, what are the {} and \; for?
(5 answers)
Closed 10 years ago.
Noob question
For example:
find /home/user/demo -type f -perm 777 -print -exec chmod 755 {} \;
I think I understand what this code does - it changes file permissions from 777 to 755 recursively. But I don't understand where the {} \; comes from. Is that bit part of exec or find or what?
Thanks for any help.
The curly brackets are placeholders used by the find command to know where to insert the file name of the file it is currently working with.
The find command looks for a ";" to show where the command it must execute for every file ends.
We put a \ in front of it (called escaping) so the shell(ie. bash) wont interpret it, and hide it from find.

Resources