linux中du命令怎样进行脚本编写
导读:在Linux中,du命令用于计算文件和目录的磁盘使用情况 打开一个文本编辑器,如nano、vim或gedit。 编写以下脚本: #!/bin/bash # 检查参数数量 if [ $# -ne 1 ]; then echo "...
在Linux中,du
命令用于计算文件和目录的磁盘使用情况
- 打开一个文本编辑器,如
nano
、vim
或gedit
。 - 编写以下脚本:
#!/bin/bash
# 检查参数数量
if [ $# -ne 1 ];
then
echo "Usage: $0 <
directory>
"
exit 1
fi
directory=$1
# 检查给定的目录是否存在
if [ ! -d "$directory" ];
then
echo "Error: Directory $directory does not exist."
exit 1
fi
# 使用du命令计算目录的磁盘使用情况,并将结果保存到output.txt文件中
du -sh "$directory" >
output.txt
echo "Disk usage of $directory has been saved to output.txt"
- 保存脚本文件,例如命名为
du_script.sh
。 - 通过运行以下命令使脚本可执行:
chmod +x du_script.sh
- 现在你可以运行这个脚本来计算指定目录的磁盘使用情况。在终端中输入:
./du_script.sh /path/to/directory
这将把/path/to/directory
的磁盘使用情况保存到名为output.txt
的文件中。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: linux中du命令怎样进行脚本编写
本文地址: https://pptw.com/jishu/710966.html