[英]Bash script to list files periodically
I have a huge set of files, 64,000, and I want to create a Bash script that lists the name of files using 我有一组庞大的文件,共64,000个,我想创建一个Bash脚本,该脚本使用以下命令列出文件名:
ls -1 > file.txt
for every 4,000 files and store the resulted file.txt in a separate folder. 每4,000个文件,并将结果file.txt存储在单独的文件夹中。 So, every 4000 files have their names listed in a text files that is stored in a folder.
因此,每4000个文件的名称都列在存储在文件夹中的文本文件中。 The result is
结果是
folder01 contains file.txt that lists files #0-#4000 folder01包含列出文件#0-#4000的file.txt
folder02 contains file.txt that lists files #4001-#8000 folder02包含列出文件#4001-#8000的file.txt
folder03 contains file.txt that lists files #8001-#12000 folder03包含列出文件#8001-#12000的file.txt
. 。
. 。
. 。
folder16 contains file.txt that lists files #60000-#64000 folder16包含列出文件#60000-#64000的file.txt
Thank you very much in advance 非常感谢你提前
You can try 你可以试试
ls -1 | awk '
{
if (! ((NR-1)%4000)) {
if (j) close(fnn)
fn=sprintf("folder%02d",++j)
system("mkdir "fn)
fnn=fn"/file.txt"
}
print >> fnn
}'
Explanation: 说明:
if
statement is true for the first line if
语句为真 system
calls an operating system function from within awk system
从awk内调用操作系统功能 print
in itself prints the current line to standard output, we can redirect (and append) the output to the file using >>
print
本身将当前行打印到标准输出,我们可以使用>>
将输出重定向(并附加)到文件 j=0
in the beginning of the program j=0
This will get you pretty close; 这会让你非常接近;
ls -1 | split -l 4000 -d - folder
Run the result of ls
through split
, breaking every 4000 lines ( -l 4000
), using numeric suffixes ( -d
), from standard input ( -
) and start the naming of the files with folder
. 通过
split
运行ls
的结果,使用数字后缀( -d
)从标准输入( -
) split
每4000行( -l 4000
),并开始使用folder
命名folder
。
Results in folder00
, folder01
, ... 结果在
folder00
, folder01
,...
Here an exact solution using awk
: 这是使用
awk
的精确解决方案:
ls -1 | awk '
(NR-1) % 4000 == 0 {
dir = sprintf("folder%02d", ++nr)
system("mkdir -p " dir);
}
{ print >> dir "/file.txt"} '
There are already some good answers above, but I would also suggest you take a look at the watch
command. 上面已经有了一些很好的答案,但我也建议你看一下
watch
命令。 This will re-run a command every n seconds, so you can, well, watch the output. 这将每n秒重新运行一次命令,因此您可以观察输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.