Linux – Using xargs with mv and mkdir command in Linux


I am attempting to create a directory using the command mkdir. However, I would like to move a subset of files into that directory. I understand I can use xargs, however my attempts have failed. For example, I have tried mkdir test | xargs -i mv test.text {}. It creates the directory, but it does not move the file test.txt into it after it has been created.

Best Answer

mkdir does not output anything, therefore xargs will not do anything useful. I don't understand exactly what you want to do, so you should explain your question better.

"would like to move a subset of files into that directory" -> do files in this subset share one or more traits? If yes, use the find command like this:

find [conditions] -exec mv "{}" dirname \;

If you want to avoid typing the name of the directory twice, or if you are doing this from a script, you can do something like

mkdir $dirname && mv filename $dirname