How can I delete all empty lines in all .java files in a project?
By using sed -i '/^\s*$/d' *.java I can delete it in the current dir but how to do the same in all subdirs?
Use GNU's find utility.
find -type f -name '*.java' -exec sed -i '/^\s*$/d' {} +
I have written some code that checks all the folders in the file and matches a regex statement to them however, there is a subfolder in the directory that I want it to enter and also perform the regex on but whenever I run the code it gives me this error
sed: couldn't edit TestFolder: not a regular file
I've looked all over S.O and the Internet and can't find anything helpful
I've tried to use code I've found to fix my problem but it isn't helping so I apologise for the potentially hideous code, it's pulled from various sources
`pwd = "$PWD"
find $pwd = -print | xargs -0 sed -i "/10.0.0.10/d" !(test.sh)
My directory structure follows
Test
-one.txt
-two.txt
TestFolder
-three.txt
With GNU find, GNU xargs, and GNU sed:
find . -type f -not -name 'test.sh' -print0 | xargs -0 sed -i '/\<10\.0\.0\.10\>/d'
Hi I want to delete a file from any of the subdirectories except one of the subdirectory.
For ex
folder a->a.txt
folder b->subdir 1 -> msgdir-> a.txt
folder c->
Now i want to delete a.txt only in folder a but not the file in msgdir .msgdir can be in any level of subdirectories as it would be changing.
Please help me to resolve this.
This will ignore specifically the msgdir at any level and remove a.txt except in msgdir.
find . ! -path '*/msgdir/*' -name a.txt -type f -delete
Tested with GNU find 4.4.2 and BSD find (Mac Yosemite).
The following approach is overkill if you have GNU find (or a newer BSD one), with the -path option. Otherwise, read on...
You haven't specified which shell you're using but if you have bash, you could go with something like this:
find -name a.txt -exec bash -c "[[ '{}' != */msgdir/* ]]" \; -print
This filters out paths containing /msgdir/, as the test will only pass if the file path doesn't contain the string. If you're happy with the results, you can change -print to -delete.
Without bash, you could use grep to determine the match:
find -name a.txt -exec sh -c "printf '%s' '{}' | grep -qv '/msgdir/'" \; -print
Im wondering if someone can help me out.
Im currently using the following to find all PHP files in a certain directory
find /home/mywebsite -type f -name "*.php"
How would i extend that to search through those PHP files and get all files with the string base64_decode?
Any help would be great.
Cheers,
find /home/mywebsite -type f -name '*.php' -exec grep -l base64_decode {} +
The -exec option to find executes a command on the files found. {} is replaced by the filename, and the + means that it should keep repeating this for all the filenames. grep looks for a string in the file, and the -l option tells it to print just the filename when there's a match, not all the matching lines.
If you're getting an error from find, you may have an old version that doesn't support the + feature of -exec. Use this command instead:
find /home/mywebsite -type f -name '*.php' | xargs grep -l base64_decode
xargs reads its standard input and turns them into arguments for the command line in its arguments.
I'm trying to use find in Windows 7 with GNU sed to recursively replace a line of text in multiple files, across multiple directories. I looked at this question but the PowerShell solution seems to work with only one file, and I want to work with all files with a certain extension, recursively from the current directory. I tried this command:
find "*.mako" -exec sed -i "s:<%inherit file="layout.mako"/>:<%inherit file="../layout.mako"/>:"
But that gives me a bunch of crap and doesn't change any files:
---------- EDIT.MAKO
File not found - -EXEC
File not found - SED
File not found - -I
File not found - LAYOUT.MAKO/>:<%INHERIT FILE=../LAYOUT.MAKO/>:
How can I do this? It seems like I should have all the tools installed that I need, without having to install Cygwin or UnixUtils or anything else.
Edit: okay, working with GNU find, I still can't get anywhere, because I can't get the find part to work:
> gfind -iname "*.mako" .
C:\Program Files (x86)\GnuWin32\bin\gfind.exe: paths must precede expression
> gfind . -iname "*.mako"
C:\Program Files (x86)\GnuWin32\bin\gfind.exe: paths must precede expression
> gfind -iname "*.mako" .
C:\Program Files (x86)\GnuWin32\bin\gfind.exe: paths must precede expression
I was originally not using GNU find in Windows 7 because of this question.
Edit:
I tried the following, but sed doesn't see any input files this way:
> ls -r | grep mako | sed -i 's/file="layout.mako"/file="..\/layout.mako"/'
sed.exe: no input files
FIND from windows is being found instead of find from gnu.
So, rename your find.exe (from gnu) to gfind.exe (for example) and then call gfind instead of find when you wish to run it.
[edit]
gfind . -name "*.mako" (not gfind -iname "*.make" .)
[/edit]
You're executing the regular windows 'find' command, which has completely different command line arguments than gnu find. MS find has no capability of executing a program for each match, it simply searches.
Addition to Marc B/KevinDTimm answers: your find syntax is wrong.
It is not:
find "*.mako"
but:
find -name "*.mako"
Also, if there are directories that matches "*.mako", they would be sent to sed. To avoid that:
find -name "*.mako" -type f
Finally, I think that you are missing a '\;' at the end or your find command.
In Powershell, note the escape sequence using backticks
find . -type f -exec grep hello `{`} `;
Its much easier to use xargs
find . | xargs grep hello
I tried the following, but sed doesn't see any input files this way:
ls -r | grep mako | sed -i 's/file="layout.mako"/file="../layout.mako"/'
sed.exe: no input files
With this you are now running into PowerShell's "ls" alias. Either call "ls.exe" or go all PowerShell like this:
ls -r | select-string mako -list | select -exp path | sed -i 's/file="layout.mako"/file="..\/layout.mako"/'
Edit:
Workaround if stdin handling doesn't seem to be working.
ls -r | select-string mako -list | select -exp path | % {sed -i 's/file="layout.mako"/file="..\/layout.mako"/' $_}
Per your
Edit:
I tried the following, but sed doesn't see any input files this way:
ls -r | grep mako | sed -i
's/file="layout.mako"/file="../layout.mako"/' sed.exe: no input files
you need to use xargs to assemble the list of files passed to sed, i.e.
ls -r | grep mako | xargs sed -i
's\:file="layout.mako":file="../layout.mako":'
Note that for most versions of sed, you can use an alternate character to identify the substitute match/replace strings (usually '/'). Some seds require escaping that alternate char, which I have done in this example.
I hope this helps.