mv files with | xargs

Rob*_*Rob 24 command-line mv

I'm just trying to move a bunch of files (not symlinks) out of my /etc/apache/sites-enabled folder into the /etc/apache/sites-available folder with the following:

/etc/apache2/sites-enabled$ find . -maxdepth 1 -type f | xargs mv {} ../sites-available/
Run Code Online (Sandbox Code Playgroud)

but I'm an ubuntu n00b and am getting this error:

mv: target `./real-file' is not a directory
Run Code Online (Sandbox Code Playgroud)

where 'real-file' is a test file I've set up on my dev environment. I'm trying to tidy up someone else's mess on a production server ;-)

Avi*_*Raj 35

You could try the -exec option with find command,

/etc/apache2/sites-enabled$ sudo find . -maxdepth 1 -type f -exec mv {} /etc/apache2/sites-available \;
Run Code Online (Sandbox Code Playgroud)

For moving files owned by root, you need sudo permissions.

If you want to use xargs command then add -I option to it.

find . -maxdepth 1 -type f | sudo xargs -I {} mv {} /etc/apache2/sites-available/
Run Code Online (Sandbox Code Playgroud)


小智 8

理想情况下,您应该将 -print0 与 find 一起使用,这样带空格的文件名就不会搞砸了。

例如这应该工作:

find . -whatever-flags-go-here -print0 | xargs -r0 mv -t target-directory
Run Code Online (Sandbox Code Playgroud)