find . -type f ! -name "*.?*" -delete
Category: Linux
Quick way to make HDD light blink
A quick and dirty way to make a specific hard drive LED blink so that it can be identified.
while true; do smartctl -a /dev/<device>; done
The simple trick is to run smartctl in a while loop. The command will run forever until you Ctrl + C. It make the LED blink rather obviously, which made things much easier.
Other method :
To start: ledctl locate=/dev/sdd
To stop: ledctl off={/dev/sdd}
This lives in the ledmon package.
Find files containing a specific text string
grep -rnlw '/path/to/dir/' -e 'pattern'
-ror-Ris recursive,-nis line number, and-wstands for match the whole word.-lcan be added to just give the file name of matching files.-eis the pattern used during the search
Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/search/' -e "pattern" - This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/search/' -e "pattern" - For directories it’s possible to exclude one or more directories using the
--exclude-dirparameter. For example, this will exclude the dirsdir1/,dir2/and all of them matching*.dst/:grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"
Find all the symlinks in a directory tree
This will recursively traverse the /path/to/folder directory and list only the symbolic links:
ls -lR /path/to/folder | grep '^l'
If your intention is to follow the symbolic links too, you should use your find command but you should include the -L option; in fact the find man page says:
-L Follow symbolic links. When find examines or prints information
about files, the information used shall be taken from the prop‐
erties of the file to which the link points, not from the link
itself (unless it is a broken symbolic link or find is unable to
examine the file to which the link points). Use of this option
implies -noleaf. If you later use the -P option, -noleaf will
still be in effect. If -L is in effect and find discovers a
symbolic link to a subdirectory during its search, the subdirec‐
tory pointed to by the symbolic link will be searched.
When the -L option is in effect, the -type predicate will always
match against the type of the file that a symbolic link points
to rather than the link itself (unless the symbolic link is bro‐
ken). Using -L causes the -lname and -ilname predicates always
to return false.
Then try this:
find -L /var/www/ -type l
This will probably work: I found in the find man page this diamond: if you are using the -type option you have to change it to the -xtype option:
l symbolic link; this is never true if the -L option or the
-follow option is in effect, unless the symbolic link is
broken. If you want to search for symbolic links when -L
is in effect, use -xtype.
Then:
find -L /var/www/ -xtype l
Upgrade Debian to the latest version
Stop critical services running on the host machine
systemctl stop XXX.service
Update the current running system to minimize the update gap :
apt update && apt full-upgrade
Remplace all occurrences of the old release (bookworm) with the new one (trixie) in the APT sources file :
sed -i 's/bookworm/trixie/g' /etc/apt/sources.list
sed -i 's/bookworm/trixie/g' /etc/apt/sources.list.d/*.list
Update the APT repository with the new packages list. You will get an estimate of the number of packages that will be upgraded.
apt update
Proceed with upgrading the packages. This step is the most time-consuming since it downloads the new packages. A list of important changes will be displayed, read it carefully. You can read through with Space and escape with Q. During the upgrade, you might be asked if you want to restart running services, you cant answer with Y.
apt full-upgrade
Once everything is up-to-date, restart the host :
reboot
After restarting
Confirm you are running the new OS version :
cat /etc/os-release
Post-install cleanup
Upgrade the source files to the new deb822 format, confirm with Y :
apt modernize-sources
List obsolete packages :
apt list '~o'
Attention, some packages installed outside official APT repos might be treated as obsolete. Double-check these carefully before purging obsolete packages :
apt purge '~o'
apt autoremove
Debian versions
See https://www.debian.org/releases/ for current information.
| Version | Code Name | Release Date | End of Life |
| 13 | Trixie | 2025-08-09 | 2028-08-09 |
| 12 | Bookworm | 2023-06-10 | 2026-06-10 |
| 11 | Bullseye | 2021-08-14 | 2024-08-14 |
| 10 | Buster | 2019-07-06 | 2022-09-10 |
Obsolete releases :
apt-key deprecation in Raspberry Pi OS Bookworm (12)
First find the key in the old location
apt-key list | grep -A4 "trusted.gpg$"
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
/etc/apt/trusted.gpg
pub rsa2048 2012-06-17 [SC]
CF8A 1AF5 02A2 AA2D 763B AE7E 82B1 2992 7FA3 303E
uid [ unknown] Raspberry Pi Archive Signing Key
What you need from this is the last 8 characters of the long hexadecimal number, without the spaces. In this case, 7FA3303E. Then you export the key to a temporary file.
apt-key export 7FA3303E | sudo gpg --dearmor -o /tmp/raspi.gpg
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
This should have created a file /tmp/raspi.gpg with the key in.
file /tmp/raspi.gpg
/tmp/raspi.gpg: PGP/GPG key public ring (v4) created Sun Jun 17 15:49:51 2012 RSA (Encrypt or Sign) 2048 bits MPI=0xabc2a41a70625f9f…
Now delete the old key.
apt-key del 7FA3303E
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK
And finally put the new key in place.
Code: Select all
mv /tmp/raspi.gpg /etc/apt/trusted.gpg.d/raspberrypi-bookworm-stable.gpg
The reason for doing the steps in this order is that otherwise the new key gets deleted as well as the old one.
Rotate DSI Display
For Raspberry Pi OS Bookworm :
Add the video information to the end of cmdline.txt :
(located at /boot/firmware/cmdline.txt or on the /boot partition of the SD Card.)
video=DSI-1:800x480@60,rotate=<rotation-value>
Replace the <rotation-value> placeholder with one of the following values, which correspond to the degree of rotation relative to the default on your display: 0 (default), 90, 180 (reversed), 270
(https://www.raspberrypi.com/documentation/accessories/display.html)
For Raspberry Pi OS Buster / Bullseye :
Add one of the following values in config.txt
(located at /boot/firmware/cmdline.txt or on the /boot partition of the SD Card.)
lcd_rotate=0 # default (normal landscape)
lcd_rotate=1 # 90 degrees (portrait, upside down)
lcd_rotate=2 # 180 degrees (landscape)
lcd_rotate=3 # 270 degrees (portrait, upside down)
Change Static IP Address
Enterprise Linux 9 / Raspberry Pi OS 12 :
sudo nmtui
Or
su
# list devices (ens192 used as example below)
nmcli device
# set [manual] for static setting or [auto] for DHCP
nmcli connection modify ens192 ipv4.method manual
nmcli connection modify ens192 ipv4.addresses xx.xx.xx.xx/24
nmcli connection modify ens192 ipv4.gateway xx.xx.xx.1
# for multiple DNS, specify with space separated
nmcli connection modify ens192 ipv4.dns "xx.xx.xx.1 xx.xx.xx.2"
# restart the interface to reload settings
nmcli connection down ens192; nmcli connection up ens192
# confirm the applied settings
nmcli device show ens192
# confirm state
ip address show
Debian 12 :
su
ip a
nano /etc/network/interfaces
# restart the interface to reload settings
systemctl restart networking
# confirm state
systemctl status networking
Raspbian 10 / Raspberry Pi OS 11 :
sudo nano /etc/dhcpcd.conf
Mount VMWare VMFS Volumes in Linux
Install the vmfs pckages :
sudo apt install vmfs6-tools
Mount the partition sdX1 using FUSE :
vmfs6-fuse /dev/sdX1 /mnt/media
About VMWare VMFS’ hidden files
There are hidden files in the root of any VMFS volume which start with a dot and end with the extension .sf, and some free space seems to be missing from a newly created VMFS datastore.
These system files are indexes and descriptors of the files on the VMFS filesystem and cannot be deleted.
- They may take more space if you have a small block size (more addresses).
- They may take less space if you have a large block size (less addresses).
- They are metadata files that are created when the volume is first set up.
- They cannot be changed and cannot be deleted.
- They may grow over time but not significantly.
The VMFS-3 file system organizes all space on disk in collections of resources. There are four resource types managed by the VMFS-3 file system: Blocks, sub-blocks, pointer blocks, and file descriptors.
Resources are grouped together into collections called CLUSTERs and clusters can be further grouped together into CLUSTER GROUPS.
Each resource type is managed by one of several system files which are created during the file system bootstrap process:
- fbb.sf – file block bitmap.system file
- fdc.sf – file descriptor cluster.system file
- pbc.sf – pointer block cluster.system file
- sbc.sf – sub-block cluster.system file
- vh.sf – vmfs heartbeat.system file
- sdd.sf – scsi device description.system file
The VMFS-5 file system uses one additional system file:
- pb2.sf – pointer block 2.system file
Prevent .DS_Store on Samba shares
Open up /etc/samba/smb.conf and add the following lines to each share :
veto files = /._*/.DS_Store/
delete veto files = yes
restart samba :
systemctl restart smbd.service
If neede, cleanup the server of unwanted .DS_Store files :
find ./ -type f -name ".DS_Store" -exec rm -f {} \;
or
find ./ ( -name ".DS_Store" -or -name "._" -or -name "Thumbs.db" -or -name ".tmp" -or -name "*.lnk" -or -name "TemporaryItems" -or -name "folders.501" -or -name ".TemporaryItems" -or -name "__MACOSX" ) -ls -delete
Cleanup old kernels
EL 7 :
sudo package-cleanup --oldkernels --count=2
EL 8, EL 9 :
sudo dnf -y remove --oldinstallonly --setopt installonly_limit=2 kernel
Debian 12 and up :
sudo apt autoremove --purge
sudo dpkg -S /lib/modules/*
sudo rm -rd /lib/modules/
dpkg -S /lib/modules/* 2>&1 | grep -v "no path found matching pattern" | awk '{ print $NF}' | xargs -I % echo %
Create archive excluding elements
tar --exclude='file1.txt' --exclude='folder1' -pvczf backup.tar.gz ./
Empty log files
cd dir
truncate -s 0 ./*.log
Creating a Proxmox EL9 VM
The CPU type must be ‘host’; otherwise the VM will kernel panic while booting the installer ISO.
That’s because EL9 now requires CPUs to be compatible with the x86-64 v2 instruction set, which the default Qemu x64 exposed vCPU doesn’t support.
Find & delete files
find /path/to/dir -args
Delete empty directories
find /path/to/dir -empty -type d -delete
Delete empty files
find /path/to/dir -empty -type f -delete
Count all empty files or directories
Count empty dirs only
find /path/to/dir/ -empty -type d | wc -l
Count empty files only
find /path/to/dir/ -empty -type f | wc -l
Where :
- -empty : Only find empty files and make sure it is a regular file or a directory.
- -type d : Only match directories.
- -type f : Only match files.
- -delete : Delete files. Always put -delete option at the end of find command as find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.
This is useful when you need to clean up empty directories and files in a single command.
Cleanup unnecessary files and Desktop OSses clutter
cd "$SOURCEDIR" find ./ \( -name ".DS_Store" -or -name "._*" -or -name "Thumbs.db" -or -name "*.tmp" -or -name "*.lnk" -or -name "TemporaryItems" -or -name "folders.501" -or -name ".TemporaryItems" -or -name "__MACOSX" \) -ls -delete
Change ownership of files owned by a specific username
find /path/to/dir/ -user username -exec chown newuser:newgroup {} +
Reset permissions of files
find ./ -type d -exec chmod 0755 {} +
find ./ -type f -exec chmod 0644 {} +
Reload udev rules without restarting udev
sudoudevadm control --reload-rules
Configure WPA Enterprise Wi-Fi connection on Raspbian Buster (10)
Add the following to /etc/wpa_supplicant/wpa_supplicant.conf :
eapol_version=2
network={
ssid="SSID_HERE"
proto=RSN
key_mgmt=WPA-EAP
pairwise=CCMP
auth_alg=OPEN
eap=PEAP
identity="USERNAME_HERE"
password=hash:#####################
phase1="peaplabel=0"
phase2="auth=MSCHAPV2"
}
and change /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant :
seach for this string in wpa_supplicant_start() function :
nl80211,wext
and replace the drivers’ order with
wext,nl80211
Check if the wlan0 interface gets an IP address :
ip a
Play sound with PHP on a Raspberry Pi
Configure the sound output interface :
sudo raspi-config
Option 1 System Options
Option S2 Audio
Install the necessary software : Apache HTTPd, PHP 7 and the PHP Apache module :
sudo apt install apache2 php libapache2-mod-php
Allow Apache to access audio devices by adding its user to the audio group :
sudo usermod -a -G audio www-data
in the PHP script, execute the aplay command :
<?php exec('aplay '.DIR.'/sound.wav'); ?>
Installing a data HDD above 2.2TB
Find out what the disk device is with :
sudo df -Th
sudo fdisk -l
sudo parted -l
You should look for one that’s the same size as the disk you installed and one that’s not listed in the output of df -Th.
Make a directory to mount the drive on and change its owner :
sudo mkdir /Volumes/somename && sudo chown -R damien /Volumes/somename
Partition the disk using parted, to make sure that the partition table is GPT :
sudo parted -a optimal /dev/sdX
(parted) mklabel gpt
(parted) unit TB
(parted) mkpart primary 0% 100%
(parted) quit
Format the new disk as ext4 :
sudo mkfs.ext4 -L LABEL -m 0 -T largefile /dev/sdX1
-T for type, largefile is one inode per MB
-m 0 to free up the reserved superblocks, can also be done later with :
sudo tune2fs -m 0 /dev/sdX1
Edit /etc/fstab and add the following line to the end :
/dev/sdX1 /Volumes/somename ext3 defaults 0 0
It is recommended using the device’s UUID in the fstab, so that even if the name changes when detecting hardware at boot, the disk is still correctly identified and mounted as the correct volume.
If you don’t know the UUID of the disk, you can find it with :
sudo blkid /dev/sdb1
Mount it so that it can be used :
sudo mount /Volumes/somename
Create a file with specific date
touch -m -t 201912301655 2019-12-30_file.txt
Useful Linux commands
1- Save man-page as pdf
man -t awk | ps2pdf - awk.pdf
2- Duplicate installed packages from one machine to the other (RPM-based systems)
ssh root@remote.host "rpm -qa" | xargs yum -y install
3- Stamp a text line on top of the pdf pages to quickly add some remark, comment, stamp text, … on top of (each of) the pages of the input pdf file
echo "This text gets stamped on the top of the pdf pages." | enscript -B -f Courier-Bold16 -o- | ps2pdf - | pdftk input.pdf stamp - output output.pdf
4- Display the number of connections to a MySQL Database
Count the number of active connections to a MySQL database.
The MySQL command “show processlist” gives a list of all the active clients.
However, by using the processlist table, in the information_schema database, we can sort and count the results within MySQL.
mysql -u root -p -BNe "select host,count(host) from processlist group by host;" information_schema
5- Create a local compressed tarball from remote host directory
ssh user@host "tar -zcf - /path/to/dir" > dir.tar.gz
This improves on #9892 by compressing the directory on the remote machine so that the amount of data transferred over the network is much smaller. The command uses ssh(1) to get to a remote host, uses tar(1) to archive and compress a remote directory, prints the result to STDOUT, which is written to a local file. In other words, we are archiving and compressing a remote directory to our local box.
6- tail a log over ssh
This is also handy for taking a look at resource usage of a remote box.
ssh -t remotebox "tail -f /var/log/remote.log"
7- Print diagram of user/groups
Parses /etc/group to “dot” format and pases it to “display” (imagemagick) to show a usefull diagram of users and groups (don’t show empty groups).
awk 'BEGIN{FS=":"; print "digraph{"}{split($4, a, ","); for (i in a) printf "\"%s\" [shape=box]\n\"%s\" -> \"%s\"\n", $1, a[i], $1}END{print "}"}' /etc/group|display
8- Draw kernel module dependancy graph.
Parse `lsmod’ output and pass to `dot’ drawing utility then finally pass it to an image viewer
lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -
9- Create strong, but easy to remember password
Why remember? Generate!
Up to 48 chars, works on any unix-like system
read -s pass; echo $pass | md5sum | base64 | cut -c -16
10- Find all files larger than 500M and less than 1GB
find / -type f -size +500M -size -1G
11- Find size of directories and files in current directory
du -h -d 1
12- Limit the cpu usage of a process
This will limit the average amount of CPU it consumes.
sudo cpulimit -p pid -l 50
13- Show the difference between two dirs
diff -qr /var/some/dir1 var/some/dir2
