Batch remove extra file extensions in bash [duplicate] - linux

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?

Related

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.

BASH "for ... in ..." don't work with variables [duplicate]

This question already has an answer here:
In bash, how do I expand a wildcard while it's inside double quotes?
(1 answer)
Closed 2 years ago.
I want to write a simple script that does something for every file in user-defined directory. Here's a script that works for predefined directory:
for file in mydir/*; do
printf "$file"
done
Here's similar script that prints name of each file in the directory defined by variable:
for file in "$nicedir*"; do
printf "$file"
done
This second script don't work. Of course, I remembered about slash at the end of the path. (I passed ./ as the argument instead of just .)
Pathname expansion doesn't happen in quoted strings. Keep the wildcard outside of the quotes:
for file in "$nicedir"* ; do
printf '%s\n' "$file"
done
The final slash is usually not required in paths, so you'll more often see
for file in "$nicedir/"*
# or equivalent
for file in "$nicedir"/*

Shell rename a lot of files recursively [duplicate]

This question already has answers here:
Find multiple files and rename them in Linux
(13 answers)
Closed 3 years ago.
I want to rename a lot of files in sub directories with a shell script/command, and I've tried different way without any success.
Here is the files I've got:
root/FOLDER1/media-125150-payasage151.jpg
root/FOLDER1/media-125165-payasage125.jpg
root/FOLDER2/media-1266165-payasage110.jpg
root/FOLDER2/media-1266165-portrait151.jpg
and I want to replace every "payasage" by "paysage"
root/FOLDER1/media-125150-paysage151.jpg
root/FOLDER1/media-125165-paysage125.jpg
root/FOLDER2/media-1266165-paysage110.jpg
root/FOLDER2/media-1266165-portrait151.jpg
I've tried RegExr with rename command or even with a mv approch...
thanks!
Try something along the lines of
for OLD in root/*/media-*-payasage*.jpg; do
NEW=$(echo "$OLD" | sed 's/payasage/paysage/g')
test "$OLD" != "$NEW" && mv "$OLD" "$NEW"
done

How to extract the file name and change the ending in bash? [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
How do I rename the extension for a bunch of files?
(28 answers)
Closed 3 years ago.
I have a bash script that accepts file names that end with .in, for example a1.in a2.in, and I want to take that argument and extract the a1 and add .out to it, how do I do that?
I know accepting an argument is $1 - but how do I extract the a1?
To remove a fixed suffix from an argument (or other variable) use ${1%.in} -- that will remove the trailing .in or do nothing if the argument does not end in .in. To add a suffix, just add it: ${1%.in}.out
To remove any suffix, you can use glob patterns after the % like so: ${1%.*}. This will remove the shortest matching suffix. You can remove the longest matching suffix with %%: ${1%%.*}
If your files have only one extension:
$ echo "a.in" | cut -d '.' -f1
a

Bash delete files from path that contain space [duplicate]

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

Resources