Change multiple file names in a pattern [duplicate] - bash

This question already has answers here:
Renaming files in bash
(5 answers)
Closed 7 years ago.
I have a number of files with somewhat similar names:
HappyBD_Stereo_144kbps.mp3
HappyBD_Stereo_192kbps.mp3
HappyBD_Stereo_256kbps.mp3
...
For some reason, I need to change/shorten these names into something like
HappyBD_Ste_144k.mp3
HappyBD_Ste_192k.mp3
HappyBD_Ste_256k.mp3
...
Can someone recommend a good way to automate this kind of file name changes? Thanks.

you can try,
for filename in `ls *_Stereo_*.mp3`; do
newfilename=$(sed 's/_Stereo_/_Ste_/g' <<< $filename);
mv $filename $newfilename;
done

Related

bash script to create folder names out of a portion of a filename [duplicate]

This question already has answers here:
Remove a fixed prefix/suffix from a string in Bash
(9 answers)
Closed last month.
I'm new to bash, and coding. I have a list of files:
test-T01___2022.txt
test-T01__2021.txt
test-T01_NONE.txt
test-T02___2022.txt
test-T02__2021.txt
test-T02_NONE.txt
test-T03___2022.txt
test-T03__2021.txt
test-T03_NONE.txt
I'm trying to write a script to create folders T01 (containing *T01 files), T02 (containing all files with T02), etc. I'm trying with wildcards and regexps and something similar to this post but having some trouble. I appreciate some help.
Many thanks!
Use the bash prefix and suffix removal operations. See the link in the comments for more details.
For example:
files=...
for file in $files
do
a=${file#test-}
dir=${a%%_*}
mkdir "$dir"
mv "$file" "$dir"
done

echo file names in reverse order [duplicate]

This question already has answers here:
How to reverse a list of words in a shell string?
(18 answers)
Sort 'ls' output by name
(13 answers)
Closed 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I would like to list files in reverse order, list of files listed are as below, the purpose is to echo all the file contents into another file.
vcp.status-200.txt
vcp-status-400.txt
vcp-status-500.txt
vcp-status-000.txt
I am currently running below command; echo will list all the files and xargs cat will append the content of these files into OutputFile.
echo *${USER}*status*.txt | xargs cat >> OutputFile
Current Output
vcp-status-000.txt vcp-status-200.txt vcp-status-400.txt vcp-status-500.txt
Intended Output
vcp-status-500.txt vcp-status-400.txt vcp-status-200.txt vcp-status-000.txt

Bash: adding a key/value pair to an existing config file [duplicate]

This question already has answers here:
How to append output to the end of a text file
(13 answers)
Closed 8 months ago.
I have an existing config config.ini file with the following content:
VALUE_A=a
VALUE_B=b
Using Bash, I'd like to add a new key-value pair VALUE_C=c to get the following:
VALUE_A=a
VALUE_B=b
VALUE_C=c
Is there a concise way to do this with Bash (ideally a one liner)?
As suggested in the comments, the answer is simply:
echo VALUE_C=c >> config.ini

Bash: how to rename a file to a string containing forward slashes? [duplicate]

This question already has answers here:
Is it possible to use "/" in a filename?
(8 answers)
Closed 1 year ago.
I have a file that I want to rename to a date like "20/02/21", but if I do mv file.txt 20/02/21 it interprets the forward slashes as referencing sub-folders. Is there a way to do this?
No, there's no way to do it. On Unix forward slash / is used to
separate directories and cannot be used in the filename. You have to
use another delimiter - 20\02\21, 20-02-21, 20.02.21 etc.

How to iterate over pairs of files with corresponding names in Bash [duplicate]

This question already has answers here:
Looping over pairs of values in bash [duplicate]
(6 answers)
Closed 3 years ago.
I have a folder with test cases named like test_1_in, test_1_out,
test_2_in, test_2_out etc. I want to write a script, that can test my program with these pairs, I suppose it would do something like this in a loop
diff <($program < $test_in) <($test_out)
So the question is what is the best way to do it in Bash? How to iterate through the pairs of files? And also how to capture exception, that is print some error message if diff shows some difference?
Iterate over *_in, and generate *_out using parameter expansions:
for test_in in test_*_in; do
test_out="${test_in%_*}_out"
if diff <("$program" "$test_in") "$test_out"; then
echo "$test_in failed."
fi
done

Resources