ls 或 dir - 限制文件名返回的数量
有没有办法限制使用 ls 或 dir 命令返回的文件名数量?我在一台装有 cygwin 的 Windows 机器上,所以任一命令都可以。
我有一个包含超过 100 万个文档的目录,我需要将其批量放入子文件夹中。我正在编写一个简单的脚本,列出前 20,000 个左右,将它们移动到子文件夹中,然后重复接下来的 20,000 个。我希望 ls 或 dir 能够选择仅列出一定数量的文件,但我似乎找不到办法来做到这一点。
我的另一个选择是将文件名打印到文本文件中并解析它以进行移动。不过,似乎应该有一个更简单的解决方案。
Is there a way to limit the number of filenames to be returned using the ls or dir command? I'm on a windows machine with cygwin, so either command will do.
I have a directory with over 1 million docs that I need to batch out into subfolders. I'm looking to write a simple script that would list the first 20,000 or so, move them to a subfolder, then repeat on the next 20,000. I would expect ls or dir to have an option to list only a certain number of files, but I can't seem to find a way to do it.
My other option is to print the filenames out to a text file and parse that to make the moves. It seems like there should be a simpler solution though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 cygwin,您应该能够使用 head 命令,如下所示:
列出前 20,000 个。
如果您想在 1 个命令行中移动批处理,类似这样的操作应该可行:(
其中 subdir 是您要将文件移动到的子目录)。
首先运行此命令(使用 echo 命令)以验证它在运行实际移动之前是否可以工作:
不过要小心,因为您要将文件移动到子目录中,因为“ls”命令也可能会获取您的子目录。
如果都是txt文件,你可以这样做:
获取以txt结尾的文件
If you are using cygwin, you should be able to use the head command, like this:
to list the first 20 thousand.
If you want to move the batch in 1 command line, something like this should work:
(where subdir is the subdir you want to move the files to).
Run this first (with the echo command) to verify it will work before running the actual move:
Just be careful though, since you are moving files into a subdir, because the "ls" command will probably pick up your subdirs as well.
If they are all txt files, you could do something like this:
to get files that end in txt
或者您可能想要组合
head
和tail
来从中间获取条目:示例数据:
现在让我们跳过 2 个条目并获取以下 2 个条目:
这将为您提供:
因此
head -n 4
将返回前 4 个条目(banana 到 orange)和后面的tail -n 2 将得到最后 2 个之前的结果(苹果到橙色)并且不在原始文件中。
Or you might want to combine
head
andtail
to fetch entries out of the middle:Example data:
Now let's skip 2 entries and fetch the following 2 ones:
That will give you:
So
head -n 4
will return the first 4 entries (banana to orange) and the followingtail -n 2
will get the last 2 ones out of that previous result (apple to orange) and not out of the original file.