How To Move Large Number of Files in Linux? [CLI]

Have a large number of files to move? No need to worry, It can be done easily through the Linux command line. Moving a large number of files manually through GUI will be too time-consuming and probably give an error. It will also affect your system resources unless you have high system specifications.

Also read: Copy Large Number of Files Using The Terminal

Move a large number of files in Linux

Linux provides some easy command-line tools to move or even copy a large number of files in Linux. We will be discussing them in detail.

1. mv command

mv command is used to move files or folders. If you have files of the same extension, You can move them from one place to another by Executing the following command:

sudo mv *.<extension> /<destination>

Here, * indicates all the files with the specified extension. For example, We have some files with the extension .txt and we need to move them to the test directory located on Desktop.

sudo mv *.txt /home/sid/Desktop/test
Screenshot From 2022 02 28 09 20 07

If you have files with different extensions, Execute the following command:

sudo mv *.txt *.sh /home/sid/Desktop/test
Screenshot From 2022 02 28 09 24 38

But, If you have a hundred thousand files to be moved, Moving them through the mv command needs a long list of arguments. Linux has a limit on the maximum number of arguments to be used with a single command. mv command will give an error: argument list too long. So, this can be done using the find command.

2. find command

find command is used to search files in a directory. We can use the find command along with other commands like the mv command or the xargs command to move a large number of files. The xargs command is used to build and execute command lines from standard input.

Screenshot From 2022 02 28 09 56 20

For example, We have 100001 files in the files directory, it will show an error when moved using the mv command:

sudo mv *.txt /home/sid/Desktop/files
Screenshot From 2022 02 28 09 45 26

To move these files using the find command, execute the following command:

find . -maxdepth 1 -name "*.txt"  -exec sh -c 'mv "$@" "$0"' /home/sid/Documents/files {} +
Screenshot From 2022 02 28 10 05 28

In the above command, replace the destination to where you want to copy the files. Instead of *.txt after the -name option, You can change it as required.

Another way of moving a large number of files is using xargs instead of -exec, Execute the following command:

find . -maxdepth 1 -iname "*.txt" -type f -print0 | xargs -0 mv -t /home/sid/Desktop/files
Screenshot From 2022 02 28 10 11 32

Conclusion

So, We saw how to move a large number of files in Linux. Performing this operation through GUI is really not a great idea as it will stop responding and even your system may hang while copying a large number of files. Similarly, We can also copy files using some command-line tools. Thank you for reading!