Deth

Fitness and everything else

FreeBSD Command Line Tips

This will be just a few useful command line tips or commands that I learn and find useful that I don’t use often enough to remember but when I do need them it’ll be handy to have things in one place. Maybe I’ll actually be able to remember them someday.

Remove location data from EXIF. It’s useful to have the location but sometimes you just don’t want it to show up like on my public website. This requires that the p5-Image-ExifTool-11.70 port be installed

exiftool -gps:all=

Files

Find files by name.. for example this finds .core core dump files in the ~ directory

find ~ -name '*.core'

Find the location of a file my name. This example shows the path to the file named FILENAME

whereis FILENAME

Find all files larger than a certain size. This came in handy when my disk was nearly full. This command below searches for files over 100 megabytes in size.

find . -type f -size +100M -exec du -m {} \; | sort -n

Before I discovered that rsync can do local drives or even thought of trying it I’d copy and paste. This makes it so much easier to do and more fail safe to boot.

rsync -vru --delete --ignore-existing --no-owner --no-group --no-perms ~/Music/flac/* /usb

Find duplicate files. The fdupes utility is great for this but it is not included in the FreeBSD base system so first it must be installed

pkg install fdupes

Once it’s installed you can run it. I like to use it with these settings where it’s interactive and only shows one set of dupes at a time. Be careful with it because it will happily delete stuff even if you copied it on purpose. (I didn’t accidentally delete the copies of pictures for my hugo site from the assets directory or anything like that.. )

fdupes -r -d -G 1 -P ~/

Find out more information about type of file one is if you don’t know what it is. Below are a couple of examples

file -k ~/Downloads/wx2024.csv 

returns: /home/carl/Downloads/wx2024.csv: CSV text\012- , Unicode text, UTF-8 text, with very long lines (1471)

file -k ~/Downloads/home.tar.gz 

Returns: /home/carl/Downloads/home.tar.gz: gzip compressed data, last modified: Mon Jan 26 14:05:02 2015, max compression, from Unix, original size modulo 2^32 144977920\012- data

File System

show used and free disk space on all file systems

df -h

Show all partitions and filesytem types on them.

gpart show

Text Files

Find and list occurrences of a string in files. This example searches all files in the content/posts directory for the word FreeBSD

grep -rnw "Freebsd" content/posts/*

Find files that do not contain a string and then open them all in an editor. In my case I’m using kate.

kate $(\grep -L 'FreeBSD' content/posts/* .)

Clear a text file. Once in a while I just want to quickly clear at text file and this does the trick. This example clears all text from the /var/log/messages file

cat /dev/null > /var/log/messages

Show the last 10 lines of a file then continue waiting for more text to be appended to the file. Very useful for log files sometimes. This watches the /var/log/messages file

tail -f /var/log/messages

Use Aspell to spellcheck all markdown files in a folder

find content/posts/ -type f -name "*.md" -exec aspell check {} \;	

Replace text in multiple files from the command line. This uses find to find the files we want then uses sed to replace the old text with tne new text.

 find PATH -name "*.md" | xargs sed -i -r 's/old text/new text/g'

FreeBSD Ports

Delete all ports’ work directories s. It’s a pretty quick way to free up some disk space

 rm -rf /usr/ports/*/*/WORK

clean up stale distfiles from the ports to free up space. This requires

portmaster to be installed

 portmaster -y --clean-distfiles

Clean up stale ports packages. Again this requires portmaster to be installed

 portmaster -y --clean-packages

Now that portsnap is depreciated to update the ports tree you can use if you don’t need or have git installed.

gitup ports

Network

Show all open ports

sockstat -l

Show all blacklisted IPs by blacklistd and remaining time they’re blocked for.

Show HTTP headers

curl --head https://deth.org

Show all blacklisted IPs from blacklistd. Also sort the results by IP address

blacklistctl dump -br | sort -h

Using the ipfw show all blocked IPs.

ipfw -d list

Show all of ipfw’s rules

 ipfw list

I use pf firewall along with blacklistd and this is how to show allthe blocked IP addresses

pfctl -a 'blacklistd/22' -t port22 -T show

Packages

Find out which package installed another package.

pkg info -r bash

Force reinstall all packages. It can be useful after upgrading between major versions of Freebsd

 pkg upgrade -f

clean up stale packages from the cache

pkg clean -a 

FreeBSD system

See what the currently installed version of FreeBSD is

freebsd-version -ku

Here’s how update the FreeBSD kernel and base version

freebsd-update fetch install

Here’s how to upgrade between major versions of the kernal and base system

freebsd-update upgrade -r 14.0-RELEASE

reboot system now

reboot

shutdown system

shutdown -p