Delete file from a folder which is X days older - unix

I need to delete file which is 14 days older.
CMD USED:
find ${FILES_DIR}/*.zip -mindepth 1 -mtime +14 -delete
This works fine. But what if there are no zip files present in the folder? It's throwing an error. How to handle the case?

The first parameter is where to start the search.
A filename-pattern is given with -name '<pattern>
You should use
find ${FILES_DIR} -name '*.zip' -mtime +14 -delete
In your form the find gets a list of starting points, but has nothing to search.
-mindepth 1 dosn't seem to have an effect. May be you wanted to use -maxdepth 1 to restrict the search to the mentioned directory and nothing else. (Place it before -nameto avoid a warning
You may consider using -ls -delete - at least you can see what has been deleted.

Related

How to have "make clean" ignore folders when deleting

I was given a Makefile for an assignment that gives me a make clean
command. With the way the repository is set up, it deletes everything in the /bin and /out folders, except for a file called .gitignore. This is what the command looks like:
clean:
find out/ ! -name .gitignore -type f -delete && \
find bin/ ! -name .gitignore -type f -delete
Now that I'm doing my project, I need to store things in a folder called /bin/fonts and /bin/word_lists. I'm trying to modify the command so that it ignores these two files. The only problem is, I don't know what language these commands are written in, so I don't even know where to start looking at the syntax. Could somebody point me in the right direction? I tried something like this:
clean:
find out/ ! -name .gitignore -type f -delete && \
find bin/ ! -name .gitignore ! -name fonts/FreeSans.ttf -type f -delete
But it still deletes everything in fonts, and even if it did work the way I wanted, that doesn't really solve the problem of saving every single font in the folder.
I also tried this:
clean:
find out/ ! -name .gitignore -type f -delete && \
find ./bin -mindepth 1 ! -name .gitignore ! -regex '^./fonts/\(/.*\)?' ! -regex '^./word_lists/\(/.*\)?' -delete
following this post, but it instead deleted everything INCLUDING the folders bin/fonts as well as bin/word_lists.
Any help would be greatly appreciated!
-name does not examine the full file path, it only matches against the file name (so -name FreeSans.ttf would match, but match this file name in any directory).
The predicate you are looking for is called -path but then you need to specify a pattern for the entire path.
clean:
find out/ bin/ ! -name .gitignore ! -path 'bin/fonts/*
! -path 'bin/word_lists/*' -type f -delete
(Notice also how I condensed the find to traverse two directories at the same time. I assume you mean bin not /bin; perhaps see also Difference between ./ and ~/)

How to list and delete directories that are not symbolic links?

I see plenty of answers on how to list all symlinks and how to remove all symlinks within a specific directory. However what about vice versa?
How would one go about listing/removing all directories within a directory that are not symlinks?
I know that rm -R removes all directories recursively but i want to know how to make it not delete symlinks in the process.
I also know that ls lists all directories files and symlinks however i would like to know how i would go about listing only directories that are not symbolic links.
Found a way finally.
First, run:
find . -depth -type d
to make sure the output looks sane, then:
sudo find . -depth -type d -exec rm -rf '{}' \;
Sure this does get a bit messy on the console to look through, but ... it works! If anyone can find a better and cleaner way to do this please post it.

Find and copy to multiple sub directories

Hi I am trying to do a find and copy to multiple preferences folders within user files just a plist but coming up on an error. I am hopping someone can help point it out for me or help me understand what I'm doing wrong.
find . -type d -name 'Preferences' -maxdepth 3 -exec cp -r {} /Users/ladmin/Desktop/source.plist *Library/Preferences \;
Running just this
find . -type d -name 'Preferences' -maxdepth 3
prints out what I am trying to copy into username/Library/Preferences
Then I want to copy the plist to the preferences folder of every user.
I hope this isn't too complicated for people to read.
Thanks Kris
Not entirely sure why that should cause an error, though it does have several issues.
find is recursive and cp -r is recursive, and they are both traversing the same tree. You can add the -prune test to find to stop it from descending found directories
Not sure if this effects anything here, but find generally likes options (i.e. -maxdepth) to come first.
*Library/Preferences if this expands to multiple paths all but one of them will get copied into the last one.
But, I think the main issue is that you are trying to copy a bunch of directories named Preferences into a single directory, so only one of them will actually get copied, and the rest will get overridden out.
find -maxdepth 3 -type d -name 'Preferences' -prune -exec echo cp -ivr {} /Users/ladmin/Desktop/source.plist username/Library/Preferences +
This fixes all of the first issues, but it's not clear from the question what should happen when a directory with that name already exists. The -iv will prompt you if you want to override when conflicts occur and add some verbosity. The + speeds up execution of find for commands that can take multiple file/dir names (like cp).

Find directory name with wildcard or similar to "like"

I am using the following command to find a directory name.
find / -type d -name "ora10"
My problem is, I am not sure what the exact directory name is, so I would like to find directories similar to "ora10g", "ora10client", etc.
How can I do this with find?
find supports wildcard matches, just add a *:
find / -type d -name "ora10*"

Problem redirecting output of find to a file

I am trying to put the result of a find command to a text file on a unix bash shell
Using:
find ~/* -name "*.txt" -print > list_of_txt_files.list
However the list_of_txt_files.list stays empty and I have to kill the find to have it return the command prompt. I do have many txt files in my home directory
Alternatively How do I save the result of a find command to a text file from the commandline. I thought that this should work
The first thing I would do is use single quotes (some shells will expand the wildcards, though I don't think bash does, at least by default), and the first argument to find is a directory, not a list of files:
find ~ -name '*.txt' -print > list_of_txt_files.list
Beyond that, it may just be taking a long time, though I can't imagine anyone having that many text files (you say you have a lot but it would have to be pretty massive to slow down find). Try it first without the redirection and see what it outputs:
find ~ -name '*.txt' -print
You can redirect output to a file and console together by using tee.
find ~ -name '*.txt' -print | tee result.log
This will redirect output to console and to a file and hence you don't have to guess whether if command is actually executing.
Here is what worked for me
find . -name '*.zip' -exec echo {} \\; > zips.out

Resources