Я хочу получить размер файла в переменную? Как сделать это?
ls -l | grep testing.txt | cut -f6 -d' '
дал размер, но как сохранить его в переменной оболочки?
вы можете сделать это с помощью ls
(проверьте значение -s на странице man)
$ var=$(ls -s1 testing.txt|awk '{print $1}')
Или вы можете использовать stat
с -... c '%s'
Или вы можете использовать find (GNU)
$ 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)