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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
07-02-2017, 02:33 AM
#1
LQ Newbie
Registered: Jul 2017
Posts: 3
Rep:
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
07-02-2017, 11:03 AM
#2
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842
something like this: (?)
find somewhere -name \*.jpg -exec mv {} target_dir \;
07-02-2017, 11:22 AM
#3
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep:
07-02-2017, 03:56 PM
#4
LQ Newbie
Registered: Jul 2017
Posts: 3
Original Poster
Rep:
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 ?
07-02-2017, 04:18 PM
#5
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep:
Quote:
Originally Posted by
ankitarao
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.
07-02-2017, 11:36 PM
#6
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
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.
07-03-2017, 02:26 AM
#7
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842
Quote:
Originally Posted by
ankitarao
Does this also find files in sub directories ?
see man find, look for description
07-03-2017, 05:20 AM
#8
Member
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508
Rep:
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 .
07-03-2017, 10:31 AM
#9
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
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 .
All times are GMT -5. The time now is 10:02 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News