LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-02-2017, 02:33 AM   #1
ankitarao
LQ Newbie
 
Registered: Jul 2017
Posts: 3

Rep: Reputation: Disabled
Arrange files from USB to folders based on extension


Hi all,

I am using VIM and I need to create a bash script to extract all data from my USB and copy it to folders based on extensions
eg - Images like .jpg, .png should be copied to images folders
Documents like .docx, .pdf should be copied to Documents folder

I have this so far


#!/bin/bash
echo "Creating directory categories"

function make_folder
{
cd -; cd content; sudo mkdir ./$1
}

make_folder "documents"
make_folder "other"
make_folder "pictures"
make_folder "media"

echo "Directories have been made"; cd -
exit

ext="${filename##*.}" #set var ext to extension of files

find ./random -name | #find and list all files in random folder
#pipe results of find into if statement

if ext == ["jpg"; "jpeg"; "png"] #move ".jpg", etc to new destination
then
mv /path/to/source /path/to/destination

elif ext == [".gif"; ".mov"] #move ".gif", etc to new destination
then
mv /path/to/source /path/to/destination
else #move other files into to new destination
mv /path/to/source /path/to/destination
fi


need help not sure where i am wrong
 
Old 07-02-2017, 11:03 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
something like this: (?)
find somewhere -name \*.jpg -exec mv {} target_dir \;
 
Old 07-02-2017, 11:22 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
http://www.linuxquestions.org/questi...ectory-875668/ may offer some insight.
 
Old 07-02-2017, 03:56 PM   #4
ankitarao
LQ Newbie
 
Registered: Jul 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Yes,

Pardon me for foolish questions, I am new to scripting

find somewhere -name \*.jpg -exec mv {} target_dir \;

Does this also find files in sub directories ?
 
Old 07-02-2017, 04:18 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by ankitarao View Post
Yes,

Pardon me for foolish questions, I am new to scripting

find somewhere -name \*.jpg -exec mv {} target_dir \;

Does this also find files in sub directories ?
Directories are files, and I would utilize this additional switch
Code:
find source -iname '*.jpg' -type f -exec mv {} target \ ;
find as previously shown will find directories with .jpg in them, and I don't think you want that.

There are 100s of find tutorials on the net, and most, if not all, deal with this subject in one variation (extensions) or another.
 
Old 07-02-2017, 11:36 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

Thanks.
 
Old 07-03-2017, 02:26 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by ankitarao View Post
Does this also find files in sub directories ?
see man find, look for description
 
Old 07-03-2017, 05:20 AM   #8
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Actually I'm going to try using Grand Central on Macs and iPhones/iPads/iPods etc. next. That will fairly easily take advantage of multiple processors/cores/hyperthreading. I can actually post an iPhone app to calculate them on the app store if it works out (of course free, who'd pay for this? lol).

Last edited by Laserbeak; 07-03-2017 at 05:21 AM.
 
Old 07-03-2017, 10:31 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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
you can have free rights to modify this to your needs.
Code:
 #!/bin/bash

#June 22, 2017
 
#set -x

count=0 max=0

working_dir=/media/data/Music-B

copy_to1=/run/media/userx/3TB-External/Music
copy_to2=/run/media/userx/750hybrid/Music

#copy_to1=/media/data/Music-B
#copy_to2=/media/data/Music-B

move_to=/media/data/Music

move_old_flac=/run/media/userx/3TB-External/OldFlac

script_dir=/home/userx/scripts/production

GirlSubString="Reference Of Female"

Gal1SubString="Guardians Of The Galaxy Deluxe"

Gal2SubString="Guardians Of The Galaxy Awesome Mix Vol. 2"

Gal3SubString="Guardians Of The Galaxy Vol. 1"

SoundtrackSubString="soundtrack"

SubString="despicable"


max="$(find "$working_dir" -type f \( -iname "*.mp3" -o -name "*.MP3" -o -name "*.flac" -o -name "*.m4a" \) | wc -l )"

while read FILENAME
do

f=$FILENAME
path=${f%/*}
xfile=${f##*/}
title=${xfile%.*}
ext=${xfile##*.}

#ARTIST="`exiftool -Artist "$FILENAME" -p '$Artist'`"
#ALBUM="`exiftool  -Album  "$FILENAME" -p '$Album'`"
#TITLE="`exiftool  -Title  "$FILENAME" -p '$Title'`"
#GENRE="`exiftool  -Genre  "$FILENAME" -p '$Genre'`"
#TRACK="`exiftool -Track "$FILENAME" -p '$Track'`"
#BITRATE="`exiftool -LameBitrate "$FILENAME" -p '$LameBitrate'`"


ARTIST=$(exiftool -p '$Artist' "$FILENAME")
ALBUM=$(exiftool -p  '$Album' "$FILENAME")
TITLE=$(exiftool -p '$Title'  "$FILENAME")
GENRE=$(exiftool -p '$Genre' "$FILENAME")
TRACK=$(exiftool -p '$Track' "$FILENAME")
BITRATE=$(exiftool -p '$LameBitrate' "$FILENAME")



BITRATE=${BITRATE% *}

title=${title/&/ and /}
TITLE=${TITLE/&/ and /}

TITLE="${TITLE//[^A-Za-z0-9-"'" ]/ }"
title=${title//[^A-Za-z0-9-"'" ]/ }
title=$(echo -e "$title" | fmt -u)
title="$(echo $title | sed -e 's/^[ \t]*//' | sed 's/.*/\L&/; s/[a-z]*/\u&/g' )"

ARTIST=$(echo $ARTIST | sed -e 's/^[ \t]*//' | sed -e "s/\b\(.\)/\u\1/g")
ALBUM=$(echo $ALBUM | sed -e 's/^[ \t]*//' | sed -e "s/\b\(.\)/\u\1/g")
TITLE=$(echo $TITLE | sed -e 's/^[ \t]*//' | sed -e "s/\b\(.\)/\u\1/g")
GENRE=$(echo $GENRE | sed -e 's/^[ \t]*//' | sed -e "s/\b\(.\)/\u\1/g")


reSampleFlac(){
NewFile1="$title.mp3"
#to Retag new MP3
redirectNewFile="$script_dir/$NewFile1"

echo "in start reSampleFlac"
echo "$FILENAME"

flac -cd "$FILENAME" | lame -b 128 - "$NewFile1" 
#flac -cd "$FILENAME" | lame -V2 -b128 -B160 -F -T --vbr-new -m f -q2 --resample 22.05  - "$NewFile1"
id3v2 -A "$ALBUM" "$redirectNewFile"
id3v2 -a "$ARTIST" "$redirectNewFile"
id3v2 -t "$TITLE" "$redirectNewFile"
id3v2 -g "$GENRE" "$redirectNewFile"
id3v2 -T "$TRACK" "$redirectNewFile"

#put new mp3 back
mv -v "$redirectNewFile" "$path"
#move flac to save place
OldFlac2Dir=${path/$working_dir/$move_old_flac}
mkdir -p "$OldFlac2Dir"
mv -v "$FILENAME" "$OldFlac2Dir"
#reassign NewFile1- mp3 and path to FILENAME
FILENAME="$path/$NewFile1"
echo "in reSampleFlac $FILENAME"
BITRATE="`exiftool -LameBitrate "$FILENAME" -p '$LameBitrate'`"
BITRATE=${BITRATE% *}
}


resampleMP3()
{
	NewFile="$TITLE-$ARTIST.mp3"
	echo "NewFile: $NewFile"
	
	retagdir="$script_dir/$NewFile"
	lame -V2 -b128 -B160 -F -T --vbr-new -m f -q2 --resample 22.05 "$FILENAME" "$NewFile"
	rm -v "$FILENAME"
	FILENAME="$retagdir"
	echo "IN resampleMP3 $FILENAME"
}

DealWithM4a() {

if [[ -n "$ARTIST" && -n "$ALBUM" ]] ; then
{
	copyIt1="$copy_to1/$ARTIST/$ALBUM"
	copyIt2="$copy_to2/$ARTIST/$ALBUM"
	moveIt="$move_to/$ARTIST/$ALBUM"
	mkdir -pv "$copyIt1"
	mkdir -pv "$copyIt2"
	mkdir -pv "$moveIt"
	cp -rv "$FILENAME" "$copyIt1"
	cp -rv "$FILENAME" "$copyIt2"
	#cp -v "$FILENAME" "$moveIt"
	mv -v "$FILENAME" "$moveIt"
}
elif [[ ! -n "$ALBUM" && -n "$ARTIST" ]] ; then
{
	copyIt1="$copy_to1/$ARTIST/$ARTIST"
	copyIt2="$copy_to2/$ARTIST/$ARTIST"
	moveIt="$move_to/$ARTIST/$ARTIST"
	mkdir -pv "$copyIt1"
	mkdir -pv "$copyIt2"
	mkdir -pv "$moveIt"
	cp -rv "$FILENAME" "$copyIt1"
	cp -rv "$FILENAME" "$copyIt2"
	#cp -v "$FILENAME" "$moveIt"
	mv -v "$FILENAME" "$moveIt"

}
fi
}

echo;echo

#fix mp3 if needed

# -f = fix -nb = remove temp files -si = surpress INFO output
[[ "$ext" == 'mp3' ]] && mp3val "$FILENAME" -f -nb -si

echo;echo

#Call functions

[[ "$ext" == 'flac' ]] && reSampleFlac

[[ "$BITRATE" -gt '128' ]] || [[ -z "$BITRATE" ]] && resampleMP3

[[ "$ext" == 'm4a' ]] && DealWithM4a

echo
echo "$FILENAME"
echo

#format strings

#lower case everything
NewFile="${TITLE,,}-${ARTIST,,}.mp3"
#one space between words
NewFile="$(echo "$NewFile" | fmt -u)"
#remove leading white space | cap each word
NewFile="$(echo $NewFile | sed -e 's/^[ \t]*//' | sed 's/.*/\L&/; s/[a-z]*/\u&/g' )"
#sed -e "s/\b\(.\)/\u\1/g")"

#do some work with finished product

if [[ -n "$ARTIST" && -n "$ALBUM" && -n "$TITLE" ]] ; then
{
  
	  
   #   id3v2 -D "$FILENAME"
   #   id3v2 -A "$ALBUM" "$FILENAME"
   #   id3v2 -a "$ARTIST" "$FILENAME"
   #   id3v2 -t "$TITLE" "$FILENAME"
   #   id3v2 -g "$GENRE" "$FILENAME"
   #   id3v2 -T "$TRACK" "$FILENAME"

    if [[ "$ALBUM" =~ "$GirlSubString" ]] ; then
      {
	echo  "HERE: $ALBUM"
	MainDir="Girl Punk Rockers"
	ALBUM=${ALBUM/[/ }
	ALBUM=${ALBUM/]/ }
	ALBUM=${ALBUM/_/ }
	ALBUM=$(echo "$ALBUM" | fmt -u)
	copyIt1="$copy_to1/$MainDir/$ALBUM"
	copyIt2="$copy_to2/$MainDir/$ALBUM"
	moveIt="$move_to/$MainDir/$ALBUM"
	mkdir -pv "$copyIt1"
	mkdir -pv "$copyIt2"
	mkdir -pv "$moveIt"
	cp -rv "$FILENAME" "$copyIt1/$NewFile"
	cp -rv "$FILENAME" "$copyIt2/$NewFile"
	#cp -v "$FILENAME" "$moveIt/$NewFile"
	mv -v "$FILENAME" "$moveIt/$NewFile"
      }
      elif [[ "${ALBUM,,}" =~ "$SubString" ]] ; then
      {
	copyIt1="$copy_to1/$ALBUM"
	copyIt2="$copy_to2/$ALBUM"
	moveIt="$move_to/$ALBUM"
	mkdir -pv "$copyIt1"
	mkdir -pv "$copyIt2"
	mkdir -pv "$moveIt"
	cp -rv "$FILENAME" "$copyIt1/$NewFile"
	cp -rv "$FILENAME" "$copyIt2/$NewFile"
	#cp -v "$FILENAME" "$moveIt/$NewFile"
	mv -v "$FILENAME" "$moveIt/$NewFile"
      }
      elif [[ "$ALBUM" =~ "$Gal1SubString" ]] ; then
      {
	copyIt1="$copy_to1/$ALBUM"
	copyIt2="$copy_to2/$ALBUM"
	moveIt="$move_to/$ALBUM"
	mkdir -pv "$copyIt1"
	mkdir -pv "$copyIt2"
	mkdir -pv "$moveIt"
	cp -rv "$FILENAME" "$copyIt1/$NewFile"
	cp -rv "$FILENAME" "$copyIt2/$NewFile"
	#cp -v "$FILENAME" "$moveIt/$NewFile"
	mv -v "$FILENAME" "$moveIt/$NewFile"
      }
      elif [[ "$ALBUM" =~ "$Gal2SubString" ]] ; then
      {
	copyIt1="$copy_to1/$ALBUM"
	copyIt2="$copy_to2/$ALBUM"
	moveIt="$move_to/$ALBUM"
	mkdir -pv "$copyIt1"
	mkdir -pv "$copyIt2"
	mkdir -pv "$moveIt"
	cp -rv "$FILENAME" "$copyIt1/$NewFile"
	cp -rv "$FILENAME" "$copyIt2/$NewFile"
	#cp -v "$FILENAME" "$moveIt/$NewFile"
	mv -v "$FILENAME" "$moveIt/$NewFile"
      }
      elif [[ "$ALBUM" =~ "$Gal3SubString" ]] ; then
      {
	copyIt1="$copy_to1/$ALBUM"
	copyIt2="$copy_to2/$ALBUM"
	moveIt="$move_to/$ALBUM"
	mkdir -pv "$copyIt1"
	mkdir -pv "$copyIt2"
	mkdir -pv "$moveIt"
	cp -rv "$FILENAME" "$copyIt1$NewFile"
	cp -rv "$FILENAME" "$copyIt2/$NewFile"
	#cp -v "$FILENAME" "$moveIt/$NewFile"
	mv -v "$FILENAME" "$moveIt/$NewFile"
      }
      elif [[ "$GENRE" =~ "$SoundtrackSubString" ]] ; then
      {
	copyIt1="$copy_to1/$ALBUM"
	copyIt2="$copy_to2/$ALBUM"
	moveIt="$move_to/$ALBUM"
	mkdir -pv "$copyIt1"
	mkdir -pv "$copyIt2"
	mkdir -pv "$moveIt"
	cp -rv "$FILENAME" "$copyIt1$NewFile"
	cp -rv "$FILENAME" "$copyIt2/$NewFile"
	#cp -v "$FILENAME" "$moveIt/$NewFile"
	mv -v "$FILENAME" "$moveIt/$NewFile"
      }      
     elif [[ -n "$ALBUM" && -n "$ARTIST" ]] ; then
     {
	copyIt1="$copy_to1/$ARTIST/$ALBUM"
	copyIt2="$copy_to2/$ARTIST/$ALBUM"
	moveIt="$move_to/$ARTIST/$ALBUM"
	mkdir -pv "$copyIt1"
	mkdir -pv "$copyIt2"
	mkdir -pv "$moveIt"
	cp -rv "$FILENAME" "$copyIt1/$NewFile"
	cp -rv "$FILENAME" "$copyIt2/$NewFile"
	#cp -v "$FILENAME" "$moveIt/$NewFile"
	mv -v "$FILENAME" "$moveIt/$NewFile"
     }
     elif [[ -n "$ARTIST" && ! -n "$ALBUM" ]] ; then
     {
	copyIt1="$copy_to1/$ARTIST"
	copyIt2="$copy_to2/$ARTIST"
	moveIt="$move_to/$ARTIST"
	mkdir -pv "$copyIt1"
	mkdir -pv "$copyIt2"
	mkdir -pv "$moveIt"
	cp -rv "$FILENAME" "$copyIt1/$NewFile"
	cp -rv "$FILENAME" "$copyIt2/$NewFile"
	#cp -v "$FILENAME" "$moveIt/$NewFile"
	mv -v "$FILENAME" "$moveIt/$NewFile"
     }
     fi     
}
fi
	echo "
			MAX    is $max
			count  is $((++count))
				--------
			left      $((max - count))
		"

done< <(find "$working_dir" -type f \( -iname "*.mp3" -o -name "*.MP3" -o -name "*.flac" -o -name "*.m4a"  \) )
if you look through it you will find everything you need is already there to get whatever file extensions you want to find and make a dir and then put it where ever you want to.

It is already basically written to do what you want to do..

all you will have to do is remove what is not needed then add what is already there in to the needed where ever it is needed to be to get it to do that in which you are wanting to do.

hence modifying it. cheers and have fun.

looking at this after posting it I can see I need to clean it up a bit for myself even .. thanks!

Last edited by BW-userx; 07-03-2017 at 12:03 PM.
 
  


Reply



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
[SOLVED] 14.1 0 folders, 0 files connecting usb camera, worked in 14.0 glorsplitz Slackware 7 11-21-2013 06:31 PM
script to created folders based on multiple input files prravin1 Programming 1 05-04-2012 06:42 AM
[SOLVED] rename and arrange files based on a list babak1112 Linux - General 2 01-29-2012 08:47 AM
bash script to delete files / folders based on date and freespace nekawa Linux - Newbie 5 06-08-2009 09:00 PM
Quarantine files based on their extension and location xbaez Programming 7 07-07-2005 12:36 PM

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

All times are GMT -5. The time now is 10:02 PM.

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