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

Thread: How to copy files of a particular extension RECURSIVELY?

  1. #1
    Join Date
    Aug 2009
    Beans
    4

    How to copy files of a particular extension RECURSIVELY?

    Dear all,

    i'm a new Ubuntu user, just recently experimented with making a file server using Samba and everything's going well...
    i wish to make periodical backups, but just i do not need to make a full backup, just of files of a certain name and extension, but i need it to be recursive and maintain folder trees...

    Example:
    /Directory-to-be-backed-up
    /Folder1
    junkfile1
    junkfile2
    backmeup.bak
    /Folder2
    junkfile3
    junkfile4
    backmeup.bak

    i want to just copy the backmeup.bak files and maintain the directories, ie. i want a command to copy the files to an empty USB-stick so that the outcome will be:

    /media/USB_stick
    /Folder1
    backmeup.bak
    /Folder2
    backmeup.bak

    In Windows, i usually just use the command:
    xcopy C:\Directory-to-be-backed-up\backmeup.*bak U:\ /s /e
    and it will create the folder1 and folder2 directories and copy just the backmeup.bak files (the * is necessary for the xcopy command in order to tell it to look for that criteria recursively)

    Can someone please help me out =)
    Will be most appreciated.... thanks so much in advance

    dk

  2. #2
    Join Date
    Aug 2009
    Beans
    4

    Re: How to copy files of a particular extension RECURSIVELY?

    Ugh the spaces didnt show well to illustrate the directory tree structure tried using Indent and it made it as one long train instead... what i meant was:

    /Directory-to-be-backed-up
    -->/Folder1
    ----->junkfile1
    ----->junkfile2
    ----->backmeup.bak
    -->/Folder2
    ----->junkfile3
    ----->junkfile4
    ----->backmeup.bak

    and i want the outcome to be:
    /media/USB_Stick
    -->/Folder1
    ----->backmeup.bak
    -->/Folder2
    ----->backmeup.bak

    Thanks =)

  3. #3
    Join Date
    Apr 2008
    Location
    LOCATION=/dev/random
    Beans
    5,767
    Distro
    Ubuntu Development Release

    Re: How to copy files of a particular extension RECURSIVELY?

    Code:
     
    man rsync
    Cheesemill

  4. #4
    Join Date
    Aug 2006
    Location
    /honolulu/downtown/
    Beans
    110
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: How to copy files of a particular extension RECURSIVELY?

    i guess that will include the "junkfiles" as well, which i think is not to be copied, as per the post , right @ dennyk23 ?

  5. #5
    Join Date
    Mar 2008
    Beans
    4,715
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: How to copy files of a particular extension RECURSIVELY?

    Try this command:
    Code:
    rsync -avm --include='*.bak' -f 'hide,! */' /Directory-to-be-backed-up/ /media/USB_stick
    It will recursively copy only files that end in .bak from /Directory-to-be-backed-up/ to /media/USB_stick.

    By way of a little explanation:
    "-f" tells rsync to filter according to a pattern:
    '*/' is a pattern which matches 'any directory'
    '! */' matches anything which is not a directory (i.e. a file)
    'hide,! */' means hide all files

    Filter rules are applied in order, and the first rule that matches is applied.

    --include='*.bak' has precedence, so if a file ends in .bak, it is included.
    Any other file gets excluded from the list of files to transfer.

  6. #6
    Join Date
    Aug 2009
    Beans
    4

    Re: How to copy files of a particular extension RECURSIVELY?

    Thanks a lot guys
    Sounds like the kind of command i need to get the job done
    Will try it once i reach home
    Thanks again!

  7. #7
    Join Date
    May 2008
    Location
    London, UK
    Beans
    250
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: How to copy files of a particular extension RECURSIVELY?

    you can use the program called rsync to do the backup,a dn use the --exclude or --exclude-from parameter for the exclusions, with the exclude option you specify a particular file, ie --exclude=junkfile1, with the --exclude-from parameter you specify a file containing the name of files you want to exclude. For example:

    I create a file in my home folder called exclude and type the lines
    Code:
    junkfile1
    junkfile2
    junkfile3
    junkfile4
    then I can type in the terminal
    Code:
    rsync -auv --exclude-from=/home/$USER/exclude /directory/to/be/backed/up/ /media/USB_Stick
    This will back up the folder but leave out the files in /home/$USER/exclude , you can inclde wildcards in the exclude file, for example to exclude all .txt files you would add a line in the file *.txt

    type in 'man rsync' in the terminal to get a full list of the options available to rsync.

    Matt

    EDIT: as the user above said you can also use the --include parameter

  8. #8
    Join Date
    Apr 2009
    Location
    Dhaka,Bangladesh
    Beans
    1,010
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to copy files of a particular extension RECURSIVELY?

    Quote Originally Posted by unutbu View Post
    Try this command:
    Code:
    rsync -avm --include='*.bak' -f 'hide,! */' /Directory-to-be-backed-up/ /media/USB_stick
    It will recursively copy only files that end in .bak from /Directory-to-be-backed-up/ to /media/USB_stick.

    By way of a little explanation:
    "-f" tells rsync to filter according to a pattern:
    '*/' is a pattern which matches 'any directory'
    '! */' matches anything which is not a directory (i.e. a file)
    'hide,! */' means hide all files

    Filter rules are applied in order, and the first rule that matches is applied.

    --include='*.bak' has precedence, so if a file ends in .bak, it is included.
    Any other file gets excluded from the list of files to transfer.
    Isn't it also possible with cp command?

  9. #9
    Join Date
    May 2006
    Location
    Switzerland
    Beans
    2,541
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How to copy files of a particular extension RECURSIVELY?

    Quote Originally Posted by colau View Post
    Isn't it also possible with cp command?
    "rsync" does a better job when bulk-copying stuff, e.g. it goes after the big files first, then fills the gaps between big files with the small files, it makes sure that your cache is maxed out and used well and yet it makes sure that your system remains responsive ...

    "cp" does nothing of that. It's plain stupid in that regard. It will grab whatever file it encounters first and then copy it wherever you told it to. That's all it does. OK if you just copy a bunch of files ... but for copying files from one directory tree into another directory tree I'd definitely recommend "rsync". The result you get is worth the little extra-effort to learn rsync's syntax (which isn't too hard anyway).

  10. #10
    Join Date
    Apr 2009
    Location
    Dhaka,Bangladesh
    Beans
    1,010
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to copy files of a particular extension RECURSIVELY?

    Quote Originally Posted by scorp123 View Post
    "rsync" does a better job when bulk-copying stuff, e.g. it goes after the big files first, then fills the gaps between big files with the small files, it makes sure that your cache is maxed out and used well and yet it makes sure that your system remains responsive ...

    "cp" does nothing of that. It's plain stupid in that regard. It will grab whatever file it encounters first and then copy it wherever you told it to. That's all it does. OK if you just copy a bunch of files ... but for copying files from one directory tree into another directory tree I'd definitely recommend "rsync". The result you get is worth the little extra-effort to learn rsync's syntax (which isn't too hard anyway).
    Thank you.

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
  •