Replacement for DOS xcopy command

6,667

Solution 1

The power of tools in Ubuntu is that you can combine them. The next command finds all .mp3 files in the current directory and its subdirectories, and copies them to the ../TEMP2/ folder, preserving paths:

find -iname '*.mp3' -exec install -D {} ../TEMP2/{} \;
  • find -iname '*.mp3' - finds all files ending with .mp3 (case-insensitive) and
    • -exec - executes a command for each match:
      • install -D {} ../TEMP2/{} - copies the matched file to ../TEMP/ preserving the path. ({} is replaced by the path including filename)
    • \; - ends the -exec command

If you want to get the progress, add -ls ("list") to the command before -exec. It can be put after \; too, but in that case the name is shown after being copied. Examples:

find -iname '*.mp3' -ls -exec install -D {} ../TEMP2/{} \;
find -iname '*.mp3' -exec install -D {} ../TEMP2/{} \; -ls

Solution 2

There are several options, but none is really simple, I'm afraid…

rsync

rsync -r --include="*/" --include="*.mp3" --exclude="*" --prune-empty-dirs . ../TEMP2

This tells to exclude all files (exclude="*"), but to look into all directories (include="*/") and to include all mp3 files (include="*.mp3"). If you do not want to copy directories not containing any mp3 files, in addition the --prune-empty-dirs option is necessary.

zip

zip -R archive.zip "*.mp3"
unzip -d ../TEMP2 archive.zip && rm archive.zip

The first command creates an archive with all mp3 files, the second unzips the content to the target directory and deletes the archive file if it was successful.

find

find . -iname "*.mp3" -exec install -D {} ../TEMP2/{} ";"

This will find all mp3 files and copy them to the corresponding path in the ../TEMP2 directory, after creating the directory structure first (install -D).

copy all and delete the rest

This only makes sense if you have just a few files that you don't want to copy:

cp -r * ../TEMP2
find ../TEMP2 -type f \! -iname '*.mp3' -delete

This copies everything and then deletes all files that are not mp3 files

Share:
6,667

Related videos on Youtube

sleepless
Author by

sleepless

Updated on September 18, 2022

Comments

  • sleepless
    sleepless over 1 year

    In DOS there's the command

    xcopy /s *.mp3 ..\TEMP2

    How can I do this in Ubuntu?

    cp -r *.mp3 ../TEMP2

    will copy recursively all mp3 files, unfortunately without the path because of the *.mp3 filter.

    cp -r *.* ../TEMP2

    will copy the path but can not be limited to mp3 files

    I had a look at rsync, but I don't get it.

    • Admin
      Admin over 12 years
      Do you want to copy the mp3 files WITHOUT the directory to the temp2 folder or do you want to copy them WITH the directory.
    • Admin
      Admin over 12 years
      @Luis: I guess he wants to preserve the directory structure, or else cp -r would have worked.
    • Admin
      Admin over 12 years
      some nifty commands below. But what is wrong with good old tar, a move and an untar :D
    • Admin
      Admin over 12 years
      What shell are you using? The second command will only copy the mp3 files in the current directory (and any directories named .mp3 -- it will not recursively copy all mp3 files.
    • Admin
      Admin over 12 years
      Indeed I want to preserve directory structure.
    • Admin
      Admin over 12 years
      I have just seen this similar question with a different and interesting solution: unix.stackexchange.com/questions/19841/…
  • MestreLion
    MestreLion over 12 years
    I dont understand why would this be any different than rsync -r, except using a much more complex syntax
  • Lekensteyn
    Lekensteyn over 12 years
    I'm not that familiar with rsync, so this is the best command I could think of.
  • enzotib
    enzotib over 12 years
    I suppose you need a -D option to install.
  • sleepless
    sleepless over 12 years
    This one does not copy the subdir.
  • sleepless
    sleepless over 12 years
    Yes your find construction works too! I like that one the most. It's a great learning experience. I have come to the right place. But the forum software is very user unfriendly. Rinus
  • Lekensteyn
    Lekensteyn over 12 years
    @sleepless Please try again, I was missing the -D option which would create any directories if needed.
  • sleepless
    sleepless over 12 years
    lekenstein's find -iname '*.mp3' -exec install -D {} ../TEMP2/{} \; with enzotib's "-D" got it going to work. Marcel Stimbergs work is excellent. That means after two days searching solved with your help. You are amazing. Thanks a lot even for the contribs that did not work. Solved! Rinus
  • sleepless
    sleepless over 12 years
    Yes lekenstein, both constructions work and I like to see the progress, thats a good addition. But I discoverd, if one of the files is hidden, it is skipped, how to solve that?
  • Lekensteyn
    Lekensteyn over 12 years
    @sleepless: I cannot confirm your findings, find shows hidden files (like .hidden.mp3 as well as .hiddenfolder/file.mp3) for me. Perhaps your file browser hide it for you? If you're using the ls command, add the -A option to show dotfiles too.
  • sleepless
    sleepless over 12 years
    You are completely right, sorry for this stupidity.
  • Lekensteyn
    Lekensteyn over 11 years
    Someone suggested to use quotes as in -exec ... "{}" \;. This is not necessary as {} correctly expands into a single argument. "{}" still works because the quotes are eaten by bash.