Skip to content

Help With Command Line Basics

I have a directory in my home drive. Within this directory, there are several hundred subdirectories. In each of these subdirectories, there are several types of files, of which I want to take ONE of these types, and move all those files to a single directory. (In this case they are .ttf files that I want to move to ~/.fonts/, but I have wanted to do this in the past with .mp3 files to a USB Drive, so I’ve decided to investigate a bit harder.

Now, I don’t mind using the command line occasionally, so I thought something along the lines of cd ~/fonts/
cp -r *.ttf ~/.fonts/
would do the trick, but it doesn’t. It cannot find *.ttf. I’ve had a read of both the cp man page and the wikipedia entry, but it appears cp doesn’t do this.

Before I go away and attempt to write some sort of script to do this, just wondered if anyone could tell me how to do this, or if it is even possible, with ’standard’ tools?

{ 8 } Comments

  1. Omahn | April 10, 2007 at 3:31 pm | Permalink

    If I understand what you’re asking, I think this will do:

    cd ~/fonts
    fine -iname ‘*.ttf’ -exec cp ‘{}’ ~/.fonts/ \;

  2. Omahn | April 10, 2007 at 3:32 pm | Permalink

    and when I say ‘fine’ I really mean ‘find’ :-)

  3. Will Thompson | April 10, 2007 at 4:17 pm | Permalink

    That solution will run cp once per font. I suspect there’s a way to use xargs to use only a single invocation of cp.

    The best way that I can see is:
    (find ~/fonts -iname ‘*.ttf’; echo ~/.fonts) | xargs cp

    The `echo` is to put the destination at the end of the argument list. Is there a better way to do this, anyone?

  4. sheepeatingtaz | April 10, 2007 at 4:40 pm | Permalink

    Omahn: With that, nothing gets copied
    Will: With that I get cp: missing destination file operand after `/home/sheepeatingtaz/.fonts'

    But thanks for the help, it is appreciated.

  5. Martin | April 10, 2007 at 4:46 pm | Permalink

    Hows about renaming the fonts dir?

  6. sheepeatingtaz | April 10, 2007 at 4:52 pm | Permalink

    Martin: I could, but I want to get rid of the subdirectories, so that all the font files are together in one directory, and I want to NOT copy other files that are in the subdirectories.

  7. Omahn | April 10, 2007 at 8:56 pm | Permalink

    I’ll try and catch you tomorrow in #lugradio - that command should work.
    (as in, it works for me) ;-)

  8. sheepeatingtaz | April 20, 2007 at 6:58 am | Permalink

    Pinched from Grifferz:
    $ cd ~/fonts
    $ find . -type f -iname '*.ttf' -print0 | xargs -I{} -0 cp {} ~/.fonts/

Post a Comment

Your email is never published nor shared. Required fields are marked *