Is there a way to use command line to search all subfolders with a folder (unlimited depth) to find and automatically open every file that's named footer.php ?
I was able run
find /desktop/themes -name footer.php
and it found all of the files but I don't know how to make it open all of them automatically. Does anyone know how to do this?
(I'm running this in cygwin in case that helps.)
Try using gvim -p. That will open all files in different tabs:
find /desktop/themes -name footer.php -print0 | xargs -0 gvim -p
Note the use of -print0 with xargs -0 for safe parsing of find's output.
Related
trying to make my workflow more efficient. I often have two files of the same name in two directories held within one "master" directory like this:
root
folder_1
my_website.html
folder_2
my_website.html
From within terminal I can enter:
find -L . -name "my_website.html"
and that gives me the list of files and their locations. But I'd like to open them directly from here, rather than navigate down the directories. Is there a way to chain an open command on to open both of the files it finds.
Thanks
After searching and toying I came across this which works:
find -L . -name 'my_website.html' -print0 | xargs -0 -n 1 open
Say I want to edit every .html file in a directory one after the other using vim, I can do this with:
find . -name "*.html" -exec vim {} \;
But what if I only want to edit every html file containing a certain string one after the other? I use grep to find files containing those strings, but how can I pipe each one to vim similar to the find command. Perphaps I should use something other than grep, or somehow pipe the find command to grep and then exec vim. Does anyone know how to edit files containing a certain string one after the other, in the same fashion the find command example I give above would?
grep -l 'certain string' *.html | xargs vim
This assumes you don't have eccentric file names with spaces etc in them. If you have to deal with eccentric file names, check whether your grep has a -z option to terminate output lines with null bytes (and xargs has a -0 option to read such inputs), and if so, then:
grep -zl 'certain string' *.html | xargs -0 vim
If you need to search subdirectories, maybe your version of Bash has support for **:
grep -zl 'certain string' **/*.html | xargs -0 vim
Note: these commands run vim on batches of files. If you must run it once per file, then you need to use -n 1 as extra options to xargs before you mention vim. If you have GNU xargs, you can use -r to prevent it running vim when there are no file names in its input (none of the files scanned by grep contain the 'certain string').
The variations can be continued as you invent new ways to confuse things.
With find :
find . -type f -name '*.html' -exec bash -c 'grep -q "yourtext" "${1}" && vim "${1}"' _ {} \;
On each files, calls bash commands that grep the file with yourtext and open it with vim if text is matching.
Solution with a for cycle:
for i in $(find . -type f -name '*.html'); do vim $i; done
This should open all files in a separate vim session once you close the previous.
I wish to create a program that zips whatever file is created in the directory the find parameters specify, and run it as a background process. I heavily comment it to give a better idea of what I'm trying to achieve. I'm running this from my MacBook Pro terminal, OS X version 10.9
#!/bin/sh
#find file in directory listed below
#type f to omit directories or special files
#mtime/ctime is modified/created -0 days or less
#name is with the name given in double quotes
#asterik meaning any file name with any file extension
#use xargs to convert find sequence to a command for the line after pipe
find /Users/name/thisdirectory type f -ctime -0 -name "'*'.'*'" | xargs zip -
Maybe you're looking for this:
find /path/to/dir -type f -ctime -0 -name "*.*" | zip -# file.zip
If you read zip -h, it explains that -# is to read the filenames from standard input.
You don't need xargs here, the function to work with a list of files received from standard input is built into zip itself, similar to most compression tools like tar.
Btw, I think you want to change -ctime -0, because I don't think it can match anything this way...
This question already has answers here:
Command line: piping find results to rm
(5 answers)
Closed last month.
Recently frigged up my external hard drive with my photos on it (most are on DVD anyway, but..) by some partition friggery.
Fortunately I was able to put things back together with PhotoRec another Unix partition utility and PDisk.
PhotoRec returned over one thousand folders chalk full of anything from .txt files to important .NEF's.
So I tried to make the sorting easier by using unix since the OSX Finder would simply crumble under such requests as to select and delete a billion .txt files.
But I encounter some BS when I tried to find and delete txt files, or find and move all jpegs recursively into a new folder called jpegs. I am a unix noob so I need some assistance please.
Here is what I did in bash. (I am in the directory that ls would list all the folders and files I need to act upon).
find . -name *.txt | rm
or
sudo find . -name *.txt | rm -f
So it's giving me some BS that I need to unlink the files. Whatever.
I need to find all .txt files recursively and delete them preferably verbose.
You can't pipe filenames to rm. You need to use xargs instead. Also, remember to quote the file pattern ".txt" or the shell will expand it.
find . -name "*.txt" | xargs rm
find . -name "*.txt" -exec rm {} \;
$ find . -name "*.txt" -type f -delete
First, any support and help on this is largely appreciated.
I'm trying to write a simple Bash script (completely new to this) to replace a file in a given directory.
Basically, I need to write a script to replace the safari preference file, here's what I have..and what's not working for that matter:
#!/bin/bash
find /Files/ -iname "com.apple.Safari.plist" - print0 | xargs -I{} -0 -1 cp file /Users/{}/Library/Preferences
It errors out with the following:
find: -: unknown option
xargs: illegal option -- 1
Any thoughts, ideas, are greatly appreciated.
Thanks,
I couldn't understand what exactly you want to accomplish with this. As I understand, you would have this "com.apple.Safari.plist" in /Files/, is that correct?
And then you want to get this file into some place that, I assume, overwrites Safari's current plist file. Assuming you take ghostdog74's correct advice and remove the space between - print0, thus turning it into -print0, and then remove the -1 from xargs, as it doesn't exist, this is what would happen:
find would find your file in /Files/, and xargs would run this:
cp file /Users/com.apple.Safari.plist/Library/Preferences; It would then die, since it would not find a file called "file" or a directory named "/Users/com.apple.Safari.plist/".
That's most likely not what you want. :)
If you just want to copy the file to somewhere, why don't you just do cp /Files/com.apple.Safari.plist ~/Library/Preferences/ ?
Do you really need find and xargs in this case? Could you clarify?
No space between - print0. and since -1 is not an option, remove it and see.
find /Files/ -iname "com.apple.Safari.plist" -print0 | xargs -I{} -0 cp file /Users/{}/Library/Preferences