0

I have a directory with a large number of files, and I need to separate them by owner. My instinct was to pipe ls output to mv, but I've seen that that is not recommended due to the possibility that filenames could contain special characters. In any case, the closest I can think of is:

ls -l | grep OWNER | find regex_for_filename | mv ../

but this does not work.

Note - I do have a reliable regex for files within the directory, but the ownership is scattered.

Community
  • 1
  • 1
wils484
  • 275
  • 1
  • 3
  • 14
  • 4
    `find -maxdepth 1 -user username -exec mv {} ../ \;`, omit the maxdepth if you need it from subdirectories as well. – Wrikken Feb 12 '14 at 21:26
  • 2
    @Wrikken why not put that in an answer? – rainkinz Feb 12 '14 at 21:30
  • You want the list files on the command line of mv, not in its standard input. So a pipe doesn't make sense. Find, like in @Wrikkens example, is the best way to do this – Guntram Blohm Feb 12 '14 at 21:30
  • @rainkinz: well, question #a couple of thousands where the answer is "use find", I have the hope someone has energy enough to close this as a duplicate :P – Wrikken Feb 12 '14 at 21:33
  • 1
    @Wrikken This is what I am looking for. This answer could help out those who, like me, mixed up the keyword "owner" with "user." – wils484 Feb 12 '14 at 22:01
  • @Wrikken Agreed with OP that this could probably help some people looking for the specific problem, unlike all the "quote your variables to prevent word-splitting..." questions, where if you know how to describe the problem you know how to fix it. – Reinstate Monica Please Feb 12 '14 at 22:03
  • Ah, well, upgraded it to an answer then ;) – Wrikken Feb 12 '14 at 22:04

1 Answers1

6
find -maxdepth 1 -user username -exec mv {} ../ \;

Omit the maxdepth if you need files from subdirectories.

Wrikken
  • 69,272
  • 8
  • 97
  • 136