简体   繁体   English

Bash脚本循环可以计算当前目录中所有文件的文件大小?

[英]Bash script loop to count file size of all files in current directory?

Im very new to bash, and I have been given the task to write a bash script incorporating a loop that counts the byte size of all files in the current directory. 我对bash还是很陌生,我被赋予编写一个bash脚本的任务,该脚本包含一个循环,该循环对当前目录中所有文件的字节大小进行计数。

I want to use du but Ive been told he wants to see wc as the main command used. 我想使用du,但有人告诉我他想将wc用作主要命令。

As to how to make a loop accomplish these things, I'm unsure. 至于如何使循环完成这些事情,我不确定。 Can anyone give me an example or point me in the right direction? 谁能给我一个例子或指出正确的方向吗?

for fname in *; do
    your-way-to-display-size-of-file "$fname"
done

You need to gather several bits of bash know-how 您需要收集一些bash专有技术

to loop over files 遍历文件

for filename in * .[^.]*
do
    ...
done

to use the output of one command in another 在另一个命令中使用一个命令的输出

command $(other_command ...)

to sum numbers in bash 用bash求和

count=$(($count + 1))

to count the number of bytes in a file with wc 用wc计算文件中的字节数

wc -c $filename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Bash脚本:目录中的所有文件 - Bash Script: All files in directory Bash脚本根据名称将所有文件从当前目录传输到特定目录 - Bash script to transfer all files from current directory to specific directory based on name bash脚本重命名当前目录中的所有文件以及它在层次结构中位于其上方的所有目录? - bash script renaming all files in current directory as well as all the directories that are above it in hierarchy? 我如何编写一个 bash 脚本来遍历文件夹中的每个目录并将所有 .txt 文件附加到一个文件中? - How do i write a bash script to loop through each directory in a folder and append all .txt files into a single file? 如何遍历当前目录并将文件存储在数组中? Bash Shell脚本 - How do you loop through your current directory and store files in an array? Bash shell script bash循环处理目录中的所有文件 - bash loop processing all files in a directory Bash脚本-隐藏目录中的所有文件 - Bash script - make all files in Directory hidden bash脚本读取目录中的所有文件 - bash script read all the files in directory Bash脚本重命名目录中的所有文件 - Bash script to rename all files in a directory 循环播放bash中目录中具有特定名称的所有文件 - Loop for all files with certain name in directory in bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM