Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Bash script for moving files

  1. #1
    Join Date
    Jul 2007
    Beans
    74

    Bash script for moving files

    I have a folder /var/www/img has lots of image files name by date.

    I want to move all images with 201108 filename to /root/a

    I tried this bash but without luck

    HTML Code:
    $mv $(find . -name "201108") /root/a/;
    Please could someone help?

  2. #2
    Join Date
    Oct 2005
    Location
    Al Ain
    Beans
    8,950

    Re: need help on a tiny bash script

    mv *201108* /wherever/.

  3. #3
    Join Date
    Oct 2009
    Location
    Reykjavík, Ísland
    Beans
    12,550
    Distro
    Lubuntu 17.10 Artful Aardvark

    Re: Bash script for moving files

    Moved to Programming Talk; title changed.
    If you install Buntu 17.10 remember to download a new ISO file.

    Old files might contain a bug which can damage UEFI hardware. Updating an existing installation and upgrading to 17.10 (if one has faith in upgrades in general) are safe.

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

    Re: Bash script for moving files

    Quote Originally Posted by huangweiqiu View Post
    I have a folder /var/www/img has lots of image files name by date.

    I want to move all images with 201108 filename to /root/a

    I tried this bash but without luck

    HTML Code:
    $mv $(find . -name "201108") /root/a/;
    Please could someone help?
    You can't use the output of find (which is multi-line) directly as command parameters. In the general case, when you have a multi-line list of things that you want to pass as multiple arguments to one single command, use "xargs". However, this is fraught with peril if you want to handle spaces in names etc... It's easier to ask find to execute a command on every file that matches:

    Code:
    find /var/www/img -name "201108" -exec mv {}  /root/a \+
    (the difference between \+or \; in the '-exec' part is that ";" has the command execute once for each file, while "+" creates a siongle command with all the files (which works very often, given Unix conventions)

  5. #5
    Join Date
    Jul 2007
    Beans
    74

    Re: Bash script for moving files

    Code:
    find /var/www/img -name "201108" -exec mv {}  /root/a \+

    ------------------
    I run this script but failed with error

    HTML Code:
    find: missing argument to `-exec'

  6. #6
    Join Date
    Jan 2010
    Location
    Sydney, Australia
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Bash script for moving files

    Quote Originally Posted by huangweiqiu View Post
    Code:
    find /var/www/img -name "201108" -exec mv {}  /root/a \+

    ------------------
    I run this script but failed with error

    HTML Code:
    find: missing argument to `-exec'
    Code:
    find /var/www/img -name "201108" -exec mv {}  /root/a/ \;
    should work.
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  7. #7
    Join Date
    Jul 2007
    Beans
    74

    Re: Bash script for moving files

    Quote Originally Posted by codemaniac View Post
    Code:
    find /var/www/img -name "201108" -exec mv {}  /root/a/ \;
    should work.
    the new one has no error, however no file was transfered to /root/a .Please help to check gain. almost done.

  8. #8
    Join Date
    Jan 2010
    Location
    Sydney, Australia
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Bash script for moving files

    Quote Originally Posted by huangweiqiu View Post
    the new one has no error, however no file was transfered to /root/a .Please help to check gain. almost done.
    Please make sure the file name is exactly 201108.
    Or if the filename contains 201108 as substring use *201108* .

    Code:
    find /var/www/img -name "*201108*" -exec mv {}  /root/a/ \;
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  9. #9
    Join Date
    Jul 2007
    Beans
    74

    Re: Bash script for moving files

    Quote Originally Posted by codemaniac View Post
    Please make sure the file name is exactly 201108.
    Or if the filename contains 201108 as substring use *201108* .

    Code:
    find /var/www/img -name "*201108*" -exec mv {}  /root/a/ \;
    works like a charm,thanks a lot!

  10. #10
    Join Date
    Oct 2009
    Location
    Reykjavík, Ísland
    Beans
    12,550
    Distro
    Lubuntu 17.10 Artful Aardvark

    Re: Bash script for moving files

    Good, please mark the thread 'solved'.
    If you install Buntu 17.10 remember to download a new ISO file.

    Old files might contain a bug which can damage UEFI hardware. Updating an existing installation and upgrading to 17.10 (if one has faith in upgrades in general) are safe.

Page 1 of 2 12 LastLast

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
  •