93 lines
2.7 KiB
Markdown
93 lines
2.7 KiB
Markdown
# 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
|
||
|
||
```bash
|
||
echo -n '' > {target file}
|
||
```
|
||
|
||
For example
|
||
|
||
```bash
|
||
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
|
||
|
||
```bash
|
||
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
|
||
|
||
```bash
|
||
egrep "\w+([._-]\w)*@\w+([._-]\w)*\.\w{2,4}"
|
||
```
|
||
|
||
### find what looks like a URL
|
||
|
||
```bash
|
||
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
|
||
|
||
```bash
|
||
ps aux | awk '{print $6/1024 " MB\t\t" $11 " " $12}' | sort -n | tail -10
|
||
```
|
||
|
||
Alias it with something like this:
|
||
|
||
```bash
|
||
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:
|
||
|
||
```bash
|
||
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:
|
||
|
||
```bash
|
||
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
|
||
|
||
```bash
|
||
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. |