Unix one-liner to extract substring from all files in a directory and subdirectories - unix

I'm looking for a UNIX one-liner that will output to a file all occurrences of NSLocalizedString (from that word to the end of the line) in all files in the current directory and all subdirectories. I've googled, but haven't found a solution.

find . -type f -exec fgrep NSLocalizedString {} \+ | \
sed -e 's/^.*\(NSLocalizedString.*\)$/\1/' > ../your_output_file

find <directory> -type f -print | xargs grep NSLocalizedString | tee <outputfile> should do what you're looking for, if I understand the question right...

Related

unix: count number of jpeg files recursively except for one subfolder in every folder?

I think the code to count all the jpeg files recursively in a folder is,
find . -type f -name "*.jpeg" | wc -l
but I now realize I need to exclude some subfolders...
for instance, my folder consists of 5 subfolders and in each subfolder there is a subsubfolder named "meh" consisting of jpeg files I wish not to include in my count... Could anyone let me know how to do that?
Thanks so much for your guidance.
You can do this with find's option -prune or -regex.
find . -name meh -prune -o -name '*.jpeg' -print | wc -l
find . -not -regex '.*/meh/.*' -a -name '*.jpeg' -print | wc -l
Weird that #Prune didn't answer that.
Since find includes the relative path of each file, you could do this:
find . -type f -name "*.jpeg" | grep -vc /meh/
Use any grep variant to filter the output of find.
While you're doing that, use the count option from grep, -c.
-v is reverse logic: list only those that do not match the given pattern.
find . -type f -name "*.jpeg" | egrep -c -v "/meh/"

Find replace text in multiple files in subdirectories. Exclude some subdirectories

I want to find replace a patter1 into pattern2 only in certain files of my subdirectories. But exclude some subdirectories with replacement. What is wrong with this command?
find ./ -type f --exclude-dir='workspace' --exclude-dir='builds' \
-exec sed -i '' 's/foo/bar/g' {} \;
I don't see the option --exclude-dir in man find (I do in man grep, but you can't just borrow other command's options).
Try
find . -type f -not -path './workspace*' ...

Need help in unix find with xargs command

Am trying to delete files with below find command, few files are not deleting and all those file names are like: filname.123.log.
I can't rename or can't do any thing on file name just need to delete
command
$ find /BASE/CODE/LOGS_BACK -type f -mtime +60 | xargs rm -f
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
I googled and try with the below command but it is giving different error.
$ find /BASE/CODE/LOGS_BACK -type f -mtime +60 | xargs -0 rm -f
xargs: argument line too long
Can you please help regarding this?
using -0 option in xargs means using -print0 option in find :
find /BASE/CODE/LOGS_BACK -type f -mtime +60 -print0 | xargs -0 rm -f
Then all results of find will be ended with '\0' NUL char and xargs will regenerate list correctly by parsing with same char.
find can directly execute a command for each result:
$ find /BASE/CODE/LOGS_BACK -type f -mtime +60 -exec rm -f {} \;
{} is replaced with each matching filename. \; end the command.
This is the fastest way to achieve what you want:
find /BASE/CODE/LOGS_BACK -type f -mtime +60 -exec rm -f {} +
Note the ending + which does the same job as xargs.

How to move or copy files listed by 'find' command in unix?

I have a list of certain files that I see using the command below, but how can I copy those files listed into another folder, say ~/test?
find . -mtime 1 -exec du -hc {} +
Adding to Eric Jablow's answer, here is a possible solution (it worked for me - linux mint 14 /nadia)
find /path/to/search/ -type f -name "glob-to-find-files" | xargs cp -t /target/path/
You can refer to "How can I use xargs to copy files that have spaces and quotes in their names?" as well.
Actually, you can process the find command output in a copy command in two ways:
If the find command's output doesn't contain any space, i.e if the filename doesn't contain a space in it, then you can use:
Syntax:
find <Path> <Conditions> | xargs cp -t <copy file path>
Example:
find -mtime -1 -type f | xargs cp -t inner/
But our production data files might contain spaces, so most of time this command is effective:
Syntax:
find <path> <condition> -exec cp '{}' <copy path> \;
Example
find -mtime -1 -type f -exec cp '{}' inner/ \;
In the second example, the last part, the semi-colon is also considered as part of the find command, and should be escaped before pressing Enter. Otherwise you will get an error something like:
find: missing argument to `-exec'
find /PATH/TO/YOUR/FILES -name NAME.EXT -exec cp -rfp {} /DST_DIR \;
If you're using GNU find,
find . -mtime 1 -exec cp -t ~/test/ {} +
This works as well as piping the output into xargs while avoiding the pitfalls of doing so (it handles embedded spaces and newlines without having to use find ... -print0 | xargs -0 ...).
This is the best way for me:
cat filename.tsv |
while read FILENAME
do
sudo find /PATH_FROM/ -name "$FILENAME" -maxdepth 4 -exec cp '{}' /PATH_TO/ \; ;
done

Unix find and move command

I need a Unix command to find all the text files older than 3 days and move them to some other location in single command.
In that case you want find and use its -exec and -mtime flags.
It should be something like:
find . -mtime -3 -type f -print0 | xargs -0 -I file mv file /new/dir/.
man find
man xargs

Resources