Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Posts
    2,203

    copying certain files

    There is a directory with many files and subdirectories. I want to copy ALL the filesnames beginning with post-1111 to another folder. What is the command to do this?

  2. #2
    Join Date
    Jan 2003
    Location
    U.S.A.
    Posts
    3,928
    Not sure exactly what your trying to do but try something like the following.

    * = WildCard

    Command = cp *.extension newname

    **Take a Backup before attempting this method, as it will take a few tries to get it right.

  3. #3
    Join Date
    Jun 2008
    Location
    Los Angeles, CA
    Posts
    272
    to copy from current dir to /home/new (example) the command is:
    cp -rf post-1111*.* /home/new/

    of course replace /home/new/ with your target directory.
    █ Lebnene
    █ Consultant: Colocation, Cloud & Dedicated Servers

  4. #4
    Join Date
    Jan 2005
    Posts
    2,203
    How to make it copy files in subdirectories too? That command only copied files in /current/dir/ but not in /current/dir/morefiles/

    Thanks.

  5. #5
    Join Date
    Jun 2007
    Location
    Spain
    Posts
    37
    Quote Originally Posted by HD Fanatic View Post
    How to make it copy files in subdirectories too? That command only copied files in /current/dir/ but not in /current/dir/morefiles/
    Use find.

    Code:
    find /current/dir/ -iname post-1111* -exec cp {} /dir/new/ \;
    Regards.

  6. #6
    Join Date
    Jun 2007
    Location
    Spain
    Posts
    37
    Just an update,

    Above command won't preserve directory tree.

    I mean, if you have a file in /dir/1/post-1111test1 and another in /dir/2/post-1111test2 and copy them using find command to /dir2, you will get the following:

    /dir2/post-1111test1
    /dir2/post-1111test2

    If you want to preserve directories... I mean:

    /dir2/1/post-1111test1
    /dir2/2/post-1111test2

    Use the following command (you should install rsync in your server):

    Code:
    rsync -r --include='*/' --include='post-1111*' --exclude='*' /dir1/ /dir2/
    I hope this helps.

    Regards,
    sahsanu

Posting Permissions

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