Faster find and move using the find and xargs commands. Almost as fast as locate.

find . -maxdepth 2 -name "*somepattern" -print0 | xargs -0 -I "{}" echo mv "{}" /destination/path
Only tested on Linux Ubunty Hardy. Works when file names have spaces. The "-maxdepth 2" limits the find search to the current directory and the next one deeper in this example. This was faster on my system because find was searching every directory before the current directory without the -maxdepth option. Almost as fast as locate when used as above. Must use double quotes around pattern to handle spaces in file names. -print0 is used in combination with xargs -0. Those are zeros not "O"s. For xargs, -I is used to replace the following "{}" with the incoming file-list items from find. Echo just prints to the command line what is happening with mv. mv needs "{}" again so it knows what you are moving from. Then end with the move destination. Some other versions may only require one "{}" in the move command and not after the -I, however this is what worked for me on Ubuntu 8.04. Some like to use -type f in the find command to limit the type.
Sample Output
#comment: output only occurs with echo in xargs command, without echo the below would #not appear.
mv /path/of/found/directory/filename /destination/path
mv /path/of/found/directory/filename /destination/path

4
2009-08-01 01:55:47

What Others Think

locate is usually faster because it searches through a pre-generated index file, whereas find actively searches the filesystem. Once filesystem data is cached, they can be competitive. However, locate only knows about files that existed during its last 'updatedb' run (which is usually daily). You can do this with find using find's "xargs mode" which is invoked with the "-exec cmd {} +" option, where cmd is "mv -t": find . -maxdepth 2 -name "*pattern" -exec mv -t /dest {} + the trick is that mv supports the -t option which lets you specify the destination first.
bwoodacre · 644 weeks and 1 day ago
you don't need that echo
blaeks · 359 weeks and 4 days ago
I want to tell everyone with the help of this homepage how to http://fixwindows10connections.com">fix connections to bluetooth audio devices in windows 10 so if you have any issue visit and get solution from here.
ethan81 · 12 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: