How to move the first x files

Clash Royale CLAN TAG#URR8PPP
up vote
14
down vote
favorite
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?
shell command-line shell-script wildcards
add a comment |Â
up vote
14
down vote
favorite
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?
shell command-line shell-script wildcards
See also Distributing thousands of files over subfolders
â Stéphane Chazelas
Dec 13 '13 at 17:33
add a comment |Â
up vote
14
down vote
favorite
up vote
14
down vote
favorite
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?
shell command-line shell-script wildcards
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?
shell command-line shell-script wildcards
shell command-line shell-script wildcards
edited Dec 14 '13 at 8:49
Anthon
58.9k1796160
58.9k1796160
asked Dec 13 '13 at 16:57
Fabinout
173115
173115
See also Distributing thousands of files over subfolders
â Stéphane Chazelas
Dec 13 '13 at 17:33
add a comment |Â
See also Distributing thousands of files over subfolders
â Stéphane Chazelas
Dec 13 '13 at 17:33
See also Distributing thousands of files over subfolders
â Stéphane Chazelas
Dec 13 '13 at 17:33
See also Distributing thousands of files over subfolders
â Stéphane Chazelas
Dec 13 '13 at 17:33
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
10
down vote
accepted
mv `ls | head -500` ./subfolder1/
10
(assuming none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with-or.and assumingsubfolder1itself does not show up in that list.)
â Stéphane Chazelas
Dec 13 '13 at 17:05
@StéphaneChazelas if the file contains those, how do we modify the command?
â Peiti Peter Li
Nov 10 '17 at 23:19
add a comment |Â
up vote
11
down vote
With zsh:
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 -- "$@" "$f"
done
mv -- "$@" subfolder1/
1
The POSIX snippet is a gem
â iruvar
Dec 13 '13 at 18:55
add a comment |Â
up vote
3
down vote
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.
1
(provided none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with - or . and provideddestinationitself does not show up in that list.)
â Stéphane Chazelas
Dec 13 '13 at 17:25
@StephaneChazelas True. This is not a complete solution just a method of dealing with the problem.
â Karlson
Dec 13 '13 at 17:39
add a comment |Â
up vote
3
down vote
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.
nice one!ls -Q -S dir1 | head -1000 | xargs -i mv dir1/ dir2/for moving 1000 largest files in dir1 (-S lists file by size)
â oneklc
May 3 at 23:05
Note thatls -Qdoes not produce an output compatible withxargs's expected input format. It helps for file names containing space characters, but not for double quotes or backslashes and harms for file names containing control characters including TAB.
â Stéphane Chazelas
Jun 29 at 13:17
add a comment |Â
up vote
0
down vote
- 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/
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
mv `ls | head -500` ./subfolder1/
10
(assuming none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with-or.and assumingsubfolder1itself does not show up in that list.)
â Stéphane Chazelas
Dec 13 '13 at 17:05
@StéphaneChazelas if the file contains those, how do we modify the command?
â Peiti Peter Li
Nov 10 '17 at 23:19
add a comment |Â
up vote
10
down vote
accepted
mv `ls | head -500` ./subfolder1/
10
(assuming none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with-or.and assumingsubfolder1itself does not show up in that list.)
â Stéphane Chazelas
Dec 13 '13 at 17:05
@StéphaneChazelas if the file contains those, how do we modify the command?
â Peiti Peter Li
Nov 10 '17 at 23:19
add a comment |Â
up vote
10
down vote
accepted
up vote
10
down vote
accepted
mv `ls | head -500` ./subfolder1/
mv `ls | head -500` ./subfolder1/
edited Dec 13 '13 at 17:04
Stéphane Chazelas
286k53528867
286k53528867
answered Dec 13 '13 at 17:03
schaiba
5,38312028
5,38312028
10
(assuming none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with-or.and assumingsubfolder1itself does not show up in that list.)
â Stéphane Chazelas
Dec 13 '13 at 17:05
@StéphaneChazelas if the file contains those, how do we modify the command?
â Peiti Peter Li
Nov 10 '17 at 23:19
add a comment |Â
10
(assuming none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with-or.and assumingsubfolder1itself does not show up in that list.)
â Stéphane Chazelas
Dec 13 '13 at 17:05
@StéphaneChazelas if the file contains those, how do we modify the command?
â Peiti Peter Li
Nov 10 '17 at 23:19
10
10
(assuming none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with
- or . and assuming subfolder1 itself does not show up in that list.)â Stéphane Chazelas
Dec 13 '13 at 17:05
(assuming none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with
- or . and assuming subfolder1 itself does not show up in that list.)â Stéphane Chazelas
Dec 13 '13 at 17:05
@StéphaneChazelas if the file contains those, how do we modify the command?
â Peiti Peter Li
Nov 10 '17 at 23:19
@StéphaneChazelas if the file contains those, how do we modify the command?
â Peiti Peter Li
Nov 10 '17 at 23:19
add a comment |Â
up vote
11
down vote
With zsh:
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 -- "$@" "$f"
done
mv -- "$@" subfolder1/
1
The POSIX snippet is a gem
â iruvar
Dec 13 '13 at 18:55
add a comment |Â
up vote
11
down vote
With zsh:
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 -- "$@" "$f"
done
mv -- "$@" subfolder1/
1
The POSIX snippet is a gem
â iruvar
Dec 13 '13 at 18:55
add a comment |Â
up vote
11
down vote
up vote
11
down vote
With zsh:
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 -- "$@" "$f"
done
mv -- "$@" subfolder1/
With zsh:
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 -- "$@" "$f"
done
mv -- "$@" subfolder1/
edited Dec 13 '13 at 17:31
answered Dec 13 '13 at 17:03
Stéphane Chazelas
286k53528867
286k53528867
1
The POSIX snippet is a gem
â iruvar
Dec 13 '13 at 18:55
add a comment |Â
1
The POSIX snippet is a gem
â iruvar
Dec 13 '13 at 18:55
1
1
The POSIX snippet is a gem
â iruvar
Dec 13 '13 at 18:55
The POSIX snippet is a gem
â iruvar
Dec 13 '13 at 18:55
add a comment |Â
up vote
3
down vote
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.
1
(provided none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with - or . and provideddestinationitself does not show up in that list.)
â Stéphane Chazelas
Dec 13 '13 at 17:25
@StephaneChazelas True. This is not a complete solution just a method of dealing with the problem.
â Karlson
Dec 13 '13 at 17:39
add a comment |Â
up vote
3
down vote
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.
1
(provided none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with - or . and provideddestinationitself does not show up in that list.)
â Stéphane Chazelas
Dec 13 '13 at 17:25
@StephaneChazelas True. This is not a complete solution just a method of dealing with the problem.
â Karlson
Dec 13 '13 at 17:39
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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.
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.
edited Dec 13 '13 at 17:33
answered Dec 13 '13 at 17:10
Karlson
4,8301742
4,8301742
1
(provided none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with - or . and provideddestinationitself does not show up in that list.)
â Stéphane Chazelas
Dec 13 '13 at 17:25
@StephaneChazelas True. This is not a complete solution just a method of dealing with the problem.
â Karlson
Dec 13 '13 at 17:39
add a comment |Â
1
(provided none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with - or . and provideddestinationitself does not show up in that list.)
â Stéphane Chazelas
Dec 13 '13 at 17:25
@StephaneChazelas True. This is not a complete solution just a method of dealing with the problem.
â Karlson
Dec 13 '13 at 17:39
1
1
(provided none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with - or . and provided
destination itself does not show up in that list.)â Stéphane Chazelas
Dec 13 '13 at 17:25
(provided none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with - or . and provided
destination itself does not show up in that list.)â Stéphane Chazelas
Dec 13 '13 at 17:25
@StephaneChazelas True. This is not a complete solution just a method of dealing with the problem.
â Karlson
Dec 13 '13 at 17:39
@StephaneChazelas True. This is not a complete solution just a method of dealing with the problem.
â Karlson
Dec 13 '13 at 17:39
add a comment |Â
up vote
3
down vote
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.
nice one!ls -Q -S dir1 | head -1000 | xargs -i mv dir1/ dir2/for moving 1000 largest files in dir1 (-S lists file by size)
â oneklc
May 3 at 23:05
Note thatls -Qdoes not produce an output compatible withxargs's expected input format. It helps for file names containing space characters, but not for double quotes or backslashes and harms for file names containing control characters including TAB.
â Stéphane Chazelas
Jun 29 at 13:17
add a comment |Â
up vote
3
down vote
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.
nice one!ls -Q -S dir1 | head -1000 | xargs -i mv dir1/ dir2/for moving 1000 largest files in dir1 (-S lists file by size)
â oneklc
May 3 at 23:05
Note thatls -Qdoes not produce an output compatible withxargs's expected input format. It helps for file names containing space characters, but not for double quotes or backslashes and harms for file names containing control characters including TAB.
â Stéphane Chazelas
Jun 29 at 13:17
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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.
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.
answered Feb 13 at 22:23
Luis AntolÃn Cano
405311
405311
nice one!ls -Q -S dir1 | head -1000 | xargs -i mv dir1/ dir2/for moving 1000 largest files in dir1 (-S lists file by size)
â oneklc
May 3 at 23:05
Note thatls -Qdoes not produce an output compatible withxargs's expected input format. It helps for file names containing space characters, but not for double quotes or backslashes and harms for file names containing control characters including TAB.
â Stéphane Chazelas
Jun 29 at 13:17
add a comment |Â
nice one!ls -Q -S dir1 | head -1000 | xargs -i mv dir1/ dir2/for moving 1000 largest files in dir1 (-S lists file by size)
â oneklc
May 3 at 23:05
Note thatls -Qdoes not produce an output compatible withxargs's expected input format. It helps for file names containing space characters, but not for double quotes or backslashes and harms for file names containing control characters including TAB.
â Stéphane Chazelas
Jun 29 at 13:17
nice one!
ls -Q -S dir1 | head -1000 | xargs -i mv dir1/ dir2/ for moving 1000 largest files in dir1 (-S lists file by size)â oneklc
May 3 at 23:05
nice one!
ls -Q -S dir1 | head -1000 | xargs -i mv dir1/ dir2/ for moving 1000 largest files in dir1 (-S lists file by size)â oneklc
May 3 at 23:05
Note that
ls -Q does not produce an output compatible with xargs's expected input format. It helps for file names containing space characters, but not for double quotes or backslashes and harms for file names containing control characters including TAB.â Stéphane Chazelas
Jun 29 at 13:17
Note that
ls -Q does not produce an output compatible with xargs's expected input format. It helps for file names containing space characters, but not for double quotes or backslashes and harms for file names containing control characters including TAB.â Stéphane Chazelas
Jun 29 at 13:17
add a comment |Â
up vote
0
down vote
- 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/
add a comment |Â
up vote
0
down vote
- 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/
add a comment |Â
up vote
0
down vote
up vote
0
down vote
- 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/
- 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/
edited Sep 13 at 6:36
RalfFriedl
4,1251625
4,1251625
answered Sep 13 at 6:13
Pratik Hiremath
1
1
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f105040%2fhow-to-move-the-first-x-files%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
See also Distributing thousands of files over subfolders
â Stéphane Chazelas
Dec 13 '13 at 17:33