Batch file rename based on pattern in bash - bash

So I have a bunch of files named like this:
Movie Name Here [720p].mp4
And I want to rename them like this:
Movie Name.mp4
I have tried stuff like:
mv ./*\ [720p].mp4 ./*.mp4
But it doesn't work, for whatever reason. Any ideas on the quickest way to do this?

Use this code to remove all ' [720p]':
rename 's/ \[720p\]//g' ./*

Related

Replace string path in log file with current folder name

I'm new here and already tried to find solution to the following requirement without success. I'm trying to achieve this:
I have these 5 folders:
ServiceEngine
PaymentEngine
InvoiceEngine
ProcessEngine
OrderProcessEngine
Inside each of these folders, I have a log file with default path location to store the log files e.g. ServiceEngine/logs
The log file contain the following path structure:
name="RollingRandomAccessFile" fileName="logs/engine.log"
filePattern="logs/engine-%i.log"
I expect to find a way that I retrieve the name of the current folder which I'm in and replace the string engine with folder name
Example: I'm in ServiceEngine folder and execute a command that retrieve the current folder name. The expected result is:
name="RollingRandomAccessFile" fileName="logs/ServiceEngine.log"
filePattern="logs/ServiceEngine-%i.log
Later I change the directory to PaymentEngine and the expected result is:
name="RollingRandomAccessFile" fileName="logs/PaymentEngine.log"
filePattern="logs/PaymentEngine-%i.log
and so on. Maybe there is a smarter way to create a script that update the string in a loop like do; if ... fi; done or to use the for in ... loop.
Do you mean something like this?
~/ServiceEngine$ cat logfile
name="RollingRandomAccessFile" fileName="logs/engine.log"
filePattern="logs/engine-%i.log"
~/ServiceEngine$ awk -v path=$(basename $(pwd)) 'gsub("engine", path)' logfile
name="RollingRandomAccessFile" fileName="logs/ServiceEngine.log"
filePattern="logs/ServiceEngine-%i.log"
See basename, declaring variables in awk and awk gsub.
I guess I don't understand your question, but:
dir=$( basename $( pwd ) )
echo name="RollingRandomAccessFile" \
fileName="logs/$dir.log" \
filePattern="logs/$dir-%i.log"
Is that what you're looking for?
It sounds like you are looking for something like this:
$ sed 's/\bengine\b/'$(basename $(pwd))'/' logs
When run from within one of your folders, it spits out the text you're asking for. It wasn't clear what you wanted to do with that text though.

How to iteratively rename files in Bash

I am trying to iterate over files in a folder, renaming them as foldername1, foldername2, etc. However, I'm getting an error which says that the mv isn't being used correctly.
So far my code looks like this:
FILES='(Full Path)/Macbeth/audio/'
for file in "$FILES"*
do
mv $file 'Macbeth'$i''
done
The final code should iterate through the files and rename them as, in this case Macbeth1.mp3, but I'm not sure how the Bash syntax works.
As suggested by Cyrus, the solution was to use the full path

Merge all .wav files in a folder that start by the same prefix

I have filelists that look like this:
S134_001.wav
S134_002.wav
S134_003.wav
S149_001.wav
S149_002.wav
S149_003.wav
S16_001.wav
S16_002.wav
S16_003.wav
S16_004.wav
S16_005.wav
S16_006.wav
S272_001.wav
S272_002.wav
S272_003.wav
S272_004.wav
S272_005.wav
S272_006.wav
S272_007.wav
S374_001.wav
S396_001.wav
S92_001.wav
And I want to merge S134_001.wav, S134_002.wav, S134_003.wav into S134.wav; S149_001.wav, S149_002.wav and S149_003.wav into S149.wav; and so on ( preferably using sox ).
I also want to delete the original files.
How do I achieve that? Bash is the preferred solution, but any programming language will do. Thanks.
You can use a for loop like this:
for f in *_001.wav
do
pre=${f%%_001.wav}
# use either cat or sox or whatever works:
#cat "${pre}_"*.wav > "${pre}.wav"
sox -m "${pre}_"*.wav "${pre}.wav"
#rm -rf "${pre}_"*.wav
done
Here we iterate over the *_001.wav files and
derive the prefix from the file
and then concat the files with the same prefix into a new file.
If everything works, you can remove the comment before the rm command.

Simple Bash Script: Change names of files to mimic directories

I have 312 directories labeled,
Ion_0001- Ion_0312.
In each directory I have a file light.out. I'd like to change the file names in each directory to, for example:
Ion_0001.out
I believe I also need to substitute the / so that my output DOESNT look this this:
Ion_0001/.out
Can any one help me out with a simple script??
This is what I've tried:
#!/bin/bash
for dir in */
do
cd $dir
for filename in *.out; do
mv $filename ${filename//$dir.out}
done
cd ..
done
Thanks!
Not a free coding service, but it's simple enough to not make it worth arguing about...
Assuming this file structure:
Ion_0001/
Ion_0001/light.out
Ion_0002/
Ion_0002/light.out
...
Run this code in a script or just at the command line:
for i in Ion_0*
do
mv "${i}/light.out" "${i}/${i}.out"
done
Resulting in this structure:
Ion_0001/
Ion_0001/Ion_0001.out
Ion_0002/
Ion_0002/Ion_0002.out
...
Is that what you were looking for?
for dir in Ion*/; do
mv "${dir}light.out" "${dir}${dir%/}.out"
done
The trailing slash in the Ion*/ pattern limits the results to directories only, but the slash will be present in the variable's value.

Create new files from existing ones but change their extension

In shell, what is a good way to duplicating files in an existing directory so that the result gives the same file but with a different extension? So taking something like:
path/view/blah.html.erb
And adding:
path/view/blah.mobile.erb
So that in the path/view directory, there would be:
path/view/blah.html.erb
path/view/blah.mobile.erb
I'd ideally like to perform this at a directory level and not create the file if it already has both extensions but that isn't necessary.
You can do:
cd /path/view/
for f in *.html.erb; do
cp "$f" "${f/.html./.mobile.}"
done
PS: This replaces first instance of .html. with .mobile., syntax is bash specific (let me know if you're not using BASH).

Resources