Terminal – How to Use Pipe to mv Command in Unix

command lineterminalunix

I'm trying to move files from Terminal but only those from the results of a grep query.

ls -l | grep -i s02 | mv

What is the best way to complete the command above?

Best Answer

You can accomplish this by just running

cd directory/containing/the/files
mv *[sS]02* /path/to/target/

For more complex operations there is also the option to use find to find all relevant files. The example from your question could also be written as

cd directory/containing/the/files
find . -type f -maxdepth 1 -iname '*s02*' -exec mv {} /path/to/target/directory/ \;

It's worth to have a look at man find to see which other options are available.