Files
MarkdownNotes/blog - fun with the Linux command line.md
2023-04-28 12:11:45 -04:00

2.7 KiB

Linux tips, one-liners and magic-tricks

Truncate a text file

This is good for emptying huge log files without actually deleting and re-creating the file

echo -n '' > {target file}

For example

echo -n '' > /var/www/vhosts/horriblewebsite.com/statistics/logs/error_log

Grep One-Liners

How to find specific things with grep

find what looks like an IP Address

egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"

find what looks like an email address

egrep "\w+([._-]\w)*@\w+([._-]\w)*\.\w{2,4}"

find what looks like a URL

egrep -o "(http|https)://[a-zA-Z0-9./?=_-]*"

mempigs

A one-liner that will return the top 10 RAM consuming processes on a Linux machine

ps aux  | awk '{print $6/1024 " MB\t\t" $11 " " $12}'  | sort -n | tail -10

Alias it with something like this:

alias mempigs="ps aux | awk '{print \$6/1024 \" MB\t\t\" \$11 \" \" \$12}'  | sort -n | tail -10"

Then you only have to type mempigs to get a quick overview of what's eating all the memory.


Filter comments and blank lines with grep

There are times when you may want to check the contents of a configuration file, but it's littered with explanatory comments. Normally, documentation in a file is a good thing, but sometimes you just want to see the relevant settings.

Filter out comments with something like this:

grep -v \# /etc/some-config-file.conf

The grep flag -v excludes the next argument from the returned results. Because the pound sign is a common bash idiom that denotes a comment, you need to escape it in the above example, or everything in the command after the pound sign would be considered a comment!

So, you've filtered out all the comments. But now you're left with lines separated by blank lines where the comments used to be.

How to remove those blank lines? Add another grep to the command:

grep -v \# /etc/some-config-file.conf | grep -v -e '^$'

The last grep uses the -v again to remove the next argument, but in this case, we are removing the regex (denoted by -e) '^$' which looks for the beginning of a line followed by the end of the line (in other words a line with no content, a blank line)


Shrink and convert PDFs with ghostscript

REQ: ghostscript

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

If the -dPDFSETTINGS=/screen setting is too low quality to suit your needs, replace it with -dPDFSETTINGS=/ebook for better quality, but slightly larger PDFs. Delete the setting altogether for the high quality default.