How can you move (or copy) all files to a directory with the same filename prefix?

10,291

Solution 1

It would be a hell to tell find what to do in this case.

Better use the shell:

for i in **/*.{xrt,ini,moo}; do
  FILE=$(basename "$i")
  DIR=~/dst/${FILE%.*}
  echo mkdir -p -- "$DIR"
  echo mv -i -t "$DIR" -- "$i"
done

Use shopt -s globstar to make the ** glob work (or use zsh!). And remove the echos later if the command prints what you want.

Solution 2

cheating find command line:

find source -name "*.xrt,*.ini,*.moo" -exec env file={} bash -c 'base="$(basename "$file")";dir="dst/${base%.*}";mkdir -p "$dir";cp "$file" "$dir"' \;

actually a work around for calling bash script in find LOL.

=P

Share:
10,291

Related videos on Youtube

qodeninja
Author by

qodeninja

I write qode mostly for myself... out of curiosity for solving problems, understanding how things work or making (sometimes unnecessarily) complex systems to only simplify them later (once I discover alternative strategies). For whatever reason, I like torturing myself with Regular Expressions, SED, Bash and JavaScript (Node), but have found a growing (painful) love with Python. Having said that, I enjoy scripting languages a lot more than compiled languages, and I've coded in almost all of the major modern ones except Ruby. I'm a secret Turing Machine/Computer Grammars/Regular Expressions nerd, and have written my own mini compilers and toy languages. I'm constantly writing command dispatchers that I later write scripting languages for; it's an addiction. There's plenty room for me to grow and learn still; and I appreciate the wisdom of grey beards and lady wizards even if I don't always follow their sage advice. FOSS is hella cool; cool projects are cool. Find me online if you have ideas. I'm a really bad programmer but I'll write a line or two for the betterization of the peoples. Edit: I recently discoved that VI is really just SED with wings. Still not using VI. Nano or bust.

Updated on September 18, 2022

Comments

  • qodeninja
    qodeninja almost 2 years

    Using Bash

    So let's say I have a bunch of files randomly placed in a parent directory ~/src, I want to grab all the files matching a certain suffix and move (or copy) them to a ~/dist directory.

    Let's assume for this purpose that all filenames have this naming convention:

    <filename_prefix>.<filename_suffix>
    

    I found out that this was a quick way to get all files with a particular filename_suffix and put them in a dist folder:

    mkdir ~/dst
    find source -name "*.xxx" -exec mv -i {} -t ~/dst \;
    

    Now a step further... how can I use the output of find, in this case filename, and use the filename_prefix to generate a directory of the same name in ~/dist and then move (or copy) all the files with that prefix into the appropriate directory?

    mkdir ~/dst
    find source -name "*.xrt,*.ini,*.moo" -exec mv -i {} -t ~/dst \;
    

    Essentially, how do I change the above command (or maybe use another command), to create a structure like this

    (OUTPUT)

    ~/dist/people/people.xrt
    ~/dist/games/games.xrt
    ~/dist/games/games.moo
    ~/dist/games/games.ini
    ~/dist/monkeys/monkeys.ini
    ~/dist/monkeys/monkeys.xrt
    

    from a directory tree like this?

    (INPUT)

    ~/src/xrt/people.xrt
    ~/src/xrt/games.xrt
    ~/src/conf/games.ini
    ~/src/pack/monkeys.xrt
    ~/src/e344/games.moo
    ~/src/e344/monkeys.moo
    ~/src/en-us/monkeys.ini
    
  • qodeninja
    qodeninja over 12 years
    using bash is this the same?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 12 years
    @codeninja shopt -s globstar, see my edit. If there are only two levels, you can use */*.* and the snippet will work in any shell.
  • Stéphane Gimenez
    Stéphane Gimenez over 12 years
    Guess what happens if there is a file which is called… happy rm -rf / ! This kind of code deserves a -1.
  • Stéphane Gimenez
    Stéphane Gimenez over 12 years
    Your edit is not enough: let's say you cliked on a link and downloaded Yay" rm -rf / "Rox0r.avi, again there is like a problem…
  • Stéphane Gimenez
    Stéphane Gimenez over 12 years
    (And there is no way to make this kind of substitution safe).
  • Stéphane Gimenez
    Stéphane Gimenez over 12 years
    Ah maybe it's possible to do this differently. It seems that the following works: -exec bash -c 'command which uses "$1"' bash '{}' ';'. Well, it's quite pointless anyway.
  • yuyichao
    yuyichao over 12 years
    @StéphaneGimenez done, bash is not the only thing to use. LOL
  • Stéphane Gimenez
    Stéphane Gimenez over 12 years
    At least it seems safe now.