If I need to have scripts like below:
find -name 'lib*.so' | xargs cp <files> ~/libs/.
Where < files > is the file which I found from the previous find command. Basically i want to pipe the data not just at the end but some where in the middle. (Some thing like $1 ???)
I understand I can have small sh file, where I can have this in a variable and use For loop & use that variable.... But what I want is simple one as I explained above. Where simple tasks can be accomplished easily.
Note: The script above is only a indication of type of Problem and the actual problem.
Let me know if this kind is possible.
If you just want to do copy
find -name 'lib*.so ' -print0 | xargs -r0 cp --target ~/libs/
You can do this using find only, without having to spawn cp(1) for each file by doing:
find -name 'lib*.so' -exec cp -t ~/libs {} +
Note that this only works with GNU cp and a POSIX 2008 compliant find, like GNU find.
I hope I understand what you're trying to do here...
You can do this using find only.
find -name 'lib*.so' -exec cp {} ~/libs/ \;
Related
I’m using Amazon Linux with the bash shell. I want to find and remove some PDF files in a single line, so I tried
find /home/jboss/.jenkins/jobs/myco/workspace/ebook/ -name '*.pdf' | xargs rm
This works fine if there are PDF files. But if there are none, I get the error
rm: missing operand
Is there any way to write the above statement in a single line so that it will not fail, even if there are no files to remove?
This can easily be achieved using the -r flag to xargs.
I also recommend using "special character tolerant" version:
find /home/jboss/.jenkins/jobs/myco/workspace/ebook/ -name '*.pdf' -print0 | xargs -0 -r rm
Have you tried doing it all within the find command?
find /home/jboss/.jenkins/jobs/myco/workspace/ebook/ -name '*.pdf' -exec rm -f {} \;
I've always used the construct above though I believe you can also use the switch -delete which may be a bit more efficient. If you do use it remember to put -delete at the end as find is evaluated left to right as an expression.
You don't even need find, you can simply use rm, it supports basic pattern matching. Just do the following:
rm -f path/*.pdf
I have a directory that contains a list of files having the following format:
240-timestamp1.ts
240-timestamp2.ts
...
360-timestamp1.ts
360-timestamp2.ts
Now, I want to implement a bash command which matches the files that start with '240' and renames them so that instead of '240-timestampX.ts' the files look like '240-human-readable-timestampX.ts'.
I have tried the following:
find . -maxdepth 1 -mmin +5 -type f -name "240*"
-exec mv $0 {$0/240-***and here I want to insert
either stat -c %y filename or date -d #timestampX***} '{}' \;
I stuck here because I don't know if I can embed a bash command inside the mv command. I know the task may look a bit confusing and over-complicated, but I would like to know if it is possible to do so. Of course I can create a bash script that would go through all the files in the directory and while loop them with changing their respective names, but somehow I think that a single command would be more efficient (even if less readable).
The OS is Linux Ubuntu 12.04.5
The shell is bash
Thank you both Kenavoz and Kurt Stutsman for the proposed solutions. Both your answers perform the task; however, I marked Kenavoz's answer as the accepted one because of the degree of similarity between my question and Kenavoz's answer. Even if it is indeed possible to do it in a cleaner way with omitting the find command, it is necessary in my case to use the respective command because I need to find files older than X units of time. So thank you both once again!
In case you want to keep your mmin option, your can use find and process found files with a bash command using xargs :
find . -maxdepth 1 -mmin +5 -type f -name "240*.ts" | xargs -L 1 bash -c 'mv "${1}" "240-$(stat -c %y ${1}).ts"' \;
In bash if all your files are in a single directory, you don't need to use find at all. You can do a for loop:
for file in 240-*; do
hr_timestamp=$(date -d $(echo "$file" | sed 's/.*-\([0-9]*\)\.ts/\1/'))
mv "$file" "240-$hr_timestamp.ts"
done
I'm trying to return a particular line from files found from this search:
find . -name "database.php"
Each of these files contains a database name, next to a php variable like $dname=
I've been trying to use -exec to execute a grep search on this file with no success
-exec "grep {\}\ dbname"
Can anyone provide me with some understanding of how to accomplish this task?
I'm running CentOS 5, and there are about 100 database.php files stored in subdirectories on my server.
Thanks
Jason
You have the arguments to grep inverted, and you need them as separate arguments:
find . -name "database.php" -exec grep '$dbname' /dev/null {} +
The presence of /dev/null ensures that the file name(s) that match are listed as well as the lines that match.
I think this will do it. Not sure if you need to make any adjustments for CentOS.
find . -name "database.php" -exec grep dbname {} \;
I worked it out using xargs
find . -name "database.php" -print | xargs grep \'database\'\=\> > list_of_databases
Feel free to post a better way if you find one (or what some rep for a good answer)
I tend to habitually avoid find because I've never learned how to use it properly, so the way I'd accomplish your task would be:
grep dbname **/database.php
Edit: This command won't be viable in all cases because it can potentially generate a very long argument list, whereas find executes its command on found files one by one like xargs. And, as I noted in my comment, it's possibly not very portable. But it's damn short ;)
Why does the following command aiming to remove recursively all .svn folders
find . -name ".svn" | rm -rfv
doesn't work ?
I know the find command provides the -exec option to solve this issue but I just want to understand what is happening there.
In your example, the results from find are passed to rm's STDIN. rm doesn't expect its arguments in STDIN, though.
Here is an example how input redirecting works.
rm does not read file names from standard input, so any data piped to it is ignored.
The only thing it uses standard input for is checking whether it's a terminal, so it can determine whether to prompt.
It doesn't work because rm does not accept a list of file names on its standard input stream.
Just for reference, the safest way to handle this in the case of directories that might contain spaces is:
find . -name .svn -exec rm -frv {} \;
Or, if you are shooting for speed:
find . -name .svn -print0 | xargs -0 rm -frv
find do works with | ( for example find ~ -name .svn | grep "a") but the problem is with rm
This question is similar to this other answered question. Hope this helps.
How do I include a pipe | in my linux find -exec command?
I am trying to use the -exec option with the find command to find specific files in my massive panoramas directory and move them to a specified location. The command I am using below passes an error argument not found for -exec. Can somebody point out my error in parsing the command? Or would I have to create a pipe of some sorts instead?
$ find -name ~/path_to_directory_of_photos/specific_photo_names* -exec mv {} ~/path_to_new_directory/
You need to terminate your exec'ed command with an escaped semicolon (\;).
You should quote the name pattern otherwise the shell will expand any wildcards in it, before running find. You also need to have a semicolon (backslashed to avoid the shell interpreting it as a command separator) to indicate the end of the mv command.
The correct command would be:
find ~/path_to_directory_of_photos -name "specific_photo_names*" -exec mv {} ~/path_to_new_directory \;
I know this post is old, but here's my answer in case it helps anyone else. See the background from this post. If you end the command with + instead of \; you can run it much more efficiently. \; will cause "mv" to be executed once per file, while + will cause "mv" to be executed with the maximum number of arguments. E.g.
mv source1 destination/
mv source2 destination/
mv source3 destination/
vs.
mv source1 source2 source3 destination/
The latter is much more efficient. To use +, you should also use --target-directory. E.g.
find ~/path_to_directory_of_photos -name "specific_photo_names*" -exec mv --target-directory="~/path_to_new_directory" {} +