How to avoid find command in Linux from throwing errors - linux

I know the way to delete older files using the find command is:
find /mydir/typ* -type f -mtime + 5 -delete
However if it doesn't find a file, it returns an error saying no matches found. Is there a way to just fail silently, i.e. not to throw an error if it can't find the file. If it does, delete it.

find /mydir/typ* -type f -mtime + 5 -delete 2> /dev/null

So, from your comment on another answer, your full error is zsh: no matches found. The error is coming from your shell, not find.
/mydir/typ* is a shell glob, and zsh by default gives you an error if no files match the glob. More info on that here.
It's not clear what your directory structure and intended use are, but if you want to find files matching typ* in /mydir, you want find /mydir -name 'typ*' -type f -mtime +5 -delete. Otherwise, you'll have to be more specific about your situation.

Related

What does this cron do for each command?

find /home/root/public_html/_sess -type f -mtime +3 -name 'sess-*' -execdir rm -- {} \;
I feel like I understand find , but I'm not 100% sure what -type is, I think that is the file type f not sure yet -mtime I feel like -mtime means a time setting of some sort, and +3 means maybe that time setting +3? , I feel like -execdir rm -- just means remove the files in the directory call -name 'sess-*' as well. But again not 100% sure of all the command elements within and wanted to get clarification.
You can do man find to get information on how Linux find works and all the options you can pass to it.
In this case, the command is using the Linux find utility to search for files in the /home/root/public_html/_sess directory with the following options:
-file f - searches for files of filetype f, which is regular files (not directories, links, etc)
-mtime +3 - searches for files modified more than 3 days ago (the + is for more than, -3 would be less than 3 days old)
-name 'sess-* - searches for files whose name matches the regex sess-* (name starts with "sess-")
-execdir <command> {}; - executes <command> on each file that find finds in the directory that the file was found in, in this case <command> is rm to remove the file
So in summary, this job searches for files located in a certain directory, whose names start with a specific string, and which are more than 3 days old, and deletes them.

How do I find a file containing using wildcard on Linux?

I would like to know if it is possible to use find with wildcards:
I use this command, but I have an error
find -type f -name /target/*.zip
You need to put the wildcard in quotes, otherwise it gets expanded by the shell before the command is run.
And it should just be a filename, not a pathname. The directory to start searching should be an argument to find before the filter specifications.
find /target -type f -name '*.zip'

Find by -name and -mtime, returns nothing

I am trying to use find command to delete some old file from backup folder but the find command return nothing, and so nothing is being removed! this is the code (find part), my system is ubuntu 18.04 LTS
find -name "*.sql" -type f -mtime +30
the result of find command
and the output of ls -l command is :
the result of ls -l command
I googled and searched the web but did find nothing to solve the problem. any help appreciated.
You are missing the starting point for your find command, in this case . because you already execute the command in the target directory:
find . -name "*.sql" -type f -mtime +30
the rest can stay the same.
First make sure it gives you the correct result and afterwards you can tack on the -exec to execute a command for each line of the result.
find . -name "*.sql" -type f -mtime +30 -exec rm '{}' ';'
You can usually find such answers on the UNIX stackexchange: How to execute ln on find results
Please see the comment from David in this particular case it might be a misunderstanding of the mtime parameter.
I have tested exactly the commands that were listed here, below you see
my preparation and some multiple usage, you can see how the files show up as expected, every time the mtime value decreases:
VIRTUAL BOX UBUNTU LTS 18.04
which is no surprise, given the man page of the find command:
FIND MAN PAGE / -mtime PARAMETER
please check for any typos... this should work.

finding files that were modified before a certain file

Hello I am trying to write a unix code that will find all the files which were modified before a certain
file.
So far I have,
find [directory] -type f -mtime date -r [certain_file]
but the system return an error saying
find: invalid argument `date' to `-mtime'
I am trying to write this in one line and I just can't seem to figure out a way to specify the timestamp
of [certain_file].
How can I express
-mtime {date -r [certain_file]}?
Try the negation of find's -newer condition.
find [directory] -type f \! -newer [certain_file]

How to delete files and directories older than n days in linux

I have a directory named repository which has a number of files and sub directories. I want to find the files and directories which have not been modified since last 14 days so that I can delete those files and directories.
I have wrote this script but it is giving the directory name only
#!/bin/sh
M2_REPO=/var/lib/jenkins/.m2/repository
echo $M2_REPO
OLDFILES=/var/lib/jenkins/.m2/repository/deleted_artifacts.txt
AGE=14
find "${M2_REPO}" -name '*' -atime +${AGE} -exec dirname {} \; >> ${OLDFILES}
find /path/to/files* -mtime +5 -exec rm {} \;
Note that there are spaces between rm, {}, and \;
Explanation
The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.
This should work on Ubuntu, Suse, Redhat, or pretty much any version of linux.
You can give the find -delete flag to remove the files with it. Just be careful to put it in the end of the command so that the time filter is applied first.
You can first just list the files that the command finds:
find "${M2_REPO}" -depth -mtime +${AGE} -print
The -d flag makes the find do the search depth-first, which is implied by the -deletecommand.
If you like the results, change the print to delete:
find "${M2_REPO}" -mtime +${AGE} -delete
I know this is a very old question but FWIW I solved the problem in two steps, first find and delete files older than N days, then find and delete empty directories. I tried doing both in one step but the delete operation updates the modification time on the file's parent directory, and then the (empty) directory does not match the -mtime criteria any more! Here's the solution with shell variables:
age=14
dir="/tmp/dirty"
find "$dir" -mtime "+$age" -delete && find "$dir" -type d -empty -delete

Resources