I have this huge folder with thousands of unordered files. Is it feasible to move the first 5000s to a subfolder via the mv command? For now I move files with
mv *some_pattern* ./subfolder1/
As for now, I move images quite randomly, it’s not really important if there aren’t exactly 5000 files in each subfolder. Is there a better way to do it?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
mv `ls | head -500` ./subfolder1/
Method 2
With zsh and its glob qualifiers:
mv -- *(D.oN[1,5000]) ./subfolder1
To move up to 5000 regular files in the order they are in the directory.
For the first 5000 in the lexicographically sorted list:
mv -- *(D.[1,5000]) ./subfolder1
If you get an error about arg list too long. You can use zsh‘s buitin mv command by issuing:
zmodload zsh/files
first.
POSIXly:
set -- for f in .* *; do [ "$#" -lt 5000 ] || break [ -f "$f" ] || continue [ -L "$f" ] && continue set -- "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="351175">[email protected]</a>" "$f" done mv -- "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c385c">[email protected]</a>" subfolder1/
Method 3
A version that is simple and supports special chars, spaces, etc.
ls -Q dir1 | head -1000 | xargs -i mv dir1/{} dir2/
For this to work as-is dir2 must exist and you have to execute it from the parent directory of dir1 and dir2.
This will move 1000 files from dir1 to dir2.
Method 4
- Goto the directory which you want to move files from
-
run below command
find . -name 'Hello*.gz' | head -n 5000 | xargs -I {} mv {} /data01/path/
In the find command, . (dot) denotes current directory
finds files which start with Hello and end with gz , first 5000 files will be moved to the path /data01/path/
Method 5
You might need to do something like this:
x=1
for file in *
do
if [ "X$x" = "X#####" ]; then
break
fi
mv $file <destination>
x=`expr $x + 1`
done
This script works in bash, ksh, sh and multiple UNIX variants.
Method 6
I was able to succesfully do a move of 50,000 without the mv bash error like this
ls | head -50000 | xargs -I{} sudo mv {} 01/
Funny enough, this was on a samba share, so the 50k limit is because Windows Explorer GUI does not like more than 60k files in a dir in general.
Method 7
Version working even in extremely large directories: moving thousand files from directory containing half million:
ls -U . | head -1000 | xargs mv -t 01/
(Note that the syntax with {} is spawning new mv for every file, while this one doesn’t)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0