Linux/Snippets: Difference between revisions

From Wiki
No edit summary
No edit summary
Line 15: Line 15:
</blockquote>
</blockquote>


 
== Processes ==
== File system & drives ==
Measure execution time
List directory sizes
<blockquote>
<blockquote>
<pre>
<pre>
clear;  for each in $(ls) ; do du -hs "$each" ; done
START=$(date +%s.%N)
command
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)
</pre>
</pre>
</blockquote>
</blockquote>




Measure execution time
== Files / Directories ==
List directory sizes
<blockquote>
<blockquote>
<pre>
<pre>
START=$(date +%s.%N)
clear;  for each in $(ls) ; do du -hs "$each" ; done
command
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)
</pre>
</pre>
</blockquote>
</blockquote>


find file with content
find file with content
Line 66: Line 65:
</blockquote>
</blockquote>


 
== HDD & SSD ==
 
show hdd power states
show hdd power states
<blockquote>
<blockquote>
Line 88: Line 86:
</pre>
</pre>
</blockquote>
</blockquote>


Manual SSD Trim
Manual SSD Trim

Revision as of 15:59, 15 July 2019

Hardware

Motherboard

dmidecode -t 2

Packages

List packages by size

dpkg-query -W --showformat='${Installed-Size}\t${Package}\n' | sort -nr

Processes

Measure execution time

START=$(date +%s.%N)
command
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)


Files / Directories

List directory sizes

clear;   for each in $(ls) ; do du -hs "$each" ; done

find file with content

grep -iR "$PATTERN" *

find top 10 biggest files

du -hsx * | sort -rh | head -10

clear file content

echo "" > filename
or
for i in *; do cat /dev/null > $i; done

rename directories with spaces to underlines

find -name "* *" -type d | rename 's/ /_/g'

HDD & SSD

show hdd power states

sudo hdparm -C /dev/sd?

show SMART data

sudo smartctl -a /dev/sd?

check for bad sectors

fsck.ext4 -cDfty -C 0 /dev/sdxx

Manual SSD Trim

sudo fstrim -v /   


Cron SSD Trim

/etc/scripts/fstrim.sh:
#!/bin/sh
#
echo `date +"%d %b %H:%M:%S"` | tr -d '\n' >> /var/log/fstrim
/sbin/fstrim -v / >> /var/log/fstrim
/etc/cron.d/fstrim:
#
0 * * * * root /etc/scripts/fstrim.sh

delete folder by folder until enough free space (1G)

cd /srv/motion
while [ `df | grep /dev/root | awk '{print $4}'` -lt 1000000 ]; do
ls -r | tail -1 | xargs rm -r
sleep 5
done

Shutdown if HDD idle

UPTIME=$(cat /proc/uptime | cut -d ' ' -f 1 | cut -d '.' -f 1)

if [ "$UPTIME" -lt "600" ]
  then exit
fi

lastSDB=$(cat /proc/diskstats | grep "sdb " | tr -s ' ' | cut -d ' ' -f 14 )
lastSDC=$(cat /proc/diskstats | grep "sdc " | tr -s ' ' | cut -d ' ' -f 14 )
lastSDD=$(cat /proc/diskstats | grep "sdd " | tr -s ' ' | cut -d ' ' -f 14 )

sleep 120

newSDB=$(cat /proc/diskstats | grep "sdb " | tr -s ' ' | cut -d ' ' -f 14 )
newSDC=$(cat /proc/diskstats | grep "sdc " | tr -s ' ' | cut -d ' ' -f 14 )
newSDD=$(cat /proc/diskstats | grep "sdd " | tr -s ' ' | cut -d ' ' -f 14 )

if [ "$lastSDB" = "$newSDB" ] && [ "$lastSDC" = "$newSDC" ] && [ "$lastSDD" = "$newSDD" ]
   then /sbin/shutdown -h now
   else echo "changes!!"
fi

Format ext4 with disabled lazy init

mkfs.ext4 -E lazy_itable_init=0,lazy_journal_init=0 /dev/sda1

kernel

remove old kernels

dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get --dry-run remove