Search code examples
terminalfindpipezshmv

Pipe 'find' command result list to move (mv) command – Terminal (zsh)


Piping result list to command - ZSH

So I want to call the mv command on a list of filtered ls results of a directory:

 print -l $(ls) | awk '$1 !~ /^A.*/ {print $0}'

Here every filename in my directory not starting with a capital "A" is returned...

Now I want the mv command to be called upon every one of these in order to move them in a directory ./Strings.
My take was adding xargs:

...| xargs -0 mv ./Strings 

But this instead ended up in renaming my "Strings"-directory to a combination of every returned filename separated with question marks ("?") - an effect of the -0 null character flag for the xargs command I suppose...

Funnily enough, the wiki on the xargs command (link to the german site here: https://wiki.ubuntuusers.de/xargs/) offers an example-code-snippet to pretty much my question (very similar to my try), but needless to say doesn't solve my problem.

find . -uid 1001 -print | xargs -i mv {} /tmp/Klaus/test

Here it complains about "xargs: illegal option -- i" when I try that in my context:

 find . -regex '^[^A].*' -print | xargs -i mv {} ./Strings

find -exec cmd {} +

I have done some research on my question and what usually pops up is "why can't I pipe find result to ...".
That's why I did not want to leave a suggestion for a solution with the find command and -exec flag, though that didn't lead to my desired result either, unfortunately...:

find . -regex '^[^A].*' -exec mv {} ./Strings +

Solution

  • It's much easier to just do this:

    1. To your .zshrc file, add
      setopt extendedglob
      
      then restart your shell.
    2. This enables you to use the ^ operator:
      mv ^A*(-.) Strings