Command line interface tools and tricks

2020-05-09

(last time edited: 2021-03-27)

tags: linux, shell

Ignoring aliases

You can ignore custom aliases by typing \ before the command, just like this:

$ \ls

$ \xrandr

$ \shutdown

$ \rm

This means the program will run without custom parameters you specified in config files such as .bashrc; .mkshrc; etc.

If you find the need to return to the previous directory do:

$ cd -

If you wanna get rid of the annoying stdout echo do this:

$ cd ~-

Using these parameters can help you get to the previous directory instead of going to home directory, it can truly help scripting.

Differences between su and sudo

Back when I started to learn about system administration I had some trouble understanding the differences between su and sudo.

They basically do the same thing but sudo is more flexible with sudoers files and permits the normal user to enter root password in order to perform superuser commands. I still prefer using su.

s stands for substitute and u for user.

Sometimes can be annoying to remember or differentiate if you are in a hurry. Both commands are pretty useful in their own way.

$ su - USERNAME -c "echo hello world"

$ sudo -u USERNAME echo hello world

These commands are especifically important if you want to execute a command from a normal user account that's locked with /sbin/nologin as default shell (you can check that out in /etc/passwd) or locked via passwd -l

Easy way to trim videos using FFmpeg

Sometimes I backup my own Twitch streaming videos. I download them from my video producer dashboard and then cut the parts I like the most.

To simplify this and don't use any kind of GUI video editor program such as olive, avidemux, shotcut, openshot... I just use the glorious ffmpeg.

This way of trimming also saves a lot of time, it does NOT re-encode the video.

$ ffmpeg -i ~/Downloads/somevideo.mp4 -ss 01:14:88 -to 03:11:00 -c:v copy -c:a copy newvideo.mp4

Using the -to parameter will cut out from an specific fixed time to another specific time. This is the best sane way to do it.

If you instead use -t it will extract (in this example) an entire time of 3 hours and 11 minutes after starting from 1h and 14 minutes and 88 seconds. This way of trimming is more random because you'll not know the specific fixed end time you'll get in the new video.

Important! If you don't declare -to it will trim everything until the end of the video. And if you don't declare -ss it will trim from the very start of the video.

Concatenate videos using FFmpeg

List your videos with full path in a new text file like this:

file '/home/username/Downloads/file1.mp4'
file '/home/username/Downloads/file2.mp4'
file '/home/username/Downloads/file3.mp4'

Then just use FFmpeg.

$ ffmpeg -f concat -safe 0 -i listofvideos.txt -c copy newvideo.mp4

Remember all videos need to be encoded with the same format.

List all non-free installed packages in a Void Linux system

I always forget this command.

$ xbps-query -s nonfree -p repository

List all available non-free packages in Void Linux repositories

$ xbps-query -Mi --repo=https://alpha.de.repo.voidlinux.org/current/nonfree -s \*

Check removed-packages in Void Linux

$ xbps-query -Rp replaces removed-packages

Stream a file using FFmpeg

$ ffmpeg -re -i movie.mp4 -c copy -f flv rtmp://server.com/stream?=key=KEYGOESHERE

Simple directory permissions

# chmod 700 dir

Simple file permissions

# chmod 600 file

Change PV (Physical volume) name

# dmsetup rename old_name new_name

Change VG (Volume group) name

# vgchange old_name new_name

Change LV (Logical volume)

# lvrename volume_group_here old_logical_volume new_logical_volume

Refresh logical volumes

If you get:

WARNING: Device mismatch detected for xxx/xxx which is accessing /dev/mapper/xxx instead of /dev/mapper/zzz.

This conflict happens when logical volumes and dm-crypt mappings are not correctly deactivated. To solve the problem you just simply execute this order:

# vgchange --refresh

Activate a VG

# vgchange -a y volume_group_here

Find XBPS packages with less or equal than X dependencies

As a shell function for your .bashrc / .mkshrc:

findpkg() {
    LIMITDEPS=9
    GREENDEPS=3
    INSTALLED="$(xbps-query -l | awk '{print $2}' | xargs -n 1 xbps-uhelper getpkgname)"
    PKGLIST="$(xbps-query -Rs "$*" | awk '{print $2}' | xargs -n 1 xbps-uhelper getpkgname | grep -v -e "i18n" -e "32bit" -e "devel")"
    if [ "$GREENDEPS" -eq 0 ]; then
        TITLE="Finding XPBS packages with no dependencies and less or equal than 1MB in size..."
    else
        TITLE="echo Finding XBPS packages with less or equal than $GREENDEPS dependencies and less or equal than 1MB in size..."
    fi  
    echo "$TITLE"
    for pkg in $PKGLIST; do
        NDEPS="$(xbps-query -Rx "$pkg" | wc -l)"
        PKGDESC="$(xbps-query -R -p short_desc "$pkg")"
        INSTALLED_SIZE="$(xbps-query -R -p installed_size "$pkg")"
        if [ ! "$(echo "$INSTALLED_SIZE" | grep -o MB)" = "MB" ] && [ "$NDEPS" -le $LIMITDEPS ] && [ "$INSTALLED_SIZE" != "0B" ]; then
            if [ "$NDEPS" -le "$GREENDEPS" ] && [ "$(echo "$INSTALLED" | grep -wo "$pkg" | head -1)" = "$pkg" ]; then
                TEXT="$(tput bold)$(tput setaf 2)$pkg $(tput setaf 7)- $(tput setaf 6)$INSTALLED_SIZE$(tput setaf 7) - $PKGDESC - $(tput setaf 2)$NDEPS deps.$(tput sgr0)"
                echo "$TEXT"
                SHOWN="$SHOWN\n$TEXT"
            elif [ "$NDEPS" -le "$GREENDEPS" ] && [ ! "$(echo "$INSTALLED" | grep -wo "$pkg" | head -1)" = "$pkg" ]; then
                TEXT="$(tput bold)$(tput setaf 1)$pkg $(tput setaf 7)- $(tput setaf 6)$INSTALLED_SIZE$(tput setaf 7) - $PKGDESC - $(tput setaf 2)$NDEPS deps.$(tput sgr0)"
                echo "$TEXT"
                SHOWN="$SHOWN\n$TEXT"
            elif [ "$NDEPS" -gt "$GREENDEPS" ] && [ "$(echo "$INSTALLED" | grep -wo "$pkg" | head -1)" = "$pkg" ]; then
                TEXT="$(tput bold)$(tput setaf 2)$pkg$(tput sgr0) - $(tput setaf 6)$INSTALLED_SIZE$(tput sgr0) - $PKGDESC - $NDEPS deps."
                echo "$TEXT"
                SHOWN="$SHOWN\n$TEXT"
            elif [ "$NDEPS" -gt "$GREENDEPS" ] && [ ! "$(echo "$INSTALLED" | grep -wo "$pkg" | head -1)" = "$pkg" ]; then
                TEXT="$(tput bold)$(tput setaf 1)$pkg$(tput sgr0) - $(tput setaf 6)$INSTALLED_SIZE$(tput sgr0) - $PKGDESC - $NDEPS deps."
                echo "$TEXT"
                SHOWN="$SHOWN\n$TEXT"
            fi
        fi
    done
    clear
    echo "$TITLE$SHOWN" | head -n -1 | less -R
    unset SHOWN
}

Usage is very simple. Open a virtual terminal and pass any kind of argument.

$ findpkg browser

Find bloat packages in your Void Linux

findbloat() {
    DEPS=9
    TITLE="Finding bloat XBPS packages with equal or more than $DEPS dependencies..."
    INSTALLED="$(xbps-query -l | awk '{print $2}' | xargs -n 1 xbps-uhelper getpkgname | grep -v -e 'i18n' -e '32bit' -e 'devel')"
    echo "$TITLE"
    for pkg in $INSTALLED; do
        NDEPS="$(xbps-query -Rx "$pkg" | wc -l)"
        INSTALLED_SIZE="$(xbps-query -R -p installed_size "$pkg")"
        if [ "$NDEPS" -ge "$DEPS" ] && [ "$INSTALLED_SIZE" != "0B" ] && [ "$(echo "$INSTALLED" | grep -wo "$pkg" | head -1)" = "$pkg" ]; then
            PKGDESC="$(xbps-query -R -p short_desc "$pkg")"
            if [ "$NDEPS" -ge 40 ]; then
                TEXT="$(tput bold)$(tput setaf 2)$pkg$(tput sgr0) - $(tput setaf 6)$INSTALLED_SIZE$(tput sgr0) - $PKGDESC - $(tput bold)$NDEPS deps.$(tput sgr0)"
                echo "$TEXT"
                SHOWN="$SHOWN\n$TEXT"
            else
                TEXT="$(tput bold)$(tput setaf 2)$pkg$(tput sgr0) - $(tput setaf 6)$INSTALLED_SIZE$(tput sgr0) - $PKGDESC - $NDEPS deps."
                echo "$TEXT"
                SHOWN="$SHOWN\n$TEXT"
            fi
        fi
    done
    clear
    echo "$TITLE$SHOWN" | head -n -1 | less -R
    unset SHOWN
}

Record desktop audio using FFmpeg and PulseAudio

$ ffmpeg -f pulse -i default $(date +%s).mp3

Record desktop video with FFmpeg

$ ffmpeg -video_size 1920x1080 -framerate 24 -f x11grab -i :0.0 $(date +%s).mp4

Listing files arguments

I know listing files via terminal can be done by any person, but adding some arguments and trying to remember them can be a pain in the ass, so I save it here instead of creating a useless alias.

$ ls -lahS

-l for list mode, -a includes hidden files; -h intuitive file sizes, -S sorted by file size.

Show all processes running by a user

$ ps -u $USER

for more info

$ ps -fu $USER

for more info and trees

$ ps -fu $USER --forest

Print CSV files (comma-separated values file) in terminal

$ cat file.csv | column -ts , | less -S

Find all programs running with exact program name

$ pgrep -xl st

Allow only specific IPs to connect to your listening port

Replace 0.0.0.0 with the client IP you want to able to connect.

# iptables -I INPUT \! --src 0.0.0.0 -m tcp -p tcp --dport 55435 -j DROP