Why find's -exec option is including 'non-matched' items? - linux

I'm trying to use find to find and exclude/filter few directories from being copied to another backup directory.
My attempts to do so using find's '-exec' option end up copying every processed file instead of only the matches, so I'm quite confused about what the expected behavior should be and would appreciate help gaining better understanding.
Starting point:
me#computer>ls
AddMonitorsOnEntry MantisCoreFormatting MantisGraph PastePicture XmlImportExport
Make sure find excludes the unwanted 'files' as expected
me#computer>find . -maxdepth 1 -not -regex '.*MantisCoreFormatting\|.*MantisGraph\|.*XmlImportExport'
.
./AddMonitorsOnEntry
./PastePicture
Now to copy those 2 directories to a backup dir:
me#computer>find . -maxdepth 1 -not -regex '.*MantisCoreFormatting\|.*MantisGraph\|.*XmlImportExport' -exec cp -dr '{}' ~/backup \;
Now to see if it worked...
me#computer>cd ~/backup
me#computer>ls
AddMonitorsOnEntry backup MantisCoreFormatting MantisGraph PastePicture XmlImportExport
WTH??
I thought '-exec' only operated on the matches, according to this snippet from the man page: " ...The specified command is run once for each matched file..."
I know there are other ways to accomplish this task, but '-exec' seems to work well enough for the poster here https://unix.stackexchange.com/questions/50612/how-to-combine-2-name-conditions-in-find/50633. I'm looking for help understanding how to make use of "-exec" versus using xargs or something else. Thanks.

Now to copy those 2 directories to a backup dir
You don't have 2 matches. Your command shows 3:
.
./AddMonitorsOnEntry
./PastePicture
. is the current directory, so your cp command copies everything.
Instead of find . you can use find * to skip the current directory ., but still process all the (non-hidden) files/dirs within it.

Silly of me..
My initial find expression includes the current directory as a result, so any files in the current dir will be operated on by "-exec".
To fix I added the current dir among the ones excluded.
me#computer>find . -maxdepth 1 -not -regex '.*MantisCoreFormatting\|.*MantisGraph\|.*XmlImportExport\|\.'
./AddMonitorsOnEntry
./PastePicture

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 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

Using Perl how can I clean up left over directories with no files?

There is a specific directory which is used as a temp/scratch directory by some program.
E.g. /a/b/c/work
Under work multiple hierarchical directories may exist e.g.
/a/b/c/work/
\d1/
\d1.1
\d2
\d2.2
What I want is to clean up this work directory as there are left over files that take space.
Essentially I need to delete all subdirectories under work that the leaf directory is empty.
So if d1.1 is empty but d2.2 has files then delete everything under d1 (including d1) but not d2.
What is the cleanest/standard way to do this in perl?
I thought to use a solution with backticks e.g. rm -rf etc but I thought there could be some better way than coding sequences of ls folowed by rm
Note: Just to be clear. I want a solution in Perl as this is not a one time thing and I dont want to do this manually each time
If you use find command this way you can achieve it.
find /path/to/dir -empty -type d -delete
Where,
-empty Only find empty files and make sure it is a regular file or a directory.
-type d Only match directories.
-delete Delete files.
Always put -delete option at the end of find command as find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.
To automate this in shell script follow below code:
path=`pwd`
find $path -empty -type d -delete
or you can give certain input as arguments of shell script like myShell.sh /path/to/mydir in that case the following code will be do the work,
$path=$1
find $path -empty -type d -delete
As for if you really want to go for perl you can find your answer as follows
use strict;
use warnings;
use File::Util;
my $path = '...';
my $fu = File::Util->new();
my #all_dirs = $fu->list_dir($path, '--recurse', '--dirs-only');
my #empty_dirs = grep { not $fu->list_dir($_) } #all_dirs;
also a short method
perl -MFile::Find -e"finddepth(sub{rmdir},'.')"
which is explained very good here.

Copy specific files recursively

This problem has been discussed extensively but I couldn't find a solution that would help me.
I'm trying to selectively copy files from a directory tree into a specific folder. After reading some Q&A, here's what I tried:
cp `find . -name "*.pdf" -type f` ../collect/
I am in the right parent directory and there indeed is a collect directory a level above. Now I'm getting the error: cp: invalid option -- 'o'
What is going wrong?
To handle difficult file names:
find . -name "*.pdf" -type f -exec cp {} ../collect/ \;
By default, find will print the file names that it finds. If one uses the -exec option, it will instead pass the file names on to a command of your choosing, in this case a cp command which is written as:
cp {} ../collect/ \;
The {} tells find where to insert the file name. The end of the command given to -exec is marked by a semicolon. Normally, the shell would eat the semicolon. So, we escape the semicolon with a backslash so that it is passed as an argument to the find command.
Because find gives the file name to cp directly without interference from the shell, this approach works for even the most difficult file names.
More efficiency
The above runs cp on every file found. If there are many files, that would be a lot of processes started. If one has GNU tools, that can be avoided as follows:
find . -name '*.pdf' -type f -exec cp -t ../collect {} +
In this variant of the command, find will supply many file names for each single invocation of cp, potentially greatly reducing the number of processes that need to be started.

Remove files for a lot of directories - Linux

How can I remove all .txt files present in several directories
Dir1 >
Dir11/123.txt
Dir12/456.txt
Dir13/test.txt
Dir14/manifest.txt
In my example I want to run the remove command from Dir1.
I know the linux command rm, but i don't know how can I make this works to my case.
PS.: I'm using ubuntu.
To do what you want recursively, find is the most used tool in this case. Combined with the -delete switch, you can do it with a single command (no need to use -exec (and forks) in find like other answers in this thread) :
find Dir1 -type f -name "*.txt" -delete
if you use bash4, you can do too :
( shopt -s globstar; rm Dir1/**/*.txt )
We're not going to enter sub directories so no need to use find; everything is at the same level. I think this is what you're looking for: rm */*.txt
Before you run this you can try echo */*.txt to see if the correct files are going to be removed.
Using find would be useful if you want to search subfolders of subfolders, etc.
There is no Dir1 in the current folder so don't do find Dir1 .... If you run the find from the prompt above this will work:
find . -type f -name "*.txt" -delete

Resources