Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 28

Thread: Script for finding string in filename then copy file

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

    Re: Script for finding string in filename then copy file

    my understanding is that + respects the limits enforced by the system and splits the bulk command if necessary.

    In some cases, eg mv oldname newname + wouldn't even work, so i guess the rule of thumb would be "use + if your find supports it, unless it doesn't make sense". And if i have to do something remotely complicated on a per file basis i prefer to roll my own while read loop fed with find -print0 and not be restricted by a huge oneliner.
    Last edited by Vaphell; December 30th, 2014 at 01:06 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

  2. #12
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Script for finding string in filename then copy file

    Yup, this is my understanding as well. Citing the find manual:
    This expansion is done in such a way as to avoid exceeding the maximum command line length available on the system.
    Different approaches are discussed at some length in the manual cited above (See info -f find -n 'Deleting Files').
    Last edited by schragge; December 30th, 2014 at 01:55 AM.

  3. #13
    Join Date
    Feb 2009
    Location
    Arcadia, CA USA
    Beans
    422
    Distro
    Ubuntu 17.04 Zesty Zapus

    Re: Script for finding string in filename then copy file

    Well thanks. I went to "info -f coreutils . . ." and, well, got smothered. I am going to settle for Vaphells explanation for now.

    1)Now I have grown the code a bit to implement as a script file, but have trouble inputing the variable "string" (= a person's name). It would seem the problem is related to the placement of "$" which I have placed before the first " ' ", and between " ' ", and " * "; neither position works. Any suggestions regarding the syntax needed would be appreciated? (Inputing "source" and "catch" works just fine)

    2)Also, the variables source and string are unhappy unless I input the explicit paths, eg /home/rocky/Desktop vs ~/Desktop. I can easily live with that small discomfort - but maybe there is a way?

    Code:
    #!/bin/bash
    
    read -p "Please enter full path to directory of image folders  : " source
    echo ""
    read -p "Please enter destination folder for copied image files  : " catch
    echo ""
    read -p "Please enter string to search for within file names  : " string
    
    
    #SAVE LINE BELOW AS WORKING LINE
    #find /home/rocky/Desktop/ -type f -name '*Dulcey*' -exec cp -t /home/rocky/Desktop/catch/ - {} +
    
    #following line works - except for trying to introduce the variable for string
    find $source -type f -name '$*string*' -exec cp -t $catch - {} +
    Thanks
    mark

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

    Re: Script for finding string in filename then copy file

    quote your "$variables" ($source $catch) or any space inside will make it go boom due to word splitting

    "*$string*". ' ' are literal quotes, so even if you didn't put $ in the wrong place, it wouldn't work. Also i suggest using -iname, which is case insensitive, instead of case sensitive -name

    -- not -

    ~: you might use something like ${source/#~/$HOME}
    Last edited by Vaphell; December 30th, 2014 at 05:13 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
    Apr 2012
    Beans
    7,257

    Re: Script for finding string in filename then copy file

    The single-quotes around $*string* prevent it from being expanded by the shell - you need to change those to double-quotes, and in any case it should be *$string*. You should also double-quote your other variables. The `-` should be two `--`

    Code:
    find "$source" -type f -name "*$string*" -exec cp -t "$catch" -- {} +
    EDIT: oops beaten by Vaphell!

  6. #16
    Join Date
    Feb 2009
    Location
    Arcadia, CA USA
    Beans
    422
    Distro
    Ubuntu 17.04 Zesty Zapus

    Re: Script for finding string in filename then copy file

    Thank you both!

    The "*$string*" syntax works just great, and I corrected the "-" to what it was supposed to be, "--".

    Sorry, and again a very,very minor issue, but I did not understand how to shorten the path, eg, /home/rocky/Desktop/catch to ~/Desktop/catch?

    Anyhow, you guys have been a big help and I have "piece of mind".

    mark

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

    Re: Script for finding string in filename then copy file

    instead of "$source" use "${source/#~\//$HOME/}". It will translate ~/some/dir to /home/you/some/dir so you don't have to type full path.
    Last edited by Vaphell; December 30th, 2014 at 06:13 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

  8. #18
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Script for finding string in filename then copy file

    Quote Originally Posted by schragge View Post
    Yup, this is my understanding as well. Citing the find manual:

    Different approaches are discussed at some length in the manual cited above (See info -f find -n 'Deleting Files').
    Definitely sheds some light on the problem. Thx.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  9. #19
    Join Date
    Feb 2009
    Location
    Arcadia, CA USA
    Beans
    422
    Distro
    Ubuntu 17.04 Zesty Zapus

    Re: Script for finding string in filename then copy file

    Well, I couldn't get the abbreviated path ($source" use "${source/#~\//$HOME/}" to work? My understanding is that it is intended to "take care of ~/home" part of the path?

    So I tried entries of "rocky/Desktop" and "/rocky/Desktop" for "source" with msg "Bad substitution". Also tried ~/rocky/Desktop with msg "No such file or directory". Getting it right is probably not worth more effort since it is such a minor issue. At any rate, I am v. pleased with out the script executes.

    Thanks
    mark

  10. #20
    Join Date
    Apr 2012
    Beans
    7,257

    Re: Script for finding string in filename then copy file

    The ~ is a synonym for your home directory (usually that would correspond to the absolute path /home/rocky - assuming your username is rocky) so your Downloads directory would simply be

    Code:
    ~/Downloads
    Vaphell's substitution would convert that to /home/rocky/Downloads e.g.

    Code:
    $ bash -c 'read -p "Enter source directory: " source; echo "${source/#~\//$HOME/}"'
    Enter source directory: ~/Downloads
    /home/steeldriver/Downloads

Page 2 of 3 FirstFirst 123 LastLast

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
  •