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.
Related
My script should check a directory called /etc/scriptbuilder/default/contents which will hold some directories, if a directory in the contents directory matches one in the project directory the files in the contents directory should be copied to the project directory (so, if /etc/scriptbuilder/default/contents/docs and /project/docs both exist, the contents of /etc/scriptbuilder/default/contents/docs should be copied).
I'm having trouble making this work. It has to find a match of the same directory name and copy the contents over from the contents directory to the project directory if they have the same directory name. This is what I have so far:
#! /bin/bash
if [ -d "$/etc/scriptbuilder/default/contents ]; then
if [[ "/etc/scriptbuilder/default/contents" =~ name ]]; then
cp -a #I'm not sure how to copy and check for the name on the project
directory
fi
fi
Something like that finds the the directories you are looking for and prints them.
source="/etc/scriptbuilder/default/contents"
destination="/home/something/project"
# loop the directories of the source folder
cd "$source"
for name in */ ; do
# create the source and the possible destination directory paths
s="$source/$name"
d="$destination/$name"
# check if the directory exists in the project
if [ -d "$d" ]; then
echo "TODO: Perfrom copy of from $s to $d"
fi
done
If you need help with copy command also post a comment.
With loop it could be like this:
Dir1=path_to_contents/contents
Dir2=path_to_project/project
for i in $(find "$Dir1/" "$Dir2/" -printf '%P\n' | sort | uniq -d)
do
cp -r $Dir1/$i $Dir2
done
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
I am sure this is possible with a bash script, but I want to make sure I'm not missing something obvious. Google hasn't been much help, so maybe you can be.
Assume a directory has the following files
dir/file1
dir/newfile2
dir/oldfile3
I would like to figure out the best solution for copying all files in the folder to a folder 2 levels deep based on the first two letters of the filename, so the result would be
dir/f/i/file1
dir/n/e/newfile2
dir/o/l/oldfile3
Something like this should do it:
cd dir
for file in *; do
newpath="${file:0:1}/${file:1:1}"
mkdir -p "$newpath"
cp "$file" "$newpath"
done
Be sure all your filenames are two chars or more, though.
${var:n:m} is simply Bash syntax for "substring of var starting at n of length m."
If there can also be arbitrary subdirectories, either add -r to the cp command if you want to copy recursively or add a test to ignore directories in the for loop:
cd dir
for file in *; do
if [ -f "$file" ]; then
newpath="${file:0:1}/${file:1:1}"
mkdir -p "$newpath"
cp "$file" "$newpath"
fi
done
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"
How could I read the content of a parent folder and if sub-folders are found, then make a tar.gz files of those subfolders found. However, I know that subfolders will have the following filename format: name-1.2.3 What I want is to create a tar.gz file that looks like: name-1.2.3-20100928.tar.gz Any help will be appreciated.
#!/bin/sh
DATE=`/bin/date +%y%m%e`
cd /path/to/your/folder
for folder in *; do
if [ -d $folder ]; then
tar -cvzf $folder-$DATE.tar.gz $folder
fi
done
Better immunize your scripts against spaces in names:
#!/bin/bash
# Yes nested quoting is allowed with $(...) syntax, no matter whether syntax coloration does
DATE="$(/bin/date "+%y%m%e")"
cd /path/to/your/folder
for folder in * ; do
if test -d "$folder" ; then
tar cvzf "$folder-$DATE.tar.gz" "$folder"
fi
done