Linux/Snippets: Difference between revisions
< Linux
(Die Seite wurde neu angelegt: „List directory sizes <blockquote> <pre> clear; for each in $(ls) ; do du -hs "$each" ; done </pre> </blockquote> Manual SSD Trim <blockquote> <pre> sudo fs…“) |
|||
(39 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Hardware == | |||
* Motherboard | |||
<blockquote> | |||
<pre> | |||
dmidecode -t 2 | |||
</pre> | |||
</blockquote> | |||
* HDD | |||
<blockquote> | |||
<pre> | |||
fdisk -l | |||
lsblk | |||
smartctl -a /dev/sda | |||
lshw -c disk | |||
</pre> | |||
</blockquote> | |||
== Packages == | |||
List packages by size | |||
<blockquote> | |||
<pre> | |||
dpkg-query --show --showformat='${Package;-50}\t${Installed-Size} ${Status}\n' | sort -k 2 -n | grep -v deinstall | |||
</pre> | |||
</blockquote> | |||
Sum of all installed packages | |||
<blockquote> | |||
<pre> | |||
dpkg-query --show --showformat='${Package;-50}\t${Installed-Size} ${Status}\n' | sort -k 2 -n | grep -v deinstall | awk '{s+=$2} END {printf "%.0f\n", s}' | |||
</pre> | |||
</blockquote> | |||
remove old kernels | |||
<blockquote> | |||
<pre> | |||
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 | |||
</pre> | |||
</blockquote> | |||
purge all removed packages | |||
<blockquote> | |||
<pre> | |||
dpkg --list | grep "^rc" | cut -d " " -f 3 | xargs sudo dpkg --purge | |||
</pre> | |||
</blockquote> | |||
== Processes == | |||
Measure execution time | |||
<blockquote> | |||
<pre> | |||
START=$(date +%s.%N) | |||
command | |||
END=$(date +%s.%N) | |||
DIFF=$(echo "$END - $START" | bc) | |||
</pre> | |||
</blockquote> | |||
== Wake-up and Shutdown == | |||
* shutdown and wake tomorrow | |||
<blockquote> | |||
<pre> | |||
sudo rtcwake -m off -t $(data --date='1 day 6:00' +%s) | |||
</pre> | |||
</blockquote> | |||
== Files / Directories == | |||
Current script dir | |||
<blockquote> | |||
<pre> | |||
currentpath=$(dirname "$0") | |||
cd $currentpath | |||
</pre> | |||
</blockquote> | |||
List directory sizes | List directory sizes | ||
<blockquote> | <blockquote> | ||
<pre> | <pre> | ||
du -sh * | |||
clear; for each in $(ls) ; do du -hs "$each" ; done | clear; for each in $(ls) ; do du -hs "$each" ; done | ||
clear; for each in $(ls) ; do du -hs "$each" ; done | sort -hr | |||
</pre> | |||
</blockquote> | |||
find file with content | |||
<blockquote> | |||
<pre> | |||
grep -iR "$PATTERN" * | |||
</pre> | |||
</blockquote> | |||
find top 10 largest directories | |||
<blockquote> | |||
<pre> | |||
du -hsx * | sort -rh | head -10 | |||
h human readable | |||
s summarize | |||
x one-file-system | |||
r reverse | |||
h human-numeric-sort | |||
</pre> | |||
</blockquote> | |||
find files bigger than 100MB | |||
<blockquote> | |||
<syntaxhighlight lang="bash"> | |||
find . -type f -size +100M | |||
find . -type f -size +1G | |||
</syntaxhighlight> | |||
</blockquote> | |||
find empty folders | |||
<blockquote> | |||
<syntaxhighlight lang="bash"> | |||
find . -type d -empty -print | |||
find . -type d -empty -delete | |||
</syntaxhighlight> | |||
</blockquote> | |||
clear file content | |||
<blockquote> | |||
<pre> | |||
echo "" > filename | |||
or | |||
for i in *; do cat /dev/null > $i; done | |||
</pre> | |||
</blockquote> | |||
rename directories with spaces to underlines | |||
<blockquote> | |||
<pre> | |||
find -name "* *" -type d | rename 's/ /_/g' | |||
</pre> | |||
</blockquote> | |||
== HDD & SSD == | |||
show hdd power states | |||
<blockquote> | |||
<pre> | |||
sudo hdparm -C /dev/sd? | |||
</pre> | </pre> | ||
</blockquote> | </blockquote> | ||
set hdd spindown (rc.local) | |||
<blockquote> | |||
<pre> | |||
hdparm -B 250 /dev/sd? | |||
hdparm -S 59 /dev/sd? | |||
</pre> | |||
</blockquote> | |||
show SMART data | |||
<blockquote> | |||
<pre> | |||
sudo smartctl -a /dev/sd? | |||
</pre> | |||
</blockquote> | |||
check for bad sectors | |||
<blockquote> | |||
<pre> | |||
fsck.ext4 -cDfty -C 0 /dev/sdxx | |||
</pre> | |||
</blockquote> | |||
Manual SSD Trim | Manual SSD Trim | ||
Line 33: | Line 193: | ||
</pre> | </pre> | ||
</blockquote> | </blockquote> | ||
delete folder by folder until enough free space (1G) | |||
<blockquote> | |||
<pre> | |||
cd /srv/motion | |||
while [ `df | grep /dev/root | awk '{print $4}'` -lt 1000000 ]; do | |||
ls -r | tail -1 | xargs rm -r | |||
sleep 5 | |||
done | |||
</pre> | |||
</blockquote> | |||
Shutdown computer if HDDs idle | |||
<blockquote> | |||
<pre> | |||
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 | |||
</pre> | |||
</blockquote> | |||
Format ext4 with disabled lazy init | |||
<blockquote> | |||
<pre> | |||
mkfs.ext4 -E lazy_itable_init=0,lazy_journal_init=0 /dev/sda1 | |||
</pre> | |||
</blockquote> | |||
== Multimedia == | |||
* yt-dlp | |||
** https://github.com/yt-dlp/yt-dlp | |||
<blockquote> | |||
<pre> | |||
yt-dlp --no-keep-video --extract-audio --audio-format mp3 --audio-quality 0 https://www.youtube.com/watch? | |||
</pre> | |||
</blockquote> | |||
[[Category:Linux/Bash]] | |||
[[Category:Linux]] |
Latest revision as of 16:40, 6 September 2024
Hardware
- Motherboard
dmidecode -t 2
- HDD
fdisk -l lsblk smartctl -a /dev/sda lshw -c disk
Packages
List packages by size
dpkg-query --show --showformat='${Package;-50}\t${Installed-Size} ${Status}\n' | sort -k 2 -n | grep -v deinstall
Sum of all installed packages
dpkg-query --show --showformat='${Package;-50}\t${Installed-Size} ${Status}\n' | sort -k 2 -n | grep -v deinstall | awk '{s+=$2} END {printf "%.0f\n", s}'
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
purge all removed packages
dpkg --list | grep "^rc" | cut -d " " -f 3 | xargs sudo dpkg --purge
Processes
Measure execution time
START=$(date +%s.%N) command END=$(date +%s.%N) DIFF=$(echo "$END - $START" | bc)
Wake-up and Shutdown
- shutdown and wake tomorrow
sudo rtcwake -m off -t $(data --date='1 day 6:00' +%s)
Files / Directories
Current script dir
currentpath=$(dirname "$0") cd $currentpath
List directory sizes
du -sh * clear; for each in $(ls) ; do du -hs "$each" ; done clear; for each in $(ls) ; do du -hs "$each" ; done | sort -hr
find file with content
grep -iR "$PATTERN" *
find top 10 largest directories
du -hsx * | sort -rh | head -10 h human readable s summarize x one-file-system r reverse h human-numeric-sort
find files bigger than 100MB
find . -type f -size +100M find . -type f -size +1G
find empty folders
find . -type d -empty -print find . -type d -empty -delete
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?
set hdd spindown (rc.local)
hdparm -B 250 /dev/sd? hdparm -S 59 /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 computer if HDDs 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
Multimedia
yt-dlp --no-keep-video --extract-audio --audio-format mp3 --audio-quality 0 https://www.youtube.com/watch?