I'm trying to use find command from FindUtils package from gnuwin32 with Windows 7 standard cmd.exe shell.
But when I type simplest possible command involving pattern matching, it doesn't work right.
If I type:
find . -name "*.java"
I end up with: paths must precede expression error. Here I found folowing explanation:
The -name test takes only one argument. In your command line, the
shell is expanding the s into more than one argument before the find
command is run. Therefore you get the error you see. You should
either escape any shell metacharacters in the -name argument or
enclose that argument in quotes.
But I am enclosing that argument in quotes!
I tried also with single quotes:
find . -name '*.java'
In this case no files are found (there are plenty of .java files in directory, but I checked '*' for sure, with the same effect).
I also tried:
find . -name \*.java
With the same effect as for double quotes. The same for:
find . -name "\*.java"
What am I doing wrong? In example I found, using double quotes with star worked well.
I think it may be related with this question.
Similar questions here and there. Seems to be related to GNUWin32's find.exe behaving different on Windows Vista and Windows 7 but not Windows XP.
Related
a bash script that used a Windows output file, generated me files and symlinks with ^M in the filename or link name.
when I do a ls /var/bla followed by a [tab] I see names like : file^M.pdf
when I do a ls /var/bla followed by a [enter] I see names like : file?.pdf
I would like to remove all of these files but a :
find /var/bla -name '*^M.*'
or
find /var/bla -name '*?.*'
do not give the attended results. the first case does not match anything and the second everything but not what I need.
Does anybody has an idea to remove these files?
Thanks by advance.
Use $'...' to create a string using C-style escape sequences.
find /va/bla -name $'*\r*'
So I recently made a simple bash script which I want to share with other people. So I tried making the script search for files in a folder in which other people will place them, which worked fine for a few files.
Though I have a command in my script that doesn't accept the variables, they get passed, but they don't get replaced in the command. So instead of using a variable I had to use the path to the files, which are only on my computer.
And since I want everyone to use that script I need a way to replace these paths. So I thought of this:
Use the same command that found the other file to find this one. Then replace the path to the file in the script with the path that was found by the command.
Though my problem is, that I haven't found something that is what I wanted. Here's my code:
#!/bin/bash
#variables that store the paths
dtree=$(find Downgrade -type f -iname DeviceTree*)
ramdisk=$(find Downgrade -type f -iname *.dmg)
kernel=$(find Downgrade -type f -iname kernelcache*)
#the execution of the command. (Using normal EOF without ”” doesn’t replace the strings.)
./irecovery -s <<"EOF" >/dev/null
/send Downgrade/DeviceTree.n90ap.img3 #This needs to be replaced by $dtree
devicetree
/send Downgrade/048-2441-007.dmg #This needs to be replaced by $ramdisk
ramdisk
/send Downgrade/kernelcache.release.n90 #This needs to be replaced by $kernel
bootx
/exit
EOF
Though I am not sure about your question, you can try replacing string using sed
sed "s/old/new/g"
I need to run the Linux find command using the regex option in a ruby script, but it does not seem to work. All the command does is find files with the extension of rb or erb or js. I know the command itself is fine because I can run it in the command prompt window.
Here is the find command that I want to run:
find . -regex ".*\.\(rb\|erb\|js\)"
Here is what my ruby script looks like:
#!/usr/bin/env ruby
files = `find . -regex ".*\.\(rb\|erb\|js\)"`
puts files
I know I can use the -name option instead of the regex, but it seems like the regex should work. Also, I was able to get the command find . -regex ".*\.rb" to work within my ruby script. It seems almost like it has a problem with the parenthesis or the pipes?
Any suggestions / help would be greatly appreciated. Thanks!
The surmise from stribizhev's comment is correct: Command literals in backwards quotes are subject to the same escaping and replacement/string interpolation rules as string literals in double quotes (").
String literals in single quotes (') are preserved literally, so the following should work without the need for double-escaping headaches:
#!/usr/bin/env ruby
command = 'find . -regex ".*\.\(rb\|erb\|js\)"'
files = `#{command}`
puts files
Ruby has shellescape https://apidock.com/ruby/String/shellescape which lets you explicitely escape arguments to be passed into Unix style shells. Your example could probably be written as
`find . -regex #{Shellwords.shellescape ".*\.\(rb\|erb\|js\)"}`
I'm new to bash and have encountered a problem i can't solve. The issue is i need to use find -name with a name defined as a variable. Part of the script:
read MYNAME
find -name $MYNAME
But when i run the script, type in '*sh' for read, there are 0 results.
However, if i type directly in the terminal:
find -name '*sh'
it's working fine.
I also tried
read MYNAME
find -name \'$MYNAME\'
with typing *sh for read and no success.
Can anyone help me out?
Most probably
read MYNAME
find -name "$MYNAME"
is the version you are looking for. Without the double quotes " the shell will expand * in your *sh example prior to running find that's why your first attempt didn't work
You probably want
find -name "$MYNAME"
since this prevents $MYNAME from being subject to bash's pathname expansion (a.k.a. "globbing"), resulting in *sh being passed intact to find. One key difference is that globbing will not match hidden files such as .ssh, whereas find -name "*sh" will. However since you don't define the expected behaviour you are seeking, it's hard to say what you need for sure.
I am reading in a list of file names:
*.txt *.xml
which are space delimited. I read this into a variable in my ksh script, and I want to be able to manipulate it before putting each of them into a find command. The problem is, as soon as I do anything with the variable (for instance, breaking it into an array), the * resolves into filenames that are in my script's directory. What I want is for the *.txt to remain unchanged, so I can put that into my find command.
How do I do this? Unfortunately, I'm at work and can't just use perl or some other language.
set -f
turns off globbing in ksh, so * and ? characters are not expanded (globbed).
what's wrong with
'*.txt' '*.xml'
? . Else you have to show us more of your issues. Maybe edit your post to include a small test case that illustrates your problem, plus the desired output or intermediate values.