Pages

Wednesday, December 21, 2022

Random Linux & Shell Command Tips - rpm, ss, kill, zip

Some of these tips are applicable only on Linux.

(1) List all files in an uninstalled RPM

Try any of the following.

rpm -qlp 
rpm -qilp 
rpm2cpio  | cpio -t

(2) Extracting a specific file from an RPM

Steps:

  1. List all files with path
    rpm2cpio  | cpio -t
    

    Note down the path of the file in focus as shown in above listing

  2. Extract the target file

    rpm2cpio  | cpio -ivdm 
    
  3. Look for the extracted file path under current directory

(3) Suspending and resuming a process

Suspend by running:

kill -TSTP 

and resume by running:

kill -CONT 

TSTP and CONT are job control signals for stopping the process and resuming it respectively.

Processes can trap and handle TSTP signal. If you don't want to give much control to the process, consider STP signal that cannot be handled, ignored, or blocked.

Related note: kill -l shows the complete list of signals.

(4) Accessing files that are shadowed by a mounted filesystem

When a directory containing one or more files becomes a mount point for a filesystem, files in the original directory are hidden and not accessible until the filesystem was unmounted. Following steps help accessing such hidden files.

High level steps:

unshare --mount
cd /
umount 
exit

(5) Socket statistics

On Linux, ss utility can be used to examine sockets statistics.

To see all open and non-listening sockets that have established connection, run ss command without any options.

-s option prints a summary of statistics.

-l option shows all listening sockets whereas -a option shows both listening and non-listening sockets.

-t, -u, -w and -x options show TCP, UDP, RAW and Unix domain sockets respectively.

ss       # all non-listening sockets in ESTAB state
ss -l    # listening sockets
ss -a    # listening and non-listening sockets
ss -t    # TCP sockets
ss -u    # UDP sockets
ss -w    # RAW sockets
ss -x    # UNIX domain sockets
ss -s    # summary of stastistics

Check man ss for all supported interesting options.

(6) Preserve symlinks when creating a Zip archive

zip --symlinks -r .zip 

(7) Exclude subdirectories when creating a Zip archive

Use -x of zip utility to exclude certain files or directories. Make sure to exclude not just directories but also the files underneath with "/*"

zip -r .zip  -x                        # exclude one file 
zip -r .zip  -x /* [-x /* .. -x /*]    # exclude one or more directories

(8) Extract specific file from a Zip archive

  • Locate the path of the file of interest by listing all files in the zip archive
  • Extract the file in focus with the help of -j option and optionally -d option to specify destination path
eg.,
$ unzip -l oci-java-sdk-2.30.0.zip | grep full
241351083  12-21-2022 18:33   lib/oci-java-sdk-full-2.30.0-javadoc.jar
 36970074  12-21-2022 17:35   lib/oci-java-sdk-full-2.30.0-sources.jar
 93038832  12-21-2022 17:35   lib/oci-java-sdk-full-2.30.0.jar
106426811  12-21-2022 18:37   shaded/lib/oci-java-sdk-full-shaded-2.30.0.jar

$ unzip -j oci-java-sdk-2.30.0.zip lib/oci-java-sdk-full-2.30.0.jar -d /var/tmp/dep
Archive:  oci-java-sdk-2.30.0.zip
  inflating: /var/tmp/dep/oci-java-sdk-full-2.30.0.jar

(9) Renaming or copying files that start with a dash (-)

Problem and workaround with an example.

eg.,
$ mv -07-01-05-40.pem api_key_pvt.pem
mv: illegal option -- 0
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory

$ mv -- "-07-01-05-40.pem" api_key_pvt.pem

$ ls -lrtha
total 16
-rw-r--r--@  1 gmandali  staff   1.7K Jun 30 22:41 api_key_pvt.pem

Double dash (--) is a bash builtin to indicate end of command options.

This workaround is applicable to cp (copy) and rm (remove) commands in addition to mv (move/rename) command.

Another option is to prefix the source file name with its pathname.

eg.,
mv ./-somefile.txt somefile.txt

No comments:

Post a Comment