파일 크기를 확인하는 방법?
0 크기를 확인하는 스크립트가 있지만 대신 파일 크기를 확인하는 더 쉬운 방법이 있어야한다고 생각했습니다. 즉, file.txt일반적으로 100k입니다. 스크립트가 90k 미만 (0 포함) 미만인지 확인하고이 경우 파일이 손상되어 새 사본을 얻는 방법.
내가 현재 사용중인 것 ..
if [ -n file.txt ]
then
echo "everything is good"
else
mail -s "file.txt size is zero, please fix. " myemail@gmail.com < /dev/null
# Grab wget as a fallback
wget -c https://www.server.org/file.txt -P /root/tmp --output-document=/root/tmp/file.txt
mv -f /root/tmp/file.txt /var/www/file.txt
fi
[ -n file.txt ]크기를 확인하지 않고 문자열의 file.txt길이가 0 이 아닌지 확인 하므로 항상 성공합니다.
당신이 말하고 싶은 경우 "크기가 제로가 아닌"다음이 필요합니다 [ -s file.txt ].
파일 크기 wc -c를 얻으려면 크기 (파일 길이)를 바이트 단위로 가져 오는 데 사용할 수 있습니다 .
file=file.txt
minimumsize=90000
actualsize=$(wc -c <"$file")
if [ $actualsize -ge $minimumsize ]; then
echo size is over $minimumsize bytes
else
echo size is under $minimumsize bytes
fi
이 경우에는 원하는 것 같습니다.
그러나 참고로, 파일이 사용하는 디스크 공간을 알고 싶다면 du -k킬로바이트 단위로 크기 (사용 된 디스크 공간)를 얻는 데 사용할 수 있습니다 .
file=file.txt
minimumsize=90
actualsize=$(du -k "$file" | cut -f 1)
if [ $actualsize -ge $minimumsize ]; then
echo size is over $minimumsize kilobytes
else
echo size is under $minimumsize kilobytes
fi
출력 형식을보다 세밀하게 제어해야하는 경우을 참조하십시오 stat. Linux에서는으로 시작하고 stat -c '%s' file.txtBSD / Mac OS X에서는으로 시작합니다 stat -f '%z' file.txt.
아무도 stat파일 크기를 확인 하지 않았다는 것이 놀랍습니다 . 일부 방법은 확실히 더 낫습니다. -s파일이 비어 있는지 여부를 확인하는 것이 원하는 경우 다른 것보다 쉽습니다. 그리고 당신이 크기의 파일 을 찾으 려면 find확실히 갈 길입니다.
나는 또한 duKB로 파일 크기를 얻는 것을 많이 원하지만 바이트의 경우 다음을 사용합니다 stat.
size=$(stat -f%z $filename) # BSD stat
size=$(stat -c%s $filename) # GNU stat?
awk와 이중 괄호가있는 대체 솔루션
FILENAME=file.txt
SIZE=$(du -sb $FILENAME | awk '{ print $1 }')
if ((SIZE<90000)) ; then
echo "less";
else
echo "not less";
fi
If your find handles this syntax, you can use it:
find -maxdepth 1 -name "file.txt" -size -90k
This will output file.txt to stdout if and only if the size of file.txt is less than 90k. To execute a script script if file.txt has a size less than 90k:
find -maxdepth 1 -name "file.txt" -size -90k -exec script \;
If you are looking for just the size of a file:
$ cat $file | wc -c
> 203233
This works in both linux and macos
function filesize
{
local file=$1
size=`stat -c%s $file 2>/dev/null` # linux
if [ $? -eq 0 ]
then
echo $size
return 0
fi
eval $(stat -s $file) # macos
if [ $? -eq 0 ]
then
echo $st_size
return 0
fi
return -1
}
For getting the file size in both Linux and Mac OS X (and presumably other BSDs), there are not many options, and most of the ones suggested here will only work on one system.
Given f=/path/to/your/file,
what does work in both Linux and Mac's Bash:
size=$( perl -e 'print -s shift' "$f" )
or
size=$( wc -c "$f" | awk '{print $1}' )
The other answers work fine in Linux, but not in Mac:
dudoesn't have a-boption in Mac, and the BLOCKSIZE=1 trick doesn't work ("minimum blocksize is 512", which leads to a wrong result)cut -d' ' -f1doesn't work because on Mac, the number may be right-aligned, padded with spaces in front.
So if you need something flexible, it's either perl's -s operator , or wc -c piped to awk '{print $1}' (awk will ignore the leading white space).
And of course, regarding the rest of your original question, use the -lt (or -gt) operator :
if [ $size -lt $your_wanted_size ]; then etc.
python -c 'import os; print (os.path.getsize("... filename ..."))'
portable, all flavours of python, avoids variation in stat dialects
stat appears to do this with the fewest system calls:
$ set debian-live-8.2.0-amd64-xfce-desktop.iso
$ strace stat --format %s $1 | wc
282 2795 27364
$ strace wc --bytes $1 | wc
307 3063 29091
$ strace du --bytes $1 | wc
437 4376 41955
$ strace find $1 -printf %s | wc
604 6061 64793
Based on gniourf_gniourf’s answer,
find "file.txt" -size -90k
will write file.txt to stdout if and only if the size of file.txt is less than 90K, and
find "file.txt" -size -90k -exec command \;
will execute the command command if file.txt has a size less than 90K. I have tested this on Linux. From find(1),
… Command-line arguments following (the
-H,-Land-Poptions) are taken to be names of files or directories to be examined, up to the first argument that begins with ‘-’, …
(emphasis added).
ls -l $file | awk '{print $6}'
assuming that ls command reports filesize at column #6
I would use du's --threshold for this. Not sure if this option is available in all versions of du but it is implemented in GNU's version.
Quoting from du(1)'s manual:
-t, --threshold=SIZE
exclude entries smaller than SIZE if positive, or entries greater
than SIZE if negative
Here's my solution, using du --threshold= for OP's use case:
THRESHOLD=90k
if [[ -z "$(du --threshold=${THRESHOLD} file.txt)" ]]; then
mail -s "file.txt size is below ${THRESHOLD}, please fix. " myemail@gmail.com < /dev/null
mv -f /root/tmp/file.txt /var/www/file.txt
fi
The advantage of that, is that du can accept an argument to that option in a known format - either human as in 10K, 10MiB or what ever you feel comfortable with - you don't need to manually convert between formats / units since du handles that.
For reference, here's the explanation on this SIZE argument from the man page:
The SIZE argument is an integer and optional unit (example: 10K is
10*1024). Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers
of 1000). Binary prefixes can be used, too: KiB=K, MiB=M, and so on.
Okay, if you're on a Mac, do this: stat -f %z "/Users/Example/config.log" That's it!
참고URL : https://stackoverflow.com/questions/5920333/how-to-check-size-of-a-file
'Programming' 카테고리의 다른 글
| Git On Custom SSH 포트 (0) | 2020.06.29 |
|---|---|
| URL 해시 위치 가져 오기 및 jQuery에서 사용 (0) | 2020.06.29 |
| npm 전역 경로 접두사 (0) | 2020.06.29 |
| Python 용 Selenium WebDriver로 페이지가로드 될 때까지 기다리십시오. (0) | 2020.06.29 |
| 자산 디렉토리에서 Webview로드 HTML (0) | 2020.06.29 |