Hmm OK - Vaphell's solutions for things like this are usually more robust though (I often miss the subtle things that can go wrong...)
Hmm OK - Vaphell's solutions for things like this are usually more robust though (I often miss the subtle things that can go wrong...)
Vaphell's solution will likely be safer for filenames with 'special' characters in them and will handle large numbers of matches better (by buffering them through the 'read' function)... possibly some other things as well
I guess it depends a bit what you want to do when you find the files - the 'case... esac' structure gives you a bit more flexibility whereas if you just want to move all files of one type to the appropriate dir find / read / mv should do the job fine
depends on your needs
if you want to 'manually sort' (this type->that dir) case would be better
if you want full auto, my code would do that (it creates dest dirs for whatever mimetypes it meets)
usually without knowing exact conditions i try to write the broadest possible code so it can be easily tailored to specific needs.
while IFS= read -d $'\0' ... < <( find ... -print0 ) looks bulky (and it is) but it is a standard boilerplate code if you have to create a list of files that matches some set of criteria.
It is the most scalable solution of all, functionally it's a superset of standard for loop+globs, it's recursive by default (globs require enabled globstar for that), offers more flexibility in criteria and that null delimiter business means it's completely resistant to troublesome characters like whitespace (globs are resistant too). Null is forbidden by filesystems so it allows for 100% safe file lists (eg newline is legit in filenames so you can't depend on it). I agree that this approach often it is an overkill (like when you don't need recursion and standard globbing is enough) but it will always work and you can't go wrong with suggesting it without knowing specifics.
Simply put if i don't know that for f in * will be enough or what kind of weirdo names will be processed, i default to full blown while read.
Last edited by Vaphell; November 23rd, 2012 at 04:15 AM.
if your question is answered, mark the thread as [SOLVED]. Thx.
To post code or command output, use [code] tags.
Check your bash script here // BashFAQ // BashPitfalls
Well since i already have all the directories for all my files in a harddrive this script will look in my downloads and move all my video, audio, text, images to their respective directories i guess i will try both solutions and see which one works best thank you guys for everything.
yes, it deals with any legit character you can throw at it (unless i ommitted "" somewhere... always quote your variables to prevent whitespace disasters, unless you are 110% sure no surprise can happen)
if your question is answered, mark the thread as [SOLVED]. Thx.
To post code or command output, use [code] tags.
Check your bash script here // BashFAQ // BashPitfalls
Bookmarks