'How To Get BIOS Serial Numbers On Linux'

The quickest way to get a BIOS serial number on Linux: sudo dmidecode -s system-serial-number That’s usually all you need. dmidecode reads the DMI/SMBIOS table and prints the serial number directly. Other options If dmidecode isn’t available or doesn’t return what you need: # lshw sudo lshw -C bios | grep serial # /sys class interface (no sudo needed) cat /sys/class/dmi/id/product_serial The /sys/class/dmi/id/ path is the cleanest option if you want to avoid sudo. It exposes several DMI fields: ...

Shutdown Proxmox VM using CLI

SSH into the Proxmox host and run: qm shutdown <VMID> This sends an ACPI shutdown signal to the guest OS for a graceful shutdown. To find the VM ID: qm list If the VM doesn’t respond to the shutdown signal, force it: qm stop <VMID> qm stop is equivalent to pulling the power cable. Use it only when qm shutdown hangs.

How to count words in a PDF from the Linux command line

The simplest approach: pdftotext input.pdf - | wc -w pdftotext is in the poppler-utils package (sudo apt install poppler-utils or sudo yum install poppler-utils). The - sends output to stdout instead of a file. Other options pdfminer: pdf2txt.py input.pdf | wc -w Install with pip install pdfminer.six. Python with PyPDF2: import PyPDF2 with open("input.pdf", "rb") as f: reader = PyPDF2.PdfReader(f) total = sum(len(page.extract_text().split()) for page in reader.pages) print(total) Note: PyPDF2’s PdfFileReader and getPage() are deprecated in newer versions — use PdfReader and index into reader.pages instead. ...

Remove lines matching a pattern from files recursively in Linux

find . -name "*.md" -type f -exec sed -i '/pattern/d' {} \; This one-liner walks the directory tree, finds every .md file, and strips out any line that matches the given pattern. The sed -i edits each file in place, and the {} gets swapped out for whatever path find hands it. Breaking it down find . -name "*.md" -type f does the walking. The -type f bit is important – without it, find would also match directories and pass them to sed, which would error out. The -exec ... \; at the end runs the command for each file individually. ...

Installing PhantomJS on Ubuntu 22.10

PhantomJS is a headless browser, which means it runs without a graphical interface and lets you automate web page interactions through scripting. I needed it for some automated testing at work and ran into a few things worth writing down, mostly because half the guides out there are either outdated or assume you already know what you’re doing. The version we’re installing is 2.1.1, which is the last release. The project has been unmaintained since 2017, but it still does what it needs to do for basic scraping and screenshot tasks. ...

Deleting files by content match in Linux

There are two ways to interpret this question, and the answer changes completely depending on which one you mean. Deleting files by filename If you want to nuke every file whose name contains a particular string: find . -type f -name '*string*' -delete The -type f keeps it from touching directories. The -delete flag removes whatever find matches. Simple enough. A word of caution: test without -delete first. Run the same command and replace it with -print to see what would actually get deleted. I’ve lost files to typos in wildcard patterns more times than I care to admit. ...

Replace all dots in filenames except the extension on Linux

Had a directory full of files with dots in the names and needed them all cleaned up. Something like my.report.final.pdf should become my_report_final.pdf, not my_report_final..pdf or worse. This one-liner does the job: for f in *; do pre="${f%.*}"; suf="${f##*.}"; mv -i -- "$f" "${pre//./_}.${suf}"; done Here’s what each part does. ${f%.*} strips everything from the last dot onwards, giving you the filename without its extension. ${f##*.} grabs just the extension by stripping everything up to and including the final dot. Then ${pre//./_} replaces every dot in the base name with an underscore. ...

Count directories in the current folder with a one-liner

A common question that comes up when you’re first getting comfortable with the command line: how do you count just the directories in your current folder? The quick answer is: ls -1 | wc -l This pipes a one-per-line listing of everything in the current directory into wc -l, which counts lines. Simple enough. But there’s a catch. This counts everything – files, symlinks, hidden items, the lot. If you only want directories, you need to be a bit more selective. ...

Count files in a directory with a one-liner

Another quick command line question: how do you count just the files in a directory, excluding subdirectories and other stuff? The classic answer: ls -l . | egrep -c '^-' This lists everything in long format (ls -l), then counts lines that start with a hyphen – which is how ls marks regular files. Directories show up as d, symlinks as l, and so on, so the regex filters them out. It works, but it’s a bit of a trick and relies on understanding ls output format. A more straightforward approach uses find: ...

'How much memory is actually free?'

I keep forgetting this one, so here it is. vmstat -s -SM | grep "free memory" | awk -F" " '{print$1}' It pipes vmstat output through grep and awk to pull just the number – no units, no labels, just the gigabytes sitting there doing nothing.

Preserving File Permissions When Copying in Linux

Use the -p flag with cp to preserve file permissions: cp -p /aaa/bbb /ccc/ddd

Identifying Your Linux Distribution

Not sure which Linux distribution you’re running? Open a terminal and type: cat /etc/issue On Debian 4.0, for example, this outputs: Debian GNU/Linux 4.0 \n \l The \n and \l are escape sequences for the hostname and terminal device – they’re not part of the distribution name. This works on most distributions. Some also have /etc/os-release with more detail: cat /etc/os-release