delete or symlink with ^M in filename - bash

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*'

Related

Replace text in a bash script that is not a variable

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"

How to rename file with bash by adding first symbol - dot?

I would like to rename my file from myscript.js to .myscript.js. How can I do it with bash? I've tried different options like mv myscript.js \.*, but it doesn't work. I've tried to experiment by adding non-dot symbol like - mv myscript.js m*, but now I don't even know where my file is (of course, I have a copy ;).
You're trying too hard.
mv myscript.js .myscript.js
Brace expansion
mv {,.}myscript.js
You can use rename utility to rename all *.js file by placing a dot before them:
rename 's/(.+\.js)/.$1/' *.js
Or in pure BASH use this for loop:
for i in *.js; do mv "$i" ".$i"; done
Don't use patterns as the target of mv (or cp for that matter). The command won't do what you want or expect, most of the time: Bash will expand the pattern at the moment when you run the command, using the file names as they were before your command was executed. So .* matches nothing (since the file doesn't exist, yet) and m* will match any file or folder which starts with m. Since you didn't get an error, chances are that the last match was a folder.
There is no way to access the previous parameter of the current command. If you want to avoid typing too much, then use Tab for both file names (so you end up with mv myscript.js myscript.js) and then use Ctrl+Left and Ctrl+Right to move quickly to the start of the second argument to insert the missing ..
You can access the parameters of the previous command, though. But that's not a feature of BASH - it's a feature of readline.

BASH find -name (read variable)

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.

FindUtils from gnuwin32 on Windows 7: simple pattern matching

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.

Keep wildcards from resolving in a ksh script

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.

Resources