Useful shell commands

Useful shell commands

April 6, 2023
shell

xargs #

xargs is a versatile command in Unix-like operating systems, which is used to read items from standard input separated by lines, spaces, or other delimiters, and then execute a command using those items as arguments. Here are some of the most commonly used options for xargs:

  1. -I: This option allows you to specify a placeholder that will be replaced with the input item in the command line. For example:
$ find . -name '*.txt' | xargs -I {} mv {} /tmp/

This command will find all the .txt files in the current directory and its subdirectories and move them to the /tmp directory.

  1. -L: This option specifies the maximum number of input items to process per command line execution. For example:
$ seq 1 10 | xargs -l2 echo

will output

1 2
3 4
5 6
7 8
9 10
  1. -P: This option specifies the maximum number of processes to run simultaneously. For example:
$ find . -name '*.txt' | xargs -P 4 -I {} gzip {}

This command will find all the .txt files in the current directory and its subdirectories and compress them using gzip with up to 4 processes running simultaneously.

  1. -0 or –null: This option reads input items terminated by a null character instead of whitespace. It is particularly useful when dealing with filenames containing spaces or other special characters. For example:
$ find . -name '*.txt' -print0 | xargs -0 rm

This command will find all the .txt files in the current directory and its subdirectories and remove them, properly handling filenames with spaces or special characters.

  1. –max-args or -n: This option is similar to -L and specifies the maximum number of input items to process per command line execution. The difference is that -n processes the exact number of items, whereas -L processes up to the specified number. For example:
$ seq 1 10 | xargs -n 3 echo

This command will print numbers from 1 to 10 in groups of 3.

find -print0 #

reload tmux session #

$ tmux list-windows -a -F "#{window_id}" | xargs -I % tmux list-panes -t % -F "#{pane_id}" | xargs -I {} tmux send-keys -t {} "source ~/.zshrc" Enter

In the xargs command, the -I option is used to specify a placeholder that will be replaced with the input item in the command line. The character (or string) following -I is the placeholder that you define. In this case, the placeholder is %.

When using xargs -I %, it means that every occurrence of % in the command following the pipe (|) will be replaced with the input item read from the standard input (stdin).

For example, if you have the following command:

$ echo "1 2 3" | xargs -I % echo "Number: %"

The output will be:

Number: 1
Number: 2
Number: 3

In this case, % is used as a placeholder for the input items (1, 2, and 3), and xargs executes the echo “Number: %” command for each input item, replacing % with the respective item.

% and {} are just different placeholders you can choose for the same purpose when using the -I option in xargs. They both represent the input item in the command line, and you can use either one as a placeholder.

number of physical cpu cores #

$ g
rep -E '^core id|^physical id' /proc/cpuinfo | xargs -l2 echo | sort -u | wc -l

In the command xargs -l2, the -l2 option (lowercase L followed by a number) is used to specify the maximum number of input items that will be processed per command line execution. In this case, it means that xargs will process two input items at a time.

xargs -l2 takes the output of the grep command (which consists of lines with ‘core id’ and ‘physical id’) and processes them in pairs. For each pair of input items, it will execute the echo command, effectively combining the ‘core id’ and ‘physical id’ lines into a single line. This makes it easier to sort and count the unique combinations later in the pipeline.

For exampe, the following command:

$ grep -E '^core id|^physical id' /proc/cpuinfo

The output will be:

physical id     : 0
core id         : 0
physical id     : 0
core id         : 1
physical id     : 0
core id         : 2
physical id     : 0
core id         : 3
physical id     : 0
core id         : 0
physical id     : 0
core id         : 1
physical id     : 0
core id         : 2
physical id     : 0
core id         : 3

And the command

$ grep -E '^core id|^physical id' /proc/cpuinfo | xargs -l2 echo

will output

physical id : 0 core id : 0
physical id : 0 core id : 1
physical id : 0 core id : 2
physical id : 0 core id : 3
physical id : 0 core id : 0
physical id : 0 core id : 1
physical id : 0 core id : 2
physical id : 0 core id : 3

number of logical cpu cores #

$ grep -c '^processor' /proc/cpuinfo

or

$ nproc

check thread of a process #

$ ps -T -p 212154  -o pid,tid,spid,uid,comm

or

$ cd /proc/212154
$ cd task

or

# gdb -p 212154
(gdb) info threads
(gdb) thread 1
(gdb) backtrace

Commonly used gdb commands #

  1. run: This command starts the program being debugged from the beginning. You can specify arguments to the program after the run command.

  2. break: This command sets a breakpoint at a specific line number or function name in the program being debugged. You can also set a breakpoint condition or hit count.

  3. next: This command steps over the current line of code being executed in the program. If the line contains a function call, the function is executed and the debugger stops on the next line of code in the current function.

  4. step: This command steps into the current line of code being executed in the program. If the line contains a function call, the debugger stops at the first line of code in the called function.

  5. continue: This command resumes program execution until a breakpoint or error occurs.

  6. print: This command prints the value of a variable or expression in the program being debugged.

  7. info: This command provides information about the program being debugged, such as the current thread, call stack, breakpoints, and source files.

  8. backtrace or bt: This command displays a backtrace of the call stack for the current thread.

  9. thread: This command is used to switch between threads when debugging a multithreaded program. You can specify a thread index or thread ID to select a specific thread.

  10. quit: This command exits the GDB debugger.

These are just a few of the many commands available in GDB. For a full list of commands and their descriptions, use the help command in the GDB prompt.

Inspect cert from CLI #

To inspect a certificate from the command line in Linux, you can use the openssl command. Here are some examples:

  1. Display the details of a certificate file:

    openssl x509 -in /path/to/certificate.crt -text -noout
    

    This command will display the text representation of the certificate in the console, including its subject and issuer details, validity period, public key information, and any extensions.

  2. Verify the validity of a certificate against a CA bundle:

    openssl verify -CAfile /path/to/ca-bundle.crt /path/to/certificate.crt
    

    This command will verify whether the certificate is trusted by any of the CAs in the CA bundle file. If the certificate is valid, the output will be “OK”. Otherwise, the output will indicate the reason for the failure.

  3. Check the expiration date of a certificate:

    openssl x509 -in /path/to/certificate.crt -enddate -noout
    

    This command will display the expiration date of the certificate in the console.

  4. Check the SHA-256 fingerprint of a certificate:

    openssl x509 -in /path/to/certificate.crt -sha256 -noout -fingerprint
    

    This command will display the SHA-256 fingerprint of the certificate in the console. This can be used to verify the integrity of the certificate.

You can replace /path/to/certificate.crt and /path/to/ca-bundle.crt in the above examples with the actual file paths of the certificate and CA bundle that you want to inspect.

lsof usage #

Show listening tcp port

lsof -i TCP -s TCP:LISTEN -n -P

inspect cert #

Using OpenSSL #

You can use the following command to connect to a server and print out the certificate:

openssl s_client -connect mydomain.com:443 -servername mydomain.com

If you only want to see the certificate, you can pipe it into the openssl x509 command like so:

openssl s_client -connect mydomain.com:443 -servername mydomain.com </dev/null 2>/dev/null | openssl x509 -text -noout

This command connects to mydomain.com on port 443 (the standard port for HTTPS) and prints out the certificate in human-readable form.

Using Curl #

If you have a certificate file, such as a .crt or .pem, you can view the certificate details using curl. Here’s a command that prints the certificate details(Check SSL connection parts in the output):

curl --insecure -v https://mydomain.com 2>&1 | awk 'BEGIN { cert=0 } /^\* SSL connection/ { cert=1 } /^\*/ { if (cert) print }'

Inspecting Local Certificate File #

If you have the certificate saved as a file (e.g., certificate.pem), you can inspect it using openssl as follows:

openssl x509 -in certificate.pem -text -noout

This will print all the details of the certificate, including issuer, subject, dates of validity, and more.

Remember to replace mydomain.com with the actual domain name you want to inspect or certificate.pem with the actual path to your certificate file.