Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: File Command???

  1. #11
    Join Date
    Apr 2012
    Beans
    7,256

    Re: File Command???

    Hmm OK - Vaphell's solutions for things like this are usually more robust though (I often miss the subtle things that can go wrong...)

  2. #12
    Join Date
    Jul 2009
    Location
    Netherlands
    Beans
    136
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: File Command???

    Quote Originally Posted by steeldriver View Post
    Hmm OK - Vaphell's solutions for things like this are usually more robust though (I often miss the subtle things that can go wrong...)
    So which option you think is better and why please?

  3. #13
    Join Date
    Apr 2012
    Beans
    7,256

    Re: File Command???

    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

  4. #14
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: File Command???

    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

  5. #15
    Join Date
    Jul 2009
    Location
    Netherlands
    Beans
    136
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: File Command???

    Quote Originally Posted by steeldriver View Post
    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
    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.

  6. #16
    Join Date
    Jul 2009
    Location
    Netherlands
    Beans
    136
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: File Command???

    Quote Originally Posted by Vaphell View Post
    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.
    I think your code is great i'm playing with it right now lol i think it will be perfect in my new harddrive, i just one more question will it deal with spaces in file names too???
    Thank you very much

  7. #17
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: File Command???

    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

  8. #18
    Join Date
    Jul 2009
    Location
    Netherlands
    Beans
    136
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: File Command???

    Quote Originally Posted by Vaphell View Post
    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)
    Thank you very much Vaphell that was really helpful.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •