Linux command to remove multiple files with two dot in the file name - linux

Example file names is
abc.edf.xdc
pqe.ide.xdc
rm -rf "*.\*.xdc" is not working

Drop the quotes and it works:
/tmp/a$ touch abc.edf.xdc pqe.ide.xdc
/tmp/a$ ls
abc.edf.xdc pqe.ide.xdc
/tmp/a$ rm -f *.*.xdc
/tmp/a$ ls
/tmp/a$

rm -rf *.xdc should match all of those files. There's no need to put the extra "*.".

Source man bash
Enclosing characters in double quotes preserves the literal value of
all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. The characters $ and `
retain their special meaning within double quotes.
So you don't need to use double quotes, just give rm -vf *.*.xdc

Related

Delete files in a variable - bash

i have a variable of filenames that end with a vowel. I need to delete all of these files at once. I have tried using
rm "$vowels"
but that only seems to return the files within the variable and state that there is "No such file or Directory"
Its your use of quotes: they tell rm that your variables contents are to be interpreted as a single argument (filename). Without quotes the contents will be broken into multiple arguments using the shell rules in effect.
Be aware that this can be risky if your filenames contain spaces - as theres no way to tell the difference between spaces between filenames, and spaces IN filenames.
You can get around this by using an array instead and using quoted array expansion (which I cant remember the syntax of, but might look something like rm "${array[#]}" - where each element in the array will be output as a quoted string).
SOLUTION
assigning the variable
vowel=$(find . -type f | grep "[aeiou]$")
removing all files within variable
echo $vowel | xargs rm -v

How can I make a file named "\$*'PNP'*$\" using touch command

How can I make a file named "\$*'PNP'*$\" using touch command?
what I did:
% touch '"\$*'PNP'*$\"'
% ls
"\$*PNP*$\"
I need ' beside P
One approach is to put the desired filename in a Try:
$ touch "\"\\\$*'PNP'*$\\\""
$ echo *PN*
"\$*'PNP'*$\"
If you run ls, be aware that it may put escapes in the file name:
$ ls
'"\$*'\''PNP'\''*$\"'
In the above touch command, escapes are required. First, to put a double-quote inside a double-quoted string, it must be escaped as \". Second to put a backslash in a double-quoted string, it must also be escaped as \\. Inside a double-quoted string, ' does not need to be escaped.
Alternative: single-quoted string
$ touch '"\$*'\''PNP'\''*$\"'
$ echo *PN*
"\$*'PNP'*$\"
The above has five strings:
'"\$*'
\'
'PNP'
\'
'*$\"'
Strings 1, 3, and 5 are single-quoted strings. Strings 2 and 4 are unquoted but escaped single-quotes.
Just wrap it into single quotes:
touch '"\$*'PNP'*$\"'

Bash remove quotes for String argument

I need to execute a command in bash that takes a constructed argument. The string argument needs to be passed without quotations. How do I remove the quotes?
This does not work:
Tracks='bark.mov'
TrackDir='~/soundtracks/'
TrackPath=$TrackDir$Track
omxplayer -o local $TrackPath
This does not work:
omxplayer -o local '~/soundtracks/bark.mov'
This does work:
omxplayer -o local ~/soundtracks/bark.mov
Tilde expansion only works outside of quotes. Get rid of the quotes when defining the variables, but do include quotes when expanding them.
Tracks=bark.mov
TrackDir=~/soundtracks/
TrackPath=$TrackDir$Track
omxplayer -o local "$TrackPath"
You need to leave the ~ unquoted so that the shell can expand it to the user's home directory. Further, it's better practice to add the path separator when you join the two, to make it explicit. Having two adjacent / in the path doesn't hurt.
Tracks=bark.mov
TrackDir=~/soundtracks/
TrackPath=$TrackDir/$Track
omxplayer -o local "$TrackPath"

BASH find and replace in all files in directory using FIND and SED

I need to look for and replace certain strings for all files in a directory, including sub-directories. I think I'm nearly there using the following method which illustrates my general approach. I do much more inside the -exec than just this replace, but have removed this for clarity.
#!/bin/bash
#call with params: in_directory out_directory
in_directory=$1
out_directory=$2
export in_directory
export out_directory
#Duplicate the in_directory folder structure in out_directory
cd "$in_directory" &&
find . -type d -exec mkdir -p -- "$out_directory"/{} \;
find $in_directory -type f -name '*' -exec sh -c '
for file do
#Quite a lot of other stuff, including some fiddling with $file to
#get rel_file, the part of the path to a file from
#in_directory. E.g if in_directory is ./ then file ./ABC/123.txt
#will have rel_file ABC/123.txt
cat $file|tr -d '|' |sed -e 's/,/|/g' > $out_directory/$rel_file
done
' sh {} +
One issue is likely how I've tried to write the file to pipe the output to. However, this isn't the main/only issue as when I replace it with an explicit test path I still get the error
|sed -e 's/,/|/g' |No such file or directory
which makes me think the cat $file part is the problem?
Any help is massively appreciated as always - this is only the second BASH script I've ever had to write so I expect I've made a fairly basic mistake!
Your "inner" single quotes are being seen as "outer" single quotes and causing you problems. You think you are quoting the | in the tr command but what you are actually doing is ending the initial single-quoted string having an unquoted | and then starting a new single-quoted string. That second single-quoted string then ends at the single-quote that you believe is starting the sed script but is instead ending the previous single-quoted string, etc.
Use double quotes for those embedded single quotes if you can. Where you can't do that you have to use the '\'' sequence to get a literal single-quote in the single-quoted string.

rm adding extra backslash to variable

I'm writing a very simple script, that will interact with my git repository, but I've reached a point, where I can't figure out why the following is happening.
Having:
destPath='~/Dropbox/Shared/Alex\&Stuff'
destFile='file.zip'
#git archive --format zip --output $destFile master
echo $destPath/$destFile
rm $destPath/$destFile
The echo outputs the correct path:
~/Dropbox/Shared/Alex\&Stuff/file.zip
But the rm fails with the following:
rm: cannot remove ‘~/Dropbox/Shared/Alex\\&Stuff/file.zip’: No such file or directory
So, why the extra backslash is added when rm is executed? Alex\\$Stuff instead of Alex\$Stuff ?
Tilde character needs to be outside quote to be expnded:
destPath=~/Dropbox/Shared/Alex\&Stuff
destFile='file.zip'
#git archive --format zip --output $destFile master
echo "$destPath/$destFile"
rm "$destPath/$destFile"
Try
destPath="$HOME/Dropbox/Shared/Alex\&Stuff"
Tildas do not always expand to $HOME. And the ampersand doesn't need the backslash, unless you want an actual backslash there.
As for the double backslash, I would guess that that's how rm quotes its internal strings (i.e., backslashes have special meaning and a single backslash needs to be written as '\'--C does it this way, for example)

Resources