Create a subdirectory in bash based on $1 - bash

I have a HD full of directories named for CD albums which contain *.wav files.
I want a bash script that takes $1: mp3-squeeze kind_of_blue
and the script changes dir to "kind_of_blue",
creates a directory called $1_MP3 example: "kind_of_blue_MP3",
and lame outputs the mp3's to the newly created "kind_of_blue_MP3" directory.
Sadly, what I have creates "$album" not "kind_of_blue_MP3". Anyone have a solution?
#!/bin/bash
#to convert .wav to .mp3 using lame utility
album=$1
cd $album
mkdir -p '$album_MP3'
for i in *.wav ; do
echo $i
bname=`basename $i .wav`
lame --preset fast extreme $i $bname.mp3
done
mkdir mp3 && mv *.mp3 mp3

Wrong quotes. Plus, you have to end the variable name.
mkdir -p "${album}_MP3"

Related

Rename a file when I don't know its name - shell script

I am trying to rename a file which is the only file in a directory.
It is a podcast download which changes name each day so I don't know what it is called but it always ends in .MP3
I want to rename it to news.mp3
I have tried the following based on another solution on this site but it appends the news to the file
#!/bin/sh
for file in *.MP3; do
mv "$file" "${file/.MP3/news.mp3}"
done
If it's the only file in the directory you can just write the following command:
mv directory_name/* directory_name/news.mp3
In case there are few files or if dir is empty:
shopt -s nullglob
src="/path/to/dir/with/files"
dst="/destanation/folder"
i=1
cd "$src"
for f in *; do
mv "$f" "$dst/new_name_$((i++))"
done

Recursively scan and identify video files with FFMPEG

UPDATE
Thank you everyone for sharing up your suggestions. I was able to make the work properly by modifying it as follows.
#!/bin/bash
ROOTPATH="/Volumes/NVME-RAID/ASSET-Processing/CORRUPT-SCAN/SCAN " # manually define root path to your folder that has subfolders
for subdir in *; do
cd ${ROOTPATH}${subdir}
mkdir 00errors
for path in *.{MOV,mov,MP4,mp4}; do
ffmpeg -i "${path}" -f null -; echo$?
RC=$?
if [ "${RC}" -ne "0" ]; then
# Do something to handle the error.
mv ${path} ./00errors
fi
done
cd ../
done
The only issue i have now is that it does not appear to be traversing the subfolders. As I understand it it should create a "00errors" folder in within EACH sub folder and move the errors files within that sub folder.
trying to sift through 14TB of recovered video...
I'm trying to figure out how to properly convert this script so it will run on a bash/MacOS. I'm coming to a wall with the "ROOTPATH" call because MacOS doesn't use /mnt
#!/bin/bash
ROOTPATH="/mnt/f/00test/" # manually define root path to your folder that has subfolders
for subdir in *; do
cd ${ROOTPATH}${subdir}
mkdir 00errors
for path in *.mp4; do
ffmpeg error -i "${path}"
RC=$?
if [ "${RC}" -ne "0" ]; then
# Do something to handle the error.
mv ${path} ./00errors
fi
done
cd ../
done
REF Quickly check the integrity of video files inside a directory with ffmpeg
If your test dir was in your home directory /Users/username aka ~:
ROOTPATH="~/00test" # manually define ROOTPATH to your folder that has subfolders
for subdir in *; do
cd ${ROOTPATH}/${subdir}
I follow the convention to not use trailing slashes in paths; here I have moved the slash to the cd command.

Downloading only new subtitles in RaspberyPi

I have problems with writing downloading subtitles on Raspberry Pi with bash script.
The issue is I would like to have 1 script running with cron every hour or so that would download subtitles for new video files in some directory.
There is a program called qnapi that automatically downloads subtitles in the correct format in the directory but it works for 1 folder and always re-downloads subtitle files.
usage is:
qnapi /path/to/folder
So what I've found is
find /media/pi/RaspStorage -iname "*.mkv"
basically gives me a list of all movie files. Now I would like to somehow compare if a *.srt subtitle file with a same base name exists and if no then somehow loop through all found files with qnapi command. Write it in a bash script and cron it.
Perhaps something along the line of this would work (untested, may have typos).
I'm guessing at the syntax of qnapi from a google search.
Basically, symlink videos that don't have subtitles into a temporary folder, download the subtitles there, then move them back to the main folder.
#!/bin/bash
MEDIA=/media/pi/RaspStorage
WORK=/tmp/workingfolder
mkdir -p "$WORK"
pushd "$WORK" || { echo "ERROR: no working directory"; exit 1; }
# assuming subtitle name is video name except with ".srt" extension
find "$MEDIA" -iname "*.mkv" | while read video; do
vidnoext="${video%????}"
viddir="${vidnoext%/*}"
subtitle="${vidnoext}.srt"
if [ ! -f "$subtitle" ]; then
# this assumes bare filenames (without directory) are distinct
ln -s "$video" "${vidnoext}.mkv"
fi
done
qnapi -c .
find . -iname '*.srt' | while IFS= read -r subtitle; do
video="$(realpath "$(subtitle%????}.mkv")"
mv -i "$subtitle" "$(dirname "$video")/"
done
rm *
popd
rmdir "$WORK"
thanks for help :)
I got it working
#!/bin/bash
echo "Updated DLNA folder"
sudo minidlnad -R
for file in $(find /media/pi/RaspStorage -name '*.mkv' -or -name '*.avi'); do
name=${file:: -4}
ext=".srt"
name=$name$ext
if [ -f $name ]; then
echo "Napisy już ściągnięte dla:" "$(basename "$file")"
else
qnapi "$(dirname "$file")"/"$(basename "$file")"
fi
done
Works fine. All subtitles are downloading and if there are some already it's skipping

How to move files from subfolders to their parent directory (unix, terminal)

I have a folder structure like this:
A big parent folder named Photos. This folder contains 900+ subfolders named a_000, a_001, a_002 etc.
Each of those subfolders contain more subfolders, named dir_001, dir_002 etc. And each of those subfolders contain lots of pictures (with unique names).
I want to move all these pictures contained in the subdirectories of a_xxx inside a_xxx. (where xxx could be 001, 002 etc)
After looking in similar questions around, this is the closest solution I came up with:
for file in *; do
if [ -d $file ]; then
cd $file; mv * ./; cd ..;
fi
done
Another solution I got is doing a bash script:
#!/bin/bash
dir1="/path/to/photos/"
subs= `ls $dir1`
for i in $subs; do
mv $dir1/$i/*/* $dir1/$i/
done
Still, I'm missing something, can you help?
(Then it would be nice to discard the empty dir_yyy, but not much of a problem at the moment)
You could try the following bash script :
#!/bin/bash
#needed in case we have empty folders
shopt -s nullglob
#we must write the full path here (no ~ character)
target="/path/to/photos"
#we use a glob to list the folders. parsing the output of ls is baaaaaaaddd !!!!
#for every folder in our photo folder ...
for dir in "$target"/*/
do
#we list the subdirectories ...
for sub in "$dir"/*/
do
#and we move the content of the subdirectories to the parent
mv "$sub"/* "$dir"
#if you want to remove subdirectories once the copy is done, uncoment the next line
#rm -r "$sub"
done
done
Here is why you don't parse ls in bash
Make sure the directory where the files exist is correct (and complete) in the following script and try it:
#!/bin/bash
BigParentDir=Photos
for subdir in "$BigParentDir"/*/; do # Select the a_001, a_002 subdirs
for ssdir in "$subdir"/*/; do # Select dir_001, … sub-subdirs
for f in "$ssdir"/*; do # Select the files to move
if [[ -f $f ]]; do # if indeed are files
echo \
mv "$ssdir"/* "$subdir"/ # Move the files.
fi
done
done
done
No file will be moved, just printed. If you are sure the script does what you want, comment the echo line and run it "for real".
You can try this
#!/bin/bash
dir1="/path/to/photos/"
subs= `ls $dir1`
cp /dev/null /tmp/newscript.sh
for i in $subs; do
find $dir1/$i -type f -exec echo mv \'\{\}\' $dir1/$i \; >> /tmp/newscript.sh
done
then open /tmp/newscript.sh with an editor or less and see if looks like what you are trying to do.
if it does then execute it with sh -x /tmp/newscript.sh

Moving multiple files in different folder

I need make multiple folder having the name from file without extension with this command
for i in *.avi; do mkdir "${i%.*}"; done
I don't know how to mv the related file in the folder made...
Thanks for any help
Thanks. Let's say I want to do a general script to record on Textexpander which ask me for the file extension a sort of this :
echo "Type the suffix of the file follewe by enter:"
read ext
for i in *.ext
do
mkdir "${i%.ext}"
mv "$i" "${i%.ext}"
done
But this is not working.
Use the mv command:
for i in *.avi
do
mkdir "${i%.*}"
mv "$i" "${i%.*}"
done

Resources