I got an issue in my linux Cent OS Server. Suddenly lots of file generated by some script inside my root directory. I was trying to Find and Move files to another directory by keyword linux command.

I was trying to Delete them. But it was quite impossible to delete millions of file one by one.

Then I found a command, which one solved my Problem. I decide to create a temporary directory named “moved”. And run 3 Commands.

Command:  find . -name ‘Hello*.gz’ | head -n 5000 | xargs -I {} mv {} /data01/path/

Explanation of this command below:

a) In this find command .(dot) denotes current directory

b)  Find files which start with Hello and end with .gz, * in the name is for any text in between this 2 name. You can use  start keyword like “Hello*“. Also you can use end keyword like “*.gz“. All of the find keyword will work nicely to find file and move to anther directory at once.

c) “head -n 5000” this part of command specify the no of affected file by find command at once. You can increase the no of file execution 5000 to 500000 or run same command multiple times to clean your file to moved directory.

d) Then mv command followed with file moving directory “/home/username/public_html/moved

e) Finally run command “rm -f -r /home/username/public_html/moved” to remove the directory.

You can have -f and -r together like “rm -fr /home/username/public_html/moved

That’s it !!! 🙂

I Hope this command will help you a lot solve this kind of problem.