首页 > 系统相关 >Linux - Check If File Is Empty Or Not Using Shell Script

Linux - Check If File Is Empty Or Not Using Shell Script

时间:2023-07-21 15:36:13浏览次数:34  
标签:tmp file1 Shell file Script File Linux empty

Linux - Check If File Is Empty Or Not Using Shell Script

How do I check if a file is empty or not using bash or ksh shell script under a UNIX / Linux / macOS / OS X / BSD family of operating systems? How do I check if a file is empty in Bash?

You can use the find command and other options as follows. The -s option to the test builtin check to see if FILE exists and has a size greater than zero. It returns true and false values to indicate that file is empty or has some data. This page shows how to check if a file is empty in Bash shell running on a Linux or Unix-like operating systems.

touch /tmp/file1
ls -l /tmp/file1
find /tmp -empty -name file1

#Sample outputs:
/tmp/file1

Bash Script – Check If File is Empty or Not With the -s Option

touch /tmp/f1
echo "data" >/tmp/f2
ls -l /tmp/f{1,2}
[ -s /tmp/f1 ] 
echo $?
# output is 1
[ -s /tmp/f2 ]
echo $?
# out put is 0

# -s FILE   FILE exists and has a size greater than zero
https://www.computerhope.com/unix/test.htm

标签:tmp,file1,Shell,file,Script,File,Linux,empty
From: https://www.cnblogs.com/chenjo/p/17571491.html

相关文章

  • JavaScript | JavaScript介绍
    JavaScript起源JavaScript诞生于1995年,它的出现主要是用于处理网页中的前端验证。所的前端验证,就是指检查用户输入的内容是否符合一定的规则。比如:用户名的长度,密码的长度,邮箱的格式等。JavaScript简史JavaScript是由网景公司发明,起初命名为LiveScript,后来由于SUN......
  • Day 3: Shell条件语句和循环
    学习目标学习内容1.条件语句if语句case语句2.循环结构for循环while循环3.练习任务大树哥个人信息学习目标学习Shell中的条件语句,如if语句和case语句。理解循环结构,如for循环和while循环。练习编写脚本,包括条件判断和循环执行。学习内容今天我们将学习Shell中的条件语......
  • CentOS-Mysql 自动备份-shell 脚本
    功能说明:在服务器A上,每天自动运行一个shell脚本;备份数据库db;然后将sql文件放到另一台服务器B上。新建文件:mysql_backup.sh内容是:#!/bin/bashHOST=127.0.0.1USERNAME=rootPASSWORD=rootDBNAME=adverserverHost=123.123.123.123DATE=$(date+%Y%m%d)OLDDATE=$(date-d......
  • JavaScript(一)
    简介:JavaScript不学不行啊,ajax,前端交互,都需要的。一:书写位置1.内部位置 2.外部位置3.行内一般在框架里支持二:注释1.单行注释//这里是注释  快捷键:ctrl+/2.多行注释/*这里是多行注释*/快捷键:alt+shift+a 三:结束符每行的结束符是“;”现在发展的可用可不......
  • Linux shell | 竖线管道符号放变量 无法访问'|': 没有那个文件或目录 无法访问'wc':
    工作中遇到一个实际问题,文本编码转换。原始命令:catutf8.log|iconv-c-fUTF-8-tGBK|teegbk.log因为某种(方便修改、方便替换)原因,想把“|iconv-c-fUTF-8-tGBK|”命令这段,想放到变量里,做到一处修改处处生效的效果。pipe="|iconv-c-fUTF-8-tGBK|"catutf......
  • mssql的xp_cmdshell扩展
    introductionxp_cmdshellextension:storedprocedureexecutescommandstringasanoperatingsystemcommandinshell andretirevealloutputastextlinesdetermineifcurrentmssqlhasxp_cmdshellareturnvalueof1indicatestheextensionisxp_cmdsh......
  • TypeScript 学习笔记
    什么是TypeScript?TypeScript是JavaScript的一个超集,支持ECMAScript6标准,它可以编译成纯javaScript,可以运行在任何浏览器上。安装TypeScriptnpminstall-gtypescript安装完成后我们可以使用tsc命令来执行TypeScript的相关代码tsc-vVersion5.1.6新建一个a......
  • [极客大挑战 2019]Secret File
    [极客大挑战2019]SecretFile题目来源:buuctf题目类型:web涉及考点:代码审计1.题目让我们去找秘密,第一反应是检查源代码发现一个Archive_room.php,点击看看:出现了一个点击按钮,点击后如下:除此之外没有别的线索了2.我们依据提示回到上一个页面抓包发现了一个新的php,进去......
  • 抓取网页图片上的文字javascript
    抓取网页图片上的文字流程以下是实现“抓取网页图片上的文字”所需要的步骤和代码示例:步骤做什么代码示例1安装必要的库和工具2下载网页图片3图片预处理4使用OCR技术抓取文字步骤1:安装必要的库和工具在进行文字抓取前,首先需要安装几个必要的库和......
  • No lockfile in this directory. Run `yarn install` to generate one.
    如何解决"Nolockfileinthisdirectory.Runyarninstalltogenerateone."错误介绍在使用Yarn进行JavaScript项目开发时,有时候会遇到一个错误信息:"Nolockfileinthisdirectory.Runyarninstalltogenerateone."这个错误通常是由于项目缺少yarn.lock文件导致......