Bash delete files from path that contain space [duplicate] - linux

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 5 years ago.
#!/bin/bash
files_path="/Volumes/HDD/Bogdan Data/"
rm -r "$files_path/files/*"
I'm getting an error that path cannot be found, probably due to the space character in the folder name. How should I approach this?

$files_path needs to be quoted, but the * glob must not be.
rm -r "$files_path"/files/*

Related

How do I pass quoted shell variables as arguments correctly? [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Variable containing multiple args with quotes in Bash
(4 answers)
Closed 10 months ago.
As part of a build pipeline I have a shell script that zips up a directory. I want to use a variable to define which patterns should be ignored.
I'm doing something like this:
IGNORE='"*.md" "some-folder/*"'
zip -x $IGNORE -r my-zip.zip ./*
Which doesn't appear to work, the ignored files and folders are still included in the zip archive.
This does work if I create the command and then pipe it through to sh though, so I'm confident that the variable contains the correct values:
echo "zip -x $IGNORE -r my-zip.zip ./*" | sh
I think it might be something to do with the quotes, since this works as expected without them. However this fails as soon as I attempt to add more than 1 pattern to IGNORE.
IGNORE=*.md
zip -x $IGNORE -r my-zip.zip ./*
What am I missing in order to be able to pass these patterns correctly quoted?
Edit: this does also not appear to work as an array, as suggested by this question.
IGNORE='"*.md" "some-folder/*"'
EXCLUDE=($IGNORE)
zip -x ${EXCLUDE[#]} -r my-zip.zip ./*

Batch remove extra file extensions in bash [duplicate]

This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
bash - command substitution is omitting whitespaces
(1 answer)
Closed 12 months ago.
I converted some files to another format but in doing so added an extra extension. For example foo.bar.temp. I wrote a script to delete the .temp, but it doesn't work when the filenames have spaces.
for f in *; do mv "$f" $(basename "$f" .temp) ; done
If I double escape "'$f'" then basename won't read the extension. If I leave it as is then it will think that the second word in the title is the directory I want to move to.
How can I just remove the .temp?

How to expand variable in bash tar command [duplicate]

This question already has answers here:
bash script execute command with double quotes, single quotes and spaces
(2 answers)
Variable containing multiple args with quotes in Bash
(4 answers)
Closed 1 year ago.
I have a tar command to create .tar.gz file. It looks like below:
exclude="--exclude='.cache' --exclude='.composer' --exclude='.local' --exclude='.config'"
tar "$exclude" -czf "$backupsDir/$timestamp-home.tar.gz" /home
The problem is the $exclude variable in not evaluated and excluded directories are still in the archive. Can somebody tell me what is wrong with it? Thanks a lot.

Replace hyphens with underscores in bash script [duplicate]

This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
Trying to write a bash script and in one part of it I need to take whatever parameter was passed to it and replace the hyphens with underscores if they exist.
Tried to do the following
#!/usr/bin/env bash
string=$1
string=${string//-/_}
echo $string;
It's telling me that this line string=${string//-/_} fails due to "Bad substitution" but it looks like it should do it? Am I missing something?
There is nothing wrong with your script, and it should work in modern versions of Bash.
But just in case you can simplify that to :
#!/bin/bash
echo "$1" | tr '-' '_'
This is in case that parameter substitution does not work ( which seems to be your case ).
Regards!

Change the output of find command [duplicate]

This question already has answers here:
How to strip leading "./" in unix "find"?
(8 answers)
Closed 8 years ago.
Hello guys I'm using the find command to find the .apk files in a directory. But the output of the find command is **./**foo.apk.
I don't want to have this ./.
cd output/dist
output_apk=`find ./ -name "*.apk" -print0`
echo "$output_apk"
The output is ./foo.apk.
I have try the sed command with no luck.
find output/dist -name "*.apk" |
sed 's%^output/dist/%%'
This also avoids the useless cd and removes the erroneous -print0. If you are not piping into a program which requires null-terminated input, this option is wrong.

Resources