40 votes
Accepted

iptables show just one chain

I almost deleted this question. D'oh! From man iptables: -L, --list [chain] List all rules in the selected chain.
bgStack15's user avatar
  • 1,131
33 votes

How to split a PEM file

The split command is available on most systems, and its invocation is likely easier to remember. If you have a file collection.pem that you want to split into individual-* files, use: split -p "--...
squidpickles's user avatar
19 votes

How to make in SSH private key from one line, three lines

I would personally base64-encode the key, store it, then base64 decode it when you need it. Encode: echo "-----BEGIN RSA PRIVATE KEY----- my_super_secret_password -----END RSA PRIVATE KEY-----" | ...
Craig Watson's user avatar
  • 9,680
11 votes

How to get all fingerprints for .ssh/authorized_keys(2) file

A one-liner based on the /dev/stdin trick from ℝaphink's answer and man xargs → EXAMPLES: egrep '^[^#]' ~/.ssh/authorized_keys | xargs -n1 -I% bash -c 'ssh-keygen -l -f /dev/stdin <<<"%"...
akavel's user avatar
  • 357
9 votes

How to split a PEM file

If you want to get a single certificate out of a multi-certificate PEM bundle, try: $ openssl crl2pkcs7 -nocrl -certfile INPUT.PEM | \ openssl pkcs7 -print_certs | \ awk '/subject.*CN=host....
cmcginty's user avatar
  • 1,303
8 votes

Extract a ids from text file

The most straightforward answer is to not try and utilize grep, sed, awk, etc for this - even though they will do it, you will most assuredly end up making assumptions and having to constantly shuffle ...
Ackack's user avatar
  • 1,024
7 votes

How to split a PEM file

If you are handling full chain certificates (i.e. the ones generated by letsencrypt / certbot etc), which are a concatenation of the certificate and the certificate authority chain, you can use bash ...
Fabio's user avatar
  • 181
7 votes

Calculate sum of several sizes of files in Bash

According to du(1), there is a -c option whose purpose is to produce the grand total. % du -chs * /etc/passwd 92K ABOUT-NLS 196K NEWS 12K README 48K THANKS 8,0K TODO 4,0K /etc/passwd 360K ...
Laurent's user avatar
  • 71
6 votes
Accepted

Calculate sum of several sizes of files in Bash

Use stat instead of du: #!/bin/bash for i in `grep -v ^# ~/cache_temp | grep -v "dovecot.index.cache"`; do [ -f "$i" ] && totalsize=$[totalsize + $(stat -c "%s" "$i")] done echo ...
Ipor Sircer's user avatar
  • 1,236
6 votes

Calculate sum of several sizes of files in Bash

If you need to use the file this snippet is hopefully efficient. xargs -a cache_file stat --format="%s" | paste -sd+ | bc -l The xargs is to prevent overflowing the argument limit but getting the ...
Matthew Ife's user avatar
  • 23.6k
5 votes

How to get all fingerprints for .ssh/authorized_keys(2) file

Since recent versions of OpenSSH, this command already produces a nice listing for you: ssh-keygen -l -f authorized_keys Example: # ssh-keygen -l -f authorized_keys 2048 SHA256:GzZ7......................
Jon Bundesen's user avatar
4 votes

iptables show just one chain

For those who might want to look at the mangle or nat tables with chains you can do so like iptables -L mychainname -t mangle Then it will show you that chain in the mangle table.
Onyxdragun's user avatar
4 votes

Calculate sum of several sizes of files in Bash

If you remove the "-h" flag from your "du" command, you'll get the raw byte sizes. You can then add them with the ((a += b)) syntax: a=0 for i in $(find . -type f -print0 | xargs -0 du -s | awk {'...
shearn89's user avatar
  • 3,513
4 votes
Accepted

How to correctly escape single quotes in AWK

You cannot escape single quotes as the command itself is surrounded by single quotes, but you could use an octal escape code \047 to represent ' in POSIX awk. Additionally, you could use a hexadecimal ...
Esa Jokinen's user avatar
  • 49.8k
3 votes

How to show specific luns from lsscsi command?

Try this: lsscsi 4 0 1 | sed -r '/\[4:0:1:(1|12|13)\].*/!d' Output will be: [4:0:1:1] /dev/sdab [4:0:1:12] /dev/sdo [4:0:1:13] /dev/sdp
Egor Vasilyev's user avatar
3 votes
Accepted

How to extract the last two (or any number) directories from a path with sed?

echo "/your/directory/here/bla" | awk -F"/" '{ print "/"$(NF-1)"/"$NF }' should work with a find, i didn't try, but something like this should be working : find /yourfolder/structure -type d | awk -...
olivierg's user avatar
  • 524
3 votes

Replace a string once another string has been found

Even if this can probably be solved by a sed command, it would be simpler to use the proper command to change yum configuration: yum install yum-utils yum-config-manager --enable remi-php56 And ...
Remi Collet's user avatar
  • 2,146
3 votes

Color awk output on commandline?

For the convenience of future querents, here's a set of functions that one could put into a file called, let's say color.awk. function BLACK(X) { return "\033[30m" X "\033[0m" } ...
merlin2011's user avatar
3 votes

text processing linux using grep awk or anything else

IMHO Typically date/time manipulation works better when you treat a time string as time and not a text string. Use the formatting options of the date command to get the timestamp to display in ...
HBruijn's user avatar
  • 80.3k
3 votes

sed/awk - find match by one string and change different string on the same line

Sure you can solve your issue with one-liner, but it isn't reusable. So I suggest try to use this quickly written script and try to figure out, what it does, to improve your skills. #!/bin/sh ...
Anton Danilov's user avatar
3 votes

How to add the filename as prefix to each line of the text file? (batch processing; subdirectories)

You can use: grep -r . . The first . is RegExp patern matching any character. It will match all lines. The second . is for the current folder, but you can replace it with any valid path. E.g.: ...
Mircea Vutcovici's user avatar
3 votes

How do I cut just the file name from from ls -l output?

/bin/ls -1 That leaves out all the goo :) The problem being that ls in most distributions is aliased to something like “ls -la”. You can check that with “alias ls” and see if anything comes up ...
Franz Bettag's user avatar
3 votes
Accepted

How do I cut just the file name from from ls -l output?

It's simpler to use a glob instead of ls to work with a list of file names for f in *; do echo file \'$f\' done
Bert's user avatar
  • 3,023
3 votes

Convert lscpu output to row based format in excel/csv table format

You can print the output of lscpu in JSON format, then select what you need with jq: [user@laptop ~]$ lscpu -J|jq .lscpu[0] { "field": "Architecture:", "data": "...
Mircea Vutcovici's user avatar
3 votes

Convert lscpu output to row based format in excel/csv table format

Use JSON format and register the data to the variable lscpu_out - command: lscpu -J register: lscpu_out Extract all variables lscpu_out, get the attribute stdout, convert the strings to ...
Vladimir Botka's user avatar
3 votes
Accepted

How to save input colors in awk?

some_command may have an option to force color even when it's connected to a terminal, for example with grep it would be --color=always. If it doesn't though you have to trick some_command into ...
Arkadiusz Drabczyk's user avatar
2 votes

AWK and file names with space in it.

You could try this. awk '{print substr($0, index($0,$9))}' For example this is the output of ls command: -rw-r--r--. 1 root root 73834496 Dec 6 10:55 File with spaces 2 If you use simple awk ...
Himanshu Chauhan's user avatar
2 votes

How to combine lsof stat and ps to get more info in one line bash command

If you use awk to print $1 (the command) and $2 (the PID) in reverse order ($2, $1) this provides most of the information you want in a usable format. The first sort and uniq -c still work as expected ...
user9517's user avatar
  • 116k
2 votes
Accepted

Matching first block of text from a file, between 2 strings

one line sed sed -n '/^menuentry/,/\}$/{p;/^\}$/q}' /^\}$/q means that "if the line matches with pattern /^\}$/ then quit"
ymonad's user avatar
  • 136
2 votes
Accepted

Replace a string once another string has been found

Try this: sed -i -e '/\[remi-php56\]/,/^\[/s/enabled=0/enabled=1/' /etc/yum.repos.d/*.repo for process all .repo files, or you can specify single target .repo file. Or you can use you-config-...
Slipeer's user avatar
  • 3,313

Only top scored, non community-wiki answers of a minimum length are eligible