Command Line – Moving All .txt Files to a New Directory

command linedirectoryfilesmvrecursive

Basically, my hard drive is a mess and I have like 200+ sub-directories in a main directory. I want to essentially move all files in 200+ sub-directories that have the extension .txt etc to a new directory. For example, n00b.txt or n00b.txt.exe

So I try the following command in the main directory consisting of the 200+ subdirectories sudo mv **/*.txt ~/Desktop/tmpremo/

Instead I am getting this error: bash: /usr/bin/sudo: Argument list too long

Why am I getting it and how do I remove say .txt,.txt.exe? How do I fix it?

Best Answer

This would move all files with .txt and .txt.exe extensions present anywhere inside the current directory (even in subdirectories) to ~/Desktop/tmpremo.

$ sudo find . -type f \( -iname '*.txt' -o -iname '*.txt.exe' \) -exec mv {} ~/Desktop/tmpremo \;

If you want another extension too, just add -o -iname '*.extension' before the -exec.

PS: As @xenoid noted, please refrain from using sudo unless it is absolutely required for the task at hand.

Related Question