LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2015, 09:51 AM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
BASH checking DIr for empty of certain files before deleteing everything + dir


Hello You, I have written a BASH script that checks the files inside a directory, strips off the unwanted stuff, renames it then does the same to the directory, then moves the new file into the new directory, leaving the old dir with some files in it at time, (depending on what was in it in the first place).

this maybe a bit un orthodoxed but because my first writting of my questions was not uderstandalbe so maybe if you where to copy and paste that script into a new file place it in a seperate dir then set up one more dir to work out of then copy in a seperate movie with its own dir then just movie inin the working root dir then copy a tv series dir with some episodes within that dir too, with out MB that part is ok


then goto the command line after you place in the area's up top where it tells you to put where what dir path the script is in and what dir path the files you what to process are at then where what path and dir you want the new one put first then run the script off the command line by cd to the dir the script is in then add the amount of files you what to process for that run off the command line $./script-name Number-of-files-to-process - ./modme 40
then let it run take a look at the output you'd get a better idea of what I am talking about.

how it creates the new directories some with the MB added to it and SOME without it, I'd like all of them two types to have MB added to it, and the TV one it creates a new dir and puts all of the same tv shows within that dir leaving the MB off of that dir
then delete all of the old dir after all of the files have been completed not while it is working on the TV dir that has more then one move within it before it gets done with it. losing the rest of the movies before they get modified and moved into the new dir

Quote:
###########
# Create Directories and move the file
##################################
this is where the code needs to go -- yes?


Code:
#!/bin/bash

#set -x #FOR DEBUGGING

typeset -i xf mf sn bn hn numbertoconvert lastNum numberToConvert
typeset -i countnum i BigNum bn hn store Left2Do gh TotMp3Left
typeset -i totalMovieFilesLeft q checkmeonce
let totalMovieFilesLeft=0 q=0

let xf=0 mf=0 i=0 numberToConvert=0 Left2Do=0 gh=0 TotMp3Left=0
let checkmeonce=0
echo ; echo ; echo 

maxSaved=0

################### SET DIRECTORIES PATHS ############
#
#   you can use "$HOME/root-dir/BAND-NAME/ALBUM" /song.mp3 || flac
#   for your working directory
#
# ### IMPORTANT !!!!!! ######
#
# for this script to add tags- artist/band album and title to tags
# for files that do not have them already then you HAVE TO SET UP
# your directories and have all of your soundFiles.mp3 || flack 
# in the album folder as follows
#
#  $HOME/root-working-dir/artistName-or-bandName/album 
# it uses the folder names as for tags and the file name as the 
# title tag for the files it will add them to both the original and
# resampled files and categories then accordingly 
#
# for path to in this these settings do not put the last /
# in the path settings example
#  working_dir="$HOME/working-dir/artist/album"
##########################################################

# Directory that orginal files are in
working_dir="/media/data/temp1"



# directories for (resampled) files to go into

move_files_to="/media/data/temp4"



# where you keep this script to run it in a terminal
# it is written so that you do not have to put it in the 
# working directory or directories to eliminate having to put
# a separate script in every folder to run it
##############################################

script_dir="/media/data/tmp"

#####################
## DO NOT CHANGE ####
runTime=1 ###########        
convertN=$1 #########
#####################
# gets amount of files wanted to resample on run
# from command line args
function rst(){

if [[ convertN -lt runTime ]]; then
	echo " you forgot to enter an amount to resample"
	ConvertNum=0
	exit 1
	else
	ConvertNum=$((convertN))
return $ConvertNum #pass the var to space land

fi

}

rst

echo ""$working_dir" START - creating list of files"
#gets then echo's how many of each type file are in all of the dirictories
numberToConvert=$ConvertNum
MAXNUM="$(find "$working_dir" -type f -name "*.*" | wc -l)"
MAXMP4="$(find "$working_dir" -type f -name "*.mp4" | wc -l)"
echo "max MP4 "$MAXMP4""
echo
MAXAVI="$(find "$working_dir" -type f -name "*.avi" | wc -l)"
echo "Max AVI "$MAXAVI""
echo
MAXM4V="$(find "$working_dir" -type f -name "*.m4v" | wc -l)"
echo "Max M4V "$MAXM4V""
echo
MAXMKV="$(find "$working_dir" -type f -name "*.mkv" | wc -l)"
echo "Max MKV "$MAXMKV""
echo
MAXFLV="$(find "$working_dir" -type f -name "*.flv" | wc -l)"
echo "Max MKV "$MAXFLV""
echo
MAXCapMP4="$(find "$working_dir" -type f -name "*.MP4" | wc -l)"
echo "Max MKV "$MAXCapMP4""
echo

# totals all of the above to get max amount of movie files all every type searched for
 totalMovieFiles=`echo $MAXMP4 + $MAXAVI + $MAXM4V + $MAXMKV + $MAXFLV + $MAXCapMP4  | bc`
 echo "Totle movie Files - > "$totalMovieFiles""
 totalMovieFilesLeft=$totalMovieFiles
 
Left2Do=$MAXNUM  
TotMp3Left=$MAXMP3

mf=$numberToConvert # for letting user know how many left to do
let xf=0

# finds all types of files in all directories then loops/proccess the amount the user told it
# to on the command line
find "$working_dir" -type f -name "*.*" | while [ $xf  -lt $numberToConvert ] ; 
	do read FILENAME;

 #gets baseDirName, dir, file, ext
 j=$FILENAME
 xpath=${j/*} 
 xbase=${j##*/}
 xfext=${xbase##*.}
 xpref=${xbase.*}
 
 path1=${xpath}
 pref1=${xpref} #this is the file name
 ext1=${xfext}


 #checks to see if varitable is empty meaning no More files to do 
 # Just incase user puts in more times to run it then files left to do
 # it shuts it down if all files have been completed before amount
 # the user told it to do
 #####
 if [ -z "$ext1" ]; then 
 echo "all Done - dar eay."
 exit 1
 fi
 

 #strip the orginal file name off the path from FILENAME

 c=$FILENAME
 xpath=${c/*} 
 xbase=${c##*/}
 xfext=${xbase##*.}
 xpref=${xbase.*}
 path=${xpath}
 filenamepref=${xpref}
 ext=${xfext}

 
 
## first if ending fi just before done statment at EOF 
## only lets in all other then move files in to check then delete them 
 for file in "${path1}" ; do 
  #echo "inside of second inner loop ext is "$ext"" 
 if [[ "$ext" == "txt" || "$ext" == "info" || "$ext" == "srt" || "$ext" == "html" || "$ext" == "nfo"  ]] ; then
  #echo "this is ext -> "$ext""
  removeme="$FILENAME"
  rm -v "$removeme"
  # set a different ext type so that it will not go into following if statement due to it is still a movie extention
  # causes it to skip over and go to next file
  ext="foobar"

  let q++
  fi
  
done

 
 ## get rid of crap sample and shit Movies files
 
 string="$filenamepref"
 substring1="sample"
 substring2="Sample"
 substring3="ETRG"
 
 ## returns amount of chars in file name string
 ckfilesize=${#filenamepref} 
  
 ## had some movie File names with matching substring this takes care of it
 # so it no longer chops up Moive I need to process 
 if [[ "$ckfilesize" -gt "4" ]] ; then
      filenamepref=${filenamepref'ETRG'*}
      string="$filenamepref"
 fi
     
      echo


#Checks each movie file to see if it is just a not needed sample of the move to regain file space by deleting it
for file in "${path1}" ; do 

if [[ "$string" == *"$substring1"* || "$string" == *"$substring2"* || "$string" == *"$substring3"* ]]; then
	removeme="$FILENAME"
	rm -v "$removeme"
	# set a different ext type so that it will not go into following if statement due to it is still a movie extention
	# causes it to skip over and go to next file
    ext1="foobar"
    fi
done
 
#only let in the Movie Files if substring is not a match
 if [[ "${ext1}" == 'avi' || "${ext1}" == 'mp4'  || "${ext1}" == 'm4v' || "${ext1}" == 'mkv' || "${ext1}" == 'flv' || "${ext1}" == 'MP4' ]] ; then 
	
 ####################################
   # GET THE SIZE OF THE FILE to be put on Directory name later 
  MovieSize="`exiftool '-File Size'  "$FILENAME" -p '$FileSize'`"

 ## First Remove all of the Periods between the names in string leaving spaces      
 filenamepref=${filenamepref//./ }
# remove all trailing common garbage on tail end (RS) of file name
 filenamepref=${filenamepref%'1080p'*}
 filenamepref=${filenamepref%'HDTV'*}
 filenamepref=${filenamepref%'TS'*}
 filenamepref=${filenamepref%'FAST'*}
 filenamepref=${filenamepref%'Adesso'*}
 filenamepref=${filenamepref%'By'*}
 filenamepref=${filenamepref%'by'*}
 filenamepref=${filenamepref%'SWE'*}
 filenamepref=${filenamepref%'PROP'*}
 filenamepref=${filenamepref%'HDRi'*}
 filenamepref=${filenamepref%'720'*}
 filenamepref=${filenamepref%'BrR'*}
 filenamepref=${filenamepref%'Blu'*}
 filenamepref=${filenamepref%'HDCAM'*}
 filenamepref=${filenamepref%'DvD'*}
 filenamepref=${filenamepref%'[DVD'*}
 filenamepref=${filenamepref%'DVD'*}
 filenamepref=${filenamepref%'BR'*}
 filenamepref=${filenamepref%VHS*}
 filenamepref=${filenamepref%'[X'*}
 filenamepref=${filenamepref%'R5'*}
 filenamepref=${filenamepref%'480'*}
 filenamepref=${filenamepref%'VOS'*}
 filenamepref=${filenamepref%'hdtv'*}
 filenamepref=${filenamepref%'TELESYNC'*}
 filenamepref=${filenamepref%'bd'*}
 filenamepref=${filenamepref%'BD'*}
 filenamepref=${filenamepref%'['*}
 
 #Removes all leading white space on both ends of string
  filenamepref="$(echo -e "${filenamepref}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  
 #puts the new file name back together with its extention - ie. fileName.mp4 - 
 newFile=${filenamepref}.${ext1}

 #change the old move file name to the new one in the same old directory it is still in  
  mv "$FILENAME" "$path1"/"$newFile"
  
 
  ######
  ## Strip all of the extra crap off the ending sting of the Directory name
  ## leving just movie name and wanted info
  #####
 b=$FILENAME
 xpath=${b%/*} 
 xbase=${b##*/}
 xfext=${xbase##*.}
 xpref=${xbase%.*}
 pathto=${xpath}
 filename=${xpref}
 ext=${xfext}
  
 #gets just the Directory name that the moive is in
 directoryName=${pathto##*/}
 #Removes any periods that where used within the file name instead of spaces or
 # other means of doing so to not have spaces between the words in the string
 directoryName=${directoryName//./ }
 #now remove all the other common stull put onto the (RS) tail end of the directory name
 directoryName=${directoryName%'1080p'*}
 directoryName=${directoryName%'HDTV'*}
 directoryName=${directoryName%'TS'*}
 directoryName=${directoryName%'FAST'*}
 directoryName=${directoryName%'Adesso'*}
 directoryName=${directoryName%'By'*}
 directoryName=${directoryName%'by'*}
 directoryName=${directoryName%'SWE'*}
 directoryName=${directoryName%'PROP'*}
 directoryName=${directoryName%'HDRi'*}
 directoryName=${directoryName%'720'*}
 directoryName=${directoryName%'BrR'*}
 directoryName=${directoryName%'Blu'*}
 directoryName=${directoryName%'HDCAM'*}
 directoryName=${directoryName%'Dv'*}
 directoryName=${directoryName%'[Dv'*}
 directoryName=${directoryName%'(DV'*}
 directoryName=${directoryName%'DV'*}
 directoryName=${directoryName%'(Dv'*}
 directoryName=${directoryName%'BR'*}
 directoryName=${directoryName%VHS*}
 directoryName=${directoryName%'[X'*}
 directoryName=${directoryName%'R5'*}
 directoryName=${directoryName%'480'*}
 directoryName=${directoryName%'VOS'*}
 directoryName=${directoryName%'TELESYNC'*}
 directoryName=${directoryName%'bd'*}
 directoryName=${directoryName%'BD'*}
 directoryName=${directoryName#*'] '}
 
 #Remove leading and trailing white space on both ends of string
 
 directoryName="$(echo -e "${directoryName}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"

 # just echo's the length of the string to command line  
 # not used 
 # echo -e "length(directoryName)==$(echo -ne "${directoryName}" | wc -m)"


 #if single movie not within its own directory but in the root working dir it gets the size of the movie added to its
 # directory name by using its own file name then size of Move in MB
  newMovieDir=""$filenamepref" - "$MovieSize""
 
###########
# Create Directories and move the file 
##################################

  ################################
  # in the proccess in figuring out how to check a directory with just on needed movie already
  # in it so I can get it to attach the amount MB to the directory too
  #
  ##############
  
  ## using TREE gets the amount of files within the Directory after all of the not needed files
  # have been remove leaving only the needed movie file itself 
  #
  # Problem with a directory that has more then one needed movie in it
  # IE. tv series episodes has more then one movie just a Reg movie only has one
  # moive within its own directory at times
  # wanting to add the MB amount to that Directory too for quick reference
  # to later resample the bigger files down to a smaller size   
  # 
  #
  #
  #######
  #
  # Probles is so far it resets the count on files in directory too soon for some
  # due to loopyness
  #
  # Set checkmeonce to zero - tells if there is only one file in directory 
  # this should be the one that is Just a movie that already has its own directory
  # therefore now needs to have MB size of that move added to the directory name
  # before adding that movie file
  #
  # Problem is that while it is doing all of them all at once, both Just a movie
  # and tv series with multi episodes files -- when it get to the last file while looping through
  # tv series w/episodes count goes to one OR zero giving a false reading that I am looking for
  # to show only orginal one Move with its own directory that needs to have MB added to it
  #
  ###########
  #
  #  Checks for amount looking for a zero, and seeing if it is zero count to indecate one file
  # in directory
  ##
  # now need to figure out if it is not a tv series episodes directory to insure that I have the right one
  # in order to modify that by adding the MovieSize to the ending of the string before creating the new
  # directory
  #
  # cannot figure out how to insure that it is NOT a tv series episodes Directory without ME looking at it first,
  # because due to the loop it resets the checkmeonce reguardless if it is in a tv series episodes Directory or
  # the one I want , a Movie File Directory -- 
  # need to figure out how to compair the directory to tell it it is now a tv series episodes Directory then 
  # modify it create dir then move that movie into it then onto the next file
  #####################3
  
    AmountFiles="$(tree "${path1}" | awk 'END{print}')}"
    AmountFiles=${AmountFiles#*,}
    AmountFiles=${AmountFiles%f*}
  #  echo "amount file in dir "$AmountFiles""
     AmountFiles="$(echo -e "${AmountFiles}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
    
    
    
    
    
    if [[ "$AmountFiles" == "1" && "$checkmeonce" == "0" && "$directoryName" =  "${directoryName}" ]] ; then 
       echo
       echo " in iffy fuck this "$AmountFiles""
             onFileInDir="$newMovieDir"
             echo "this is it -> "$newMovieDir""
       echo
       else
       echo
       echo "in iffy mount file is -> "$AmountFiles""
       let checkmeonce++
       if [[ "$AmountFiles" == "1" ]] ; then
          checkmeonce=0
          echo "reset CheckMeOnce"
          fi
       echo
       fi
       
 ###
 ### Here lays where all of it gets put together for final use
 # checks to insure the directories are created first, if not then does so
 # according to where user tells it to put the root dir first
 # then subDirectories are then created according to file name
 # that's been stripped off of orginal directory
 # if the move did not have one then it uses its own file name to 
 # create the directory with the size of the move already added to it
 # then moves the Moive into the new directory
 ##################################
    
	if [[ ! -d "$move_files_to" ]] ;then 
			echo "creating dir "$move_files_to"" 
			echo
			mkdir "$move_files_to"/
	fi
	
     # for multi-files in one directory
     if [[ "$directoryName" !=  "${working_dir##*/}" && ! -d "$move_files_to"/"${directoryName}" ]]; then 
			mkdir "$move_files_to"/"${directoryName}"/
			mv -vf "${path1}"/"$newFile" "$move_files_to"/"${directoryName}"
	      
	elif [[ "$directoryName" !=  "${working_dir##*/}" &&  -d "$move_files_to"/"${directoryName}" ]]; then 
			mv -vf "${path1}"/"$newFile" "$move_files_to"/"${directoryName}"
	        
       # for files that have not their own directory NewMovieDir as MB added
       elif [[ "$directoryName" =  "${working_dir##*/}" && ! -d "$move_files_to"/"${newMovieDir}" || "$onFileInDir" = "$newMovieDir" && "$checkmeonce" == "0" ]]; then
			mkdir "$move_files_to"/"${newMovieDir}"/
 		    mv -vf "${path1}"/"$newFile" "$move_files_to"/"${newMovieDir}"
	fi
	    #rm -R  "$path1"
	       # for user info to tell him or her how many files left total
	       # and amount left to do per run
	        let Left2Do--
	        let totalMovieFilesLeft--
	        
	        
	#####################
	#
	# TO DO -- figure out when to delete old (Hopefully empty) directories 
	# before scrpit stops 
	#
	#
	#

############
#
#Used for MAXNUM Loop control
#############
let xf++
let mf--	

# User info output to terminal
echo ; echo ; echo "$mf ..... files left to convert this run ..."
echo ; echo        ""$MAXNUM"  Start count of files in "$working_dir""
echo ; echo        ""$Left2Do"  Files left to do in "$working_dir""
echo ; echo " ..>>> "$totalMovieFilesLeft" actual total Movie Files Left to do in Directories Dude! <<<.."
echo;echo

# fi # end of second if

fi # end of first checking for needed extentions only if statment

  
 
#EOF	
done

Last edited by BW-userx; 11-18-2015 at 03:12 PM.
 
Old 11-17-2015, 06:36 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
TBH I'm not entirely across your qn there, but it seems to me that in re 'S04E05' etc, if you can predict in advance what those values should be (more accurately the range), then you could generate them into a file, one per line, then read it into an array to do the comparisons/assignments with.
http://wiki.bash-hackers.org/syntax/arrays

For the SvarEvar approach, look into bash regexes http://www.tldp.org/LDP/abs/html/regexp.html, http://www.itworld.com/article/26933...pressions.html or maybe use eg sed.

Hope that gives you a couple of ideas to look into.
 
Old 11-18-2015, 01:16 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I have 2 issues:

1. I am with above and not really sure I follow the issues

2. It is a little unrealistic (IMO) to show a script with so much old chaff that it is difficult to follow what relates to the new requirements of the new script

For number 2 it would be simpler for the onlookers to not see all the references to the old script as it is distracting and makes it almost impossible to follow which parts I should be looking at.
Could you maybe redact all the superfluous parts so we may assist better? This may also help with number 1 in that even if your description may be hard to follow your code may give more insight
 
Old 11-18-2015, 07:37 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
As far as understanding the filenames to be handled, is it sufficient to not care about the details of that middle term and instead just understand that files can be in the format:
Code:
filename.extra-term.extension
And then will all files be in that format, or some yes, some no? Either case, I'm thinking that there's some form of regular expression you could derive which would give you the entire string, leading up to the file extension. Such as starting backwards from the end of the entire name, retaining the extension part, because that's the part found after the final period character, and then assuming that all to the left of that, then is the filename desired? Or am I missing something?

I do agree that given the enormity of the former work, and that there are 2 or 3 issues you wish to work out, that you ought to address them one at a time.
 
Old 11-18-2015, 08:02 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Following up on the thoughts about using a regex, I found this solution here

My test script:
Code:
#!/bin/sh

fullname=$1
extension="${fullname##*.}"
filename="${fullname%.*}"

echo "Fullname:  $fullname"
echo "Extension: $extension"
echo "Filename:  $filename"
Test output:
Code:
$ ./filex.sh new.tar.gz
Fullname:  new.tar.gz
Extension: gz
Filename:  new.tar
$ ./filex.sh this-is-my-file.ABCDEF.mp3
Fullname:  this-is-my-file.ABCDEF.mp3
Extension: mp3
Filename:  this-is-my-file.ABCDEF
 
Old 11-18-2015, 08:36 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I would guess the file names are more like:
Code:
NCIS.Los.Angeles.S07E01.720p.HDTV.X264-DIMENSION.mkv
Hence quite a bit more complicated than we are lead to believe
 
Old 11-18-2015, 02:21 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
I would guess the file names are more like:
Code:
NCIS.Los.Angeles.S07E01.720p.HDTV.X264-DIMENSION.mkv
Hence quite a bit more complicated than we are lead to believe
first off Just cuz - if you where to copy and paste that script into a new file place it in a seperate dir then set up one more dir to work out of then copy in a seperate movie with its own dir then just movie inin the working root dir then copy a tv series dir with some episodes within that dir too,


then goto the command line after you place in the area's up top where it tells you to put where what dir path the script is in and what dir path the files you what to process are at then where what path and dir you want the new one put first then run the script off the command line by cd to the dir the script is in then add the amount of files you what to process for that run off the command line $./script-name Number-of-files-to-process - ./modme 40
then let it run take a look at the output you'd get a better idea of what I am talking about.

how it creates the new directories some with the MB added to it and SOME without it, I'd like all of them two types to have MB added to it, and the TV one it creates a new dir and puts all of the same tv shows within that dir leaving the MB off of that dir that part is ok

then delete all of the old dir after all of the files have been completed not while it is working on the TV dir that has more then one move within it before it gets done with it. losing the rest of the movies before they get modified and moved into the new dir


(you must like that show huh? )

Yeah then it hit me last night when I got away from it and put it to more thought in bed
Code:
 ## First Remove all of the Periods between the names in string leaving spaces      
 filenamepref=${filenamepref//./ }
now I do not have to worry about stripping off any period at the end for it to take away that S07E01 because I was leaving it like this before hand
Code:
NCIS.Los.Angeles.S07E01.
with that period at the end - now it leaves it like this instead
Code:
 NCIS Los Angeles S07E01
so when I put it back together it don't look like this
Code:
NCIS.Los.Angeles.S07E01..mkv
but like this
Code:
 NCIS.Los.Angeles.S07E01.mkv
removing that extra period between the name and extention .. and taking it off the directory name too

now I am woking on other problems to find solutions to them with this just trying to figure out how to put it in "understandable english question language" first before I try to ask it in here, and i even speak english too lOl go figure.


(I cleaned up my code in the script and put in the problems within the coments at the areas I am working on for now within this post)

Last edited by BW-userx; 11-18-2015 at 03:13 PM.
 
Old 11-19-2015, 12:33 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I was looking through your solution to clean up filenamepref, that list will grow to be unusable and unmaintainable, an obvious first one is if you have 'dvd' and not 'DVD', your code
will remove one but not the other. There are other pieces in my example that also would not be removed.

Maybe you should look at what you do know would be in all file names (for me it is the season and episode information) and build a cleanup routine around that??
As an example, for my shows I would use something like:
Code:
f=NCIS.Los.Angeles.S07E01.720p.HDTV.X264-DIMENSION.mkv
ext=${f##*.}

regex='(.*)\.([Ss]([0-9]+)[Ee]([0-9]+))'

[[ "$f" =~ $regex ]]

echo "Original file :- $f"
echo "Name :- ${BASH_REMATCH[1]}"
echo "Season :- ${BASH_REMATCH[3]}"
echo "Episode :- ${BASH_REMATCH[4]}"
echo "Extension :- $ext"
Obviously you would assign the array (BASH_REMATCH) data to more usable names

Hopefully that might help.
 
Old 11-19-2015, 06:37 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
So I was looking through your solution to clean up filenamepref, that list will grow to be unusable and unmaintainable, an obvious first one is if you have 'dvd' and not 'DVD', your code
will remove one but not the other. There are other pieces in my example that also would not be removed.

Maybe you should look at what you do know would be in all file names (for me it is the season and episode information) and build a cleanup routine around that??
As an example, for my shows I would use something like:
Code:
f=NCIS.Los.Angeles.S07E01.720p.HDTV.X264-DIMENSION.mkv
ext=${f##*.}

regex='(.*)\.([Ss]([0-9]+)[Ee]([0-9]+))'

[[ "$f" =~ $regex ]]

echo "Original file :- $f"
echo "Name :- ${BASH_REMATCH[1]}"
echo "Season :- ${BASH_REMATCH[3]}"
echo "Episode :- ${BASH_REMATCH[4]}"
echo "Extension :- $ext"
Obviously you would assign the array (BASH_REMATCH) data to more usable names

Hopefully that might help.
Thanks -- thats sed - got a learn that now? hehe - cuz yeah I got a big list of what to remove now written for each case DVD - DvD dvd ext...
I'm working on my mp3 scprit right this minute cleaning it up and adding what I've learned off the Movie sctipt to it in applicable places so that it fixes the directory names mostly people cannot get there Caps and spaces right got a few same artist in muliti directories for same band etc.

I'll run that code you wrote and try to work with it after this music script is to where I got to get it -- its almost perfect --
thanks again

Last edited by BW-userx; 11-19-2015 at 06:38 AM.
 
Old 11-19-2015, 08:10 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just a by the way, that is not sed, it is bash's version of using regexes
 
Old 11-19-2015, 09:38 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
Just a by the way, that is not sed, it is bash's version of using regexes
thanks for clarifying that -- saves time trying to look that up under sed --

quick question if I may -

on mp3 META Tags clean up in lu of posting another question

this is term output by echo' ing it using exiftool
Code:
 ARTIST1="`exiftool -Artist "$FILENAME" -p '$Artist'`"
 TITLE1="`exiftool -Title  "$FILENAME" -p '$Title'`"
 ALBUM1="`exiftool -Album  "$FILENAME" -p '$Album'`"

first stript... Artist is -> The Stranglers and Friends
first stript... Album is -> Live in
first stript... Title is -> The Raven – With Basil Gabbi
what would be a good script way of looping through that Dir will all of them (music) files and stripping out that middle part so I can put the left overs in a var and replace it will cleaned up string that should go from this
Code:
The Raven – With Basil Gabbi
to this
Code:
The Raven With Basil Gabbi
what is that, just me not having the proper fonts for read it?

thanks hope I am not taxing your brain too much
 
Old 11-19-2015, 11:00 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So first point, use $() over `` as it is not only clearer but should you need to nest them you can without escaping the hell out of it.

I am guessing the title has another language embedded in it. So from that point I am not sure how you would get rid of it?? Interestingly bash can remove some of it but not all so
I am not sure how it is registering the text. Hopefully someone else will jump on and help.
 
Old 11-19-2015, 11:10 AM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
So first point, use $() over `` as it is not only clearer but should you need to nest them you can without escaping the hell out of it.

I am guessing the title has another language embedded in it. So from that point I am not sure how you would get rid of it?? Interestingly bash can remove some of it but not all so
I am not sure how it is registering the text. Hopefully someone else will jump on and help.
maybe something that would chomp along the string and check for normal chars US.UTF-8 or english reg charsor what ever its called and then chomp out what is not a a normal english char leaving what is alone that might leave it at what looks like just getting rid of the crap not haing to do much escaping over it --- but that is just logic talking without knowleadge of how to do that
 
Old 11-19-2015, 12:14 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well interestingly bash seemed to see some of the unknown text as normal???
This was my test:
Code:
$ x='The Raven  With Basil Gabbi'
$ echo ${x//[^A-Za-z ]/}
The Raven  With Basil Gabbi
As you can see it removed some but seems to think what is left is somehow in the normal alphabet range?? I am not sure bash has the ability to use unicode settings so that would then move up to something
like Perl / Ruby / Python. As I said, someone else might have an idea
 
Old 11-19-2015, 12:37 PM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
Well interestingly bash seemed to see some of the unknown text as normal???
This was my test:
Code:
$ x='The Raven  With Basil Gabbi'
$ echo ${x//[^A-Za-z ]/}
The Raven  With Basil Gabbi
As you can see it removed some but seems to think what is left is somehow in the normal alphabet range?? I am not sure bash has the ability to use unicode settings so that would then move up to something
like Perl / Ruby / Python. As I said, someone else might have an idea
I just ran that in term
Code:
[userx@voided ~]$ x="The Raven – With Basil Gabbi"
[userx@voided ~]$ echo ${x//[^A-Za-z ]/}
The Raven With Basil Gabbi
looks like it worked to me.. see you do know something more then you thought
you seem to have that fancy [^A-Za-z ]/ stuff down I've not had to work with it yet so I've not looked deeply into it
thanks
 
  


Reply

Tags
bash scripting, directories, files, string



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copying files from home dir to another dir from another dir in a lower dir chomito44 Linux - General 5 10-19-2013 06:18 PM
Move files contained in source dir to destination dir, but not source dir itself unixunderground Linux - Software 3 09-20-2013 11:17 AM
tar dir and sub dir removing files but not existing not empty dir j-me Linux - General 2 08-12-2013 11:37 AM
Command to display /dir, /dir/sub, /dir/sub/files knockout_artist Linux - Newbie 9 10-25-2007 02:57 PM
Bash Script test for empty dir uopjohnson Linux - Software 3 10-07-2005 06:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration