How about -execdir
?
This differs from -exec
in that it runs the command from the same directory that it finds the files (rather than the current working directory). Given that you're only going to find one translation.po
file in your LC_MESSAGES directories, we can lock things right down and not have to fuss around trying to snap the extension off the end of the path.
find . -name \*.po -execdir msgfmt translation.po -o translation.mo \;
Unfortunately the find
command's -name
predicate only accepts a single pattern. If you want to search for multiple files by name you'd need to chain them with the -o
(logical OR
) operator - something like:
find Documents/ \( -name "file2.txt" -o -name "file5.txt" -o -name "file9.txt" \) -print
This makes it tricky to construct the search programmatically from a list; the closest I can come to your attempted command is:
read the list into a shell array
mapfile -t files < list.txt
use the bash shell's printf
to construct the predicate list
printf -- '-name "%s" -o ' "${files[@]}"
use eval
to evaluate the resulting command string
There's a wrinkle here inasmuch as if we use printf
's format re-use feature to construct the list in this way, we're left with a 'dangling' -o
; we can work around this by terminating the list with a -false
test (since -o -false
is a Boolean no-op) so that our final predicate string becomes
"\( $(printf -- '-name "%s" -o ' "${files[@]}") -false \)"
Putting it all together - given
$ tree dir
dir
├── Folder_1
│ ├── file1.txt
│ ├── file2.txt
│ ├── file3.txt
│ └── file4.txt
├── Folder_2
│ ├── file5.txt
│ ├── file6.txt
│ └── file7.txt
└── Folder_3
├── file8.txt
└── file9.txt
3 directories, 9 files
and
$ cat list.txt
file2.txt
file5.txt
file9.txt
then
$ mapfile -t files < list.txt
$ eval find dir/ "\( $(printf -- '-name "%s" -o ' "${files[@]}") -false \)" -print
dir/Folder_1/file2.txt
dir/Folder_2/file5.txt
dir/Folder_3/file9.txt
To copy files instead of just listing them, you could then do
$ mkdir newdir
$ eval find dir/ "\( $(printf -- '-name "%s" -o ' "${files[@]}") -false \)" -exec cp -t newdir/ -- {} +
resulting in
$ tree newdir
newdir
├── file2.txt
├── file5.txt
└── file9.txt
0 directories, 3 files
Note: the eval
command is powerful and potentially open to abuse: use with care.
In practice, given that you appear to want to find only a small number of files, the KISS approach would be to accept the performance hit of multiple find
calls and just use a loop:
while read -r f; do
find dir/ -name "$f" -exec cp -v -- {} newdir/ \;
done < list.txt
or even using xargs
xargs -a list.txt -n1 -I@ find dir/ -name @ -exec cp -v -- {} newdir/ \;
Best Answer
The command
find . -name '*2015*' -mmin +1440 -ls
will probably do what you want. See below for details.Your first command had
-name 2015
. It did not work because it finds only files whose names are exactly2015
, with no other characters in them.Your second command,
find . -name *2015* -mtime +1 -exec ls -ltrh {} \;
, might have failed for a couple of reasons:1. Unquoted
*
characters are expanded by the shell, then passed on tofind
.If there are any files directly contained in the current directory (the one you're in when you ran that
find ...
command) whose names contain2015
(and don't start with a.
), then the shell expanded*2015*
into a list of those filenames, then passed that list as arguments tofind
. This is not what you want--instead, you want to pass*2015*
literally as an argument to find, so thatfind
, and not the shell, can find which files match it.To fix that problem, quote
*2015*
. There are three common ways to do it:'*2015*'
(i.e.,find . -name '*2015*' -mtime +1 -exec ls -ltrh {} \;
)"*2015*"
(i.e.,find . -name "*2015*" -mtime +1 -exec ls -ltrh {} \;
)\*2015\*
(i.e.,find . -name \*2015\* -mtime +1 -exec ls -ltrh {} \;
)I suggest writing it with single quotes as
'*2015*'
, because:\
can make it hard to see exactly what is being passed by the shell to thefind
command."
sometimes involves somewhat complicated rules.But in this case it doesn't really matter.
'
and"
both treat*
the same and the expression isn't complicated enough for\
quoting to make it hard to understand.2.
-mtime +1
only selects files modified two or more days ago.As
man find
says:Suppose a file was modified 47 hours ago. To figure out how many 24-hour periods that is,
find
rounds down: it is one 24-hour period ago. But-mtime +1
matches only files whose modification times are strictly more than one 24-hour period ago. Thus files from yesterday are not matched.See Why does find -mtime +1 only return files older than 2 days? for more information, as suggested by steeldriver.
To find files last modified anytime more than 24 hours ago, I suggest instead stipulating it as 1440 minutes ago with
-mmin +1440
:Some readers might be wondering why I did not quote
{}
. Some people quote{}
to remind humans that it is not an expression for brace expansion. Bourne-style shells (like bash) don't require{}
with nothing inside to be quoted. Maybe some non-Bourne-style shell does treat it specially; that might be why some users quote it. But there's also a misconception that one must sometimes quote{}
so-exec
handles filenames with spaces correctly. That;s false: with{}
or'{}'
,find
gets the same arguments, as the shell removes the quotes before passing'{}'
tofind
. To counter this misconception, I don't quote{}
, but it's a matter of style--if you prefer{}
to document how the shell isn't treating{
and}
specially, that's fine.I recommend you also either change your
ls
command, or (as muru has suggested) replace it withfind
's-ls
action.ls -ltrh
is probably not doing what you intend because it is run separately for each file found and thus the-t
and-r
flags, which specify sorting, are irrelevant.Though the output will be formatted a bit differently than with
ls -l
, using-ls
is simpler and easier.Or if you decide you really only need to list the file's names (including their paths, relative to
.
), you can simply specify no action, causing the default-print
action to be used: