0

I have a large set of working files of the form a.mp4 b.txt c.avi d.doc etc (the extension is irrelevant to the question). I also have a set of files which include the same named files except with a common specific prefix "broken_" eg broken_a.mp4, broken_b.txt

If I have a.mp4 and broken_a.mp4, I want to move the broken_a.mp4 to a holding directory. If I have broken_d.mp4 but no matching d.mp4, then leave it alone.

I have some code successfully used to identify and move files with the same extension which I'd like to modify

This is the form of working example code for same extension files (kudos to the original author) which I'd like to modify if possible to do the job

#!/bin/bash                                                                                                                                                                                                                                                                      

# Name of source directory                                                                                                                                                                                                                                                       
SOURCE_DIR=.

# Name of destination directory                                                                                                                                                                                                                                                  
DEST_DIR=already_converted_m4v

# Create the destination directory for the moved files, if it doesn't already exist.                                                                                                                                                                                             
[ ! -d $DEST_DIR ] && mkdir -p $DEST_DIR

find $SOURCE_DIR -maxdepth 1 -type f -iname "*.avi"  | while read fin
do
#echo "m4v doing avi"                                                                                                                                                                                                                                                            
  fm4v=${fin/.avi/.m4v}
 [ -f "$fm4v" ] && gmv -v --backup=numbered "$fin" $DEST_DIR/
done

My garbage first attempt which clearly doesnt work looks horribly like:

#!/bin/bash                                                                                                                                                                                                                                                                      

# Name of source directory                                                                                                                                                                                                                                                       
SOURCE_DIR=.

# Name of destination directory                                                                                                                                                                                                                                                  
DEST_DIR=Already_broken

# Create the destination directory for the moved files, if it doesn't already exist.                                                                                                                                                                                             
[ ! -d $DEST_DIR ] && mkdir -p $DEST_DIR

find $SOURCE_DIR -maxdepth 1 -type f -iname "*"  | while read fin
do
#echo "working to find existing broken and unbroken files"                                                                                                                                                                                                                       
  filetest_basename=$(basename "$fin" )
  filetest_extension=$(extension "$fin" )
echo $filetest_basename
echo $filetest_extension
  fileok=${filetest_basename/!broken_/broken_}
 [ -f "$fileok" ] && gmv -v --backup=numbered "$fin" $DEST_DIR/
done

Grateful for help

oguz ismail
  • 1
  • 16
  • 47
  • 69
Richard L
  • 99
  • 1
  • 6
  • 1
    Are `a.mp4` and `broken_a.mp4` in the same directory? Somehow visualizing the directory structure would make this clearer – oguz ismail Oct 20 '19 at 12:55
  • the files are all in the same directory. My current example code uses -maxdepth 1 to limit the search. – Richard L Oct 20 '19 at 13:10

1 Answers1

1

find is irrelevant here, a simple shell loop would suffice:

SRCDIR='.'
DSTDIR='Already_broken'

if ! [ -d "$DSTDIR" ]; then
    mkdir -p -- "$DSTDIR"
fi

for broken in "$SRCDIR"/broken_*; do
    if [ -f "${broken%"${broken##*/}"}${broken##*/broken_}" ]; then
        echo gmv -v --backup=numbered "$broken" -- "$DSTDIR"
    fi
done

If its output looks good, remove echo.

Some notes:

  • We can't use ${broken/broken_} here for SRCDIR might contain broken_ in the future.
  • Nested PE (${broken##*/}) needs to be quoted for its result might contain metacharacters and that would bring about undesired results.
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • if I had to extend this to search for files which in all respects had the same filenames (nothing to do with a prefix this time) but one set had an additional suffix ".rar" is there a simple modification to the code? eg 1.txt.rar and 1.txt – Richard L Oct 20 '19 at 13:40
  • 1
    @RichardL that'd be even easier, just replace if line with `if [ -f "$broken.rar" ]; then` – oguz ismail Oct 20 '19 at 13:43