Unix - arg list too long - bash

Using bash 3.2. Trying to delete some log files older than 7 days...anyways this command works on another server but not on the current one.
Wondering if anyone can fix the syntax for me as I'm no Unix expert:
find /export/home1/dir1/dir2/sync/logs/* -mtime +7 -exec rm -f {} \;

Remove * from path of find:
find /export/home1/dir1/dir2/sync/logs/ -mtime +7 -exec rm -f {} \;
or if you newer find version:
find /export/home1/dir1/dir2/sync/logs/ -mtime +7 -delete
By having * in path shell expands to all the available entries in the given directory.

Related

Delete files that are 5 days old using bash script

I currently use a command that searches a directory and deletes 5 day old files.
find /path/to/files* -mtime +5 -exec rm {} \;
I run it from the command line and it works fine. But when I put it in a .sh file it says findĀ /path/to/files*: No such file or directory.
There is only two lines in the shell script:
#! /usr/bin/env bash
find /path/to/files* -mtime +5 -exec rm {} \;
How can I rewrite the script so that it works?
`
The error happens if there are currently no files matching the wildcard, presumably because none have been created since you deleted them previously.
The argument to find should be the directory containing the files, not the filenames themselves, since find will automatically search the directory. If you want to restrict the filenames, use the -name option to specify the wildcard.
And if you don't want to go into subdirectories, use the -maxdepth option.
find /path/to -maxdepth 1 -type f -name 'files*' -mtime +5 -delete
This works:
#! /usr/bin/env bash
find /home/ubuntu/directory -type f -name 'filename*' -mtime +4 -delete
Here is an example:
find /home/ubuntu/processed -type f -name 'output*' -mtime +4 -delete

Deleting files from an AIX system

We have an AIX system, which gets files on a daily basis, so we manually delete the previous days files manually. Is it possible to write a script which will take the files 15 or 20 days before today and delete the files from the folder?
Or you can use native AIX find command:
find /dir/to/files -type f -mtime +15 -exec rm {} \;
where:
-type f - Find only files, not directories
-mtime +15 - Find files, that modification time more then 15 days
-exec rm {} \; - Run command rm on each matched file
You can run this command with -exec ls -l {} \; for testing, that found files correspond to your criteria.
If you can/may install GNU!find, them it's simple, e.g.:
#!/bin/sh
cd /var/log/apache
gfind . -name '*log*Z' -mtime +30 -delete
this script is run by cron; a line from crontab:
02 23 1 * * /root/cmd/httpd.logdelete >/dev/null 2>&1
Edit:
-mdays + means files of which last modification date is earlier than now-
-delete means deleting the files that match the criteria

execdir and rename commands together?

I ask a question about finding, copying and renaming which can be found here Find, copy, rename within the same directory
The answer was great and solved the issue I had in that thread but it did bring up another question about how I can rename just part of the file....for example when running this command;
find /home/ian/Desktop/TEST/ -type f -mmin -1 -execdir echo cp \{} \{}_backup \;
and the file is called TEST_MASTER how can you run the above and have the new file called TEST_BACKUP as opposed to TEST_MASTER_BACKUP?
I can solve this by running a new rename command straight after like below;
find /home/ian/Desktop/TEST/ -type f -mmin -1 -execdir cp \{} \{}_backup \; ;
rename __MASTER_backup _backup *MASTER_backup ;
but there must be a way to do this in one go?
All the best,
Ian
You can use this find command:
find /home/ian/Desktop/TEST/ -type f -mmin -1 -execdir bash -c 'cp "$1" "${1%%_*}_BACKUP"' - '{}' \;
I came with almost the same answer as anubhava:
find /home/ian/Desktop/TEST/ -type f -name '*_MASTER' -mmin -1 -execdir \
bash -c 'mv $1 ${1/MASTER/BACKUP}' - \{} \;
This will only backup *_MASTER files. If you need to backup the other files as well (and add an extra _BACKUP at the end, vote for anubhava!

find files older than X days in bash and delete

I have a directory with a few TB of files. I'd like to delete every file in it that is older than 14 days.
I thought I would use find . -mtime +13 -delete. To make sure the command works as expected I ran find . -mtime +13 -exec /bin/ls -lh '{}' \; | grep '<today>'. The latter should return nothing, since files that were created/modified today should not be found by find using -mtime +13. To my surprise, however, find just spew out a list of all the files modified/created today!
find your/folder -type f -mtime +13 -exec rm {} \;
This works for me.
$ find ./folder_name/* -type f -mtime +13 -print | xargs rm -rf
The simplest solution to this is in #navid's and #gniourf_gniourf's comments. Because it's buried in the comments, I'd like to bring it up to be more visible.
find your/folder -type f -mtime +13 -delete
This avoids any possible issues with spaces and whatnot in the filenames and it doesn't spin up another executable to do the deleting so it should be faster too.
I tried and tested this.

Trying to remove a file and its parent directories

I've got a script that finds files within folders older than 30 days:
find /my/path/*/README.txt -mtime +30
that'll then produce a result such as
/my/path/jobs1/README.txt
/my/path/job2/README.txt
/my/path/job3/README.txt
Now the part I'm stuck at is I'd like to remove the folder + files that are older than 30 days.
find /my/path/*/README.txt -mtime +30 -exec rm -r {} \;
doesn't seem to work. It's only removing the readme.txt file
so ideally I'd like to just remove /job1, /job2, /job3 and any nested files
Can anyone point me in the right direction ?
This would be a safer way:
find /my/path/ -mindepth 2 -maxdepth 2 -type f -name 'README.txt' -mtime +30 -printf '%h\n' | xargs echo rm -r
Remove echo if you find it already correct after seeing the output.
With that you use printf '%h\n' to get the directory of the file, then use xargs to process it.
You can just run the following command in order to recursively remove directories modified more than 30 days ago.
find /my/path/ -type d -mtime +30 -exec rm -rf {} \;

Resources