linux外壳文件大小
我想将文件大小设置为变量?怎么做linux外壳文件大小,linux,shell,Linux,Shell,我想将文件大小设置为变量?怎么做 ls -l | grep testing.txt | cut -f6 -d' ' 给出了大小,但如何将其存储在shell变量中?您可以使用ls(查看手册页了解-s的含义) filesize=$(stat -c '%s' testing.txt) 或者您可以将stat与-c“%s” 或者您可以使用find(GNU) size(){ file=“$1” 如果[-b“$file”];则 /sbin/blockdev--getsize64“$file” 其他的 wc
ls -l | grep testing.txt | cut -f6 -d' '
给出了大小,但如何将其存储在shell变量中?您可以使用
ls
(查看手册页了解-s的含义)
filesize=$(stat -c '%s' testing.txt)
或者您可以将stat
与-c“%s”
或者您可以使用find(GNU)
size(){
file=“$1”
如果[-b“$file”];则
/sbin/blockdev--getsize64“$file”
其他的
wc-c<“$file”#处理像/proc/cpuinfo这样的伪文件
#stat--格式化%s“$文件”
#查找“$file”-printf“%s\n”
#du-b“$file”|剪切-f1
fi
}
fs=$(size testing.txt)
您可以使用命令wc
获得以字节为单位的文件大小,这在Linux系统中相当常见,因为它是
是的,这是bash shell,谢谢大家。可能的重复看起来像2011年提出的其他问题是的,但有更好的答案。更好的答案,是的。但这并不意味着这个问题是一年后创建的另一个问题的重复
wc-c
比依赖ls
行为或GNUfind
或stat
可用要好。请注意,wc-c对于/proc/cpuinfo之类的伪文件更准确。不能保证大小以空格分隔时在字段6中。这也是低效的,因为管道ls
intogrep
列出目录中的所有文件并对其名称进行排序(在一个包含数千个文件的目录中可能需要大量时间),此外,由于grep testing.txt
还返回名为not_testingatxt_file
(在正则表达式中,
表示匹配任何字符)。
size=`ls -l | grep testing.txt | cut -f6 -d' '`
$ var=$(ls -s1 testing.txt|awk '{print $1}')
$ var=$(find testing.txt -printf "%s")
size() {
file="$1"
if [ -b "$file" ]; then
/sbin/blockdev --getsize64 "$file"
else
wc -c < "$file" # Handles pseudo files like /proc/cpuinfo
# stat --format %s "$file"
# find "$file" -printf '%s\n'
# du -b "$file" | cut -f1
fi
}
fs=$(size testing.txt)
a=\`stat -c '%s' testing.txt\`;
echo $a
wc -c < file
FILESIZE=$(wc -c < file)
-c, --bytes
print the byte counts