Skip to content

Instantly share code, notes, and snippets.

@fatihbaltaci
Last active February 27, 2024 21:10
Show Gist options
  • Save fatihbaltaci/11c017db00e54ac025ed4bb9f641a03f to your computer and use it in GitHub Desktop.
Save fatihbaltaci/11c017db00e54ac025ed4bb9f641a03f to your computer and use it in GitHub Desktop.
Linux Commands and Tools

Table of Contents

Keyboard Shortcuts

Grab Screenshot Area and Copy to Clipboard

  • Name = screenshot_grab
  • Command = gnome-screenshot -a -c Not Working on Ubuntu 20.04
  • Command = /home/fatih/ss.sh
  • Shortcut = F4

ss.sh

#!/bin/bash
gnome-screenshot -acf /tmp/test && cat /tmp/test | xclip -i -selection clipboard -target image/png

or

Change Copy a screenshot of an area to clipboard to F4

Spotify Controller

Play-Pause
  • Name = spotify_play_pause
  • Command = dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
  • Shortcut = SHIFT + 5 (Numpad)
Next
  • Name = spotify_next
  • Command = dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next
  • Shortcut = SHIFT + 6 (Numpad)
Prev
  • Name = spotify_prev
  • Command = dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous
  • Shortcut = SHIFT + 4 (Numpad)

Useful Shortcuts

Open Terminal in current Directory = Shift + F10 + e


vim

dw : Deletes one word/token. Also very useful to remove indenting in source code.

Terminal

cd - : Move back into the most recent directory

sudo !! : runs the previous command as sudo.

CTRL + Z : Pauses an application fg : Returns you to the application

xdg-open : Opens a file

grep -A3 -B7 <PATTERN> : include 3 line After the match and include 7 lines Before the match

Add User

$ sudo adduser <username>
$ sudo passwd <username>
$ sudo usermod -aG docker <username>
# Give sudo priviliges
$ sudo usermod -aG wheel username

Install Terminalizer (Terminal Recorder)

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - && sudo apt install nodejs
$ sudo npm install -g terminalizer --unsafe-perm=true --allow-root

error while loading shared libraries: libgconf-2.so.4: cannot open shared object file: No such file or directory Solution:

sudo apt-get install libgconf-2-4

Using Terminalizer

First time:

$ terminalizer init
$ cd ~/.terminalizer && wget https://gist.githubusercontent.com/fatihbaltaci/9692cf265949d63953fe7cd7ac8b6549/raw/8092b2d94277c70a30b680f9f2191b6cc7e295a0/config.yml -O config.yml
$ terminalizer record redial_add
$ sudo terminalizer render redial_add

SSH

Copy SSH Key

$ ssh-copy-id -i id_rsa.pub <username>@<server_ip>

Dynamic Port Forwarding

$ ssh -D <PORT> <USERNAME>@<HOSTNAME>
  • List process:
$ netstat -tlpn | grep ssh

Set SSH Key for Multiple Accounts

~/.ssh/config

Host bitbucket_personal
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa
Host bitbucket_work
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa_work
  • Clone. Change bitbucket.org with bitbucket_work
git clone git@bitbucket_work:company/repo.git

Install Synergy

$ sudo apt install synergy=1.8.8-stable+dfsg.1-1build1

Screen

Create screen session
$ screen -S <session_name> 
Detach from Linux Screen Session
Ctrl+a d
List Screen Sessions
$ screen -ls
Reattach to a Linux Screen
$ screen -r <session_id>
Kill Screen Session
  • Attach screen session
  • Press Ctrl + A then type :quit

FFmpeg - youtube-dl

Split Video

-ss : start timestamp (HH:MM:SS.xxx) -t : encoding duration (HH:MM:SS.xxx)

$ ffmpeg -ss 00:00:00 -i not_stable_video.avi -c copy -t 00:00:03 not_stable_video_splitted.avi

Create UDP Stream

$ ffmpeg -re -stream_loop -1 -i video.mp4 -vcodec h264 -f mpegts udp://localhost:1414
  • Open VLC:

VLC -> Media -> Open Network Stream -> Network

URL:

udp://@localhost:1414

Extract Frames from Video

$ ffmpeg -i video.mp4 frame%d.png

Get metadata From Video or Stream

ffprobe -show_data -hide_banner video.mp4

Save Stream as Video

ffmpeg -i rtsp://... -vcodec copy out.mp4

Youtube Video to Audio Stream(mp3)

Credit (Ahmet Alp Balkan): https://www.youtube.com/watch?v=6BV7TaDSu0Q

youtube-dl <VIDEO_URL> -o- | ffmpeg -i /dev/stdin -f mp3 -ab 96000 -vn - > out.mp3

youtube-dl to vlc

youtube-dl <VIDEO_URL> -o- | vlc -

or

vlc <VIDEO_URL>

nmap

Find Open Ports

$ nmap -v -A <URL>

Host IP Discovery

$ nmap -sP 192.168.2.1/24

Install Offline Package

Install .deb files offline.

Prepare deb Packages on Online Machine

  • Remove apt caches
$ sudo rm -rf /var/cache/apt/archives/*
  • Download package
$ sudo apt-get install wireshark
# ls /var/cache/apt/archives/
libc-ares2_1.14.0-1_amd64.deb                libwireshark11_2.6.10-1~ubuntu18.04.0_amd64.deb   lock
libmaxminddb0_1.3.1-1_amd64.deb              libwireshark-data_2.6.10-1~ubuntu18.04.0_all.deb  partial
libnl-route-3-200_3.2.29-0ubuntu3_amd64.deb  libwiretap8_2.6.10-1~ubuntu18.04.0_amd64.deb      wireshark_2.6.10-1~ubuntu18.04.0_amd64.deb
libsmi2ldbl_0.4.8+dfsg2-15_amd64.deb         libwscodecs2_2.6.10-1~ubuntu18.04.0_amd64.deb     wireshark-common_2.6.10-1~ubuntu18.04.0_amd64.deb
libspandsp2_0.0.6+dfsg-0.1_amd64.deb         libwsutil9_2.6.10-1~ubuntu18.04.0_amd64.deb       wireshark-qt_2.6.10-1~ubuntu18.04.0_amd64.deb

Dependencies are shown in /var/cache/apt/archives/.

  • Zip dependencies
$ cd /var/cache/apt/archives/ && sudo zip wireshark_deb.zip *

Install deb Packages on Offline Machine

  • Move wireshark_deb.zip to offline machine.
  • Unzip it to /var/cache/apt/archives/
  • Install the package with dpkg
$ sudo dpkg -i wireshark_2.6.10-1~ubuntu18.04.0_amd64.deb
  • This will give an error with missing dependencies. apt-get can fix these problems
$ sudo apt-get -f install
  • Package is installed

Conda

Export Environment

  • Activate
$ conda activate yolov3
  • Export
$ conda env export > conda_yolov3.yml

Git

Get Remote (origin) URL

git remote show origin
git remote -v

Change Local Config

git config user.email "[email protected]"
git config user.name "Fatih Baltacı"

Change Global Config

git config --global user.email "[email protected]"
git config --global user.name "Fatih Baltacı"

Delete Local Branch

git branch -d <branch_name>

Delete Remote Branch

git push origin --delete <branch_name>

Python

Profile Script with cProfiler

python -m cProfile test.py

Profile Inside Code with cProfiler

import cProfile
cProfile.run('foo()')

Upgrade to 3.7

sudo apt install python3.7

nohup

nohup <COMMAND> > output.txt 2>&1 &

Add User Non-Interactive

#!/bin/bash
for USERNAME in user1 user2 user3
do
    sudo adduser $USERNAME
    echo 123 | sudo passwd $USERNAME --stdin
    sudo usermod -aG docker $USERNAME
done

Delete User

  • Without home directory
sudo userdel <username>
  • With home directory
sudo userdel -r <username>

jq Json Parser

https://stedolan.github.io/jq/

sudo apt-get install jq

Ubuntu 18.04 Beats Studio 3 Bluetooth Connection Problem

  • Install blueman
sudo apt-get install blueman

https://forum.manjaro.org/t/pair-wireless-bluetooth-headphones-beats-solo-3/41032/4

  • Change: sudo touch /etc/bluetooth/audio.conf Add these lines inside. Do not change anything. The order is important.
[General]
Disable=Socket
Disable=Headset
Enable=Media,Source,Sink,Gateway
AutoConnect=true
load-module module-switch-on-connect

Now you will have to edit the file /etc/bluetooth/main.conf. Change the contents to this.

### Restricts all controllers to the specified transport. Default value
## #is "dual", i.e. both BR/EDR and LE enabled (when supported by the HW).
## #Possible values: "dual", "bredr", "le"
ControllerMode = bredr

If you have just one Bluetooth toggle and you switch from computer all the time, change this line too. Change the code to this.

#AutoEnable defines option to enable all controllers when they are found. 
#This includes adapters present on start as well as adapters that are plugged 
#in later on. Defaults to 'false'. 
AutoEnable=true
  • Restart bluetooth service
sudo service bluetooth restart
  • Connect using blueman
    • Pair
    • Audio Sink
  • Exit blueman To prevent Glitch problem, close settings and bluetoohctl tool.

Install minikube

https://minikube.sigs.k8s.io/docs/start/linux/

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \
   && sudo install minikube-linux-amd64 /usr/local/bin/minikube
sudo minikube start --vm-driver=none

Install kubectl

https://kubernetes.io/docs/tasks/tools/install-kubectl/

curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl && chmod +x ./kubectl && sudo mv ./kubectl /usr/local/bin/kubectl

Get pods

kubectl get po -A

Copy to Clipboard

sudo apt install xclip
# copy
echo "fatih" | xclip -selection clipboard
#paste
xclip -selection clipboard -o

Crontab

/etc/crontab

# every hour
0 0 * ? * * *   fatih   cd /home/fatih && bash docker_files_run.sh

Copy and Preserve Existing File

file1.txt -> file1.txt file1.txt.orig

cp --backup --suffix=.orig file1.txt /file/path

Add Swap Memory

Ubuntu

https://www.cloudbooklet.com/how-to-add-swap-space-on-ubuntu-18-04-google-cloud/

sudo swapon --show
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Making the Swap File Permanent
sudo vi /etc/fstab
# Add
/swapfile swap swap defaults 0 0
sudo mount -a

Centos

https://linuxize.com/post/how-to-add-swap-space-on-centos-7/

sudo swapon --show
sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Making the Swap File Permanent
sudo vi /etc/fstab
# Add
/swapfile swap swap defaults 0 0
sudo mount -a

Install Nvidia Driver

Ubuntu

https://www.cyberciti.biz/faq/ubuntu-linux-install-nvidia-driver-latest-proprietary-driver/

sudo apt search nvidia-driver
sudo apt install nvidia-driver-430

Centos

P100

sudo rpm -i nvidia-driver-local-repo-rhel7-440.64.00-1.0-1.x86_64.rpm
sudo yum clean all
sudo yum install cuda-drivers
sudo reboot

Change username, group and home directory

NEW_USERNAME=new_user
OLD_USERNAME=old_user
sudo usermod -l $NEW_USERNAME $OLD_USERNAME
sudo groupmod -n $NEW_USERNAME $OLD_USERNAME
sudo usermod -d /home/$NEW_USERNAME -m $NEW_USERNAME

Ubuntu Post Install

#!/bin/bash

sudo apt update && \
sudo apt upgrade -y && \
sudo apt install -y openssh-server vim htop curl wget zip unzip && \
curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh && \
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) && \
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - && \
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list && \
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit && \
sudo systemctl restart docker && \
sudo apt-get install -y nvidia-container-runtime && \
sudo echo `curl -s -I https://gist.githubusercontent.com/fatihbaltaci/025b35a0563f2eff3713ba333e3f0218/raw/192c98650d7f5be8758309239df29372b851d3a1/default_nvidia_runtime.txt` > /etc/docker/daemon.json
sudo pkill -SIGHUP dockerd && \
sudo systemctl restart docker

Logitech M560 Buttons

https://mehmethanoglu.com.tr/tips/16-logitech-mouse-m560-ileri-ve-geri-butonlari.html

sudo apt-get install imwheel
imwheel -k -b "0 0 0 0 11 10"

Ubuntu Install Freeze

https://askubuntu.com/a/870245/1074472

Then choose the Ubuntu, or Install Ubuntu (it depends, you will see it hopefully), go to it with the arrows and press the 'e' key.
Here go to the line which contains quiet splash at the end and add acpi=off after these words.
Then press F10 to boot with these settings.

Error Installing Ubuntu

initramfs unable to find a medium containing a live file system 
  • Enable CSB (Legacy)
  • Disable Fast Boot

Move Gitlab issues Bulk to Another Project

https://gitlab.com/gitlab-org/gitlab/-/issues/2102#note_347630681

var fakeRun = true;                        // set to FALSE to actually run it; this way, you can check the url and params before actually doing it
var baseUrl = "https://gitlab.com/"; // TODO replace with your url
var targetProjectId = 0000001;                   // TODO replace with your target project id
var time = 500;

jQuery(".issues-list > li").each(function (index, element) {
    setTimeout(function () {
        var relativeUrl = jQuery(element).attr('url');
        var data = {move_to_project_id: targetProjectId};
        var url = baseUrl + relativeUrl + "/move";
        
        console.log('POSTING TO ' + url + ' with ' + JSON.stringify(data));
        
        if (!fakeRun) {
            $.ajax({
                type: "POST",
                url: url,
                data: data,
                success: function () {
                    console.log('OK');
                }
            });
        }
    }, time);

    time += 800;
});

Assign Static IP Adress

Centos

sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
sudo systemctl restart network

Ubuntu

  • /etc/netplan/00....yaml
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp39s0:
      dhcp4: no
      addresses: [192.168.3.214/24]
      gateway4: 192.168.3.1
      nameservers: 
        addresses: [192.168.3.1,8.8.8.8,8.8.4.4]

OpenCV

Frames to Video

import cv2
import glob
from tqdm import tqdm

PATH = "*.jpg"
VID_OUT_PATH = "tmp.mp4"
fourcc = 'mp4v'
fps = 30
width = 1920
height = 1080
img_files = sorted(glob.glob(PATH))

vid_writer = cv2.VideoWriter(
    VID_OUT_PATH, 0,  cv2.VideoWriter_fourcc(*fourcc), fps, (width, height))


for img_file in tqdm(img_files):
    img = cv2.imread(img_file)
    # img = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA)
    vid_writer.write(img)

gdown download from google drive

gdown https://drive.google.com/uc?id=<ID_HERE>

Reload Nvidia Kernel

sudo rmmod nvidia_uvm
sudo modprobe nvidia_uvm

Git Clone with Specific SSH Key

GIT_SSH_COMMAND='ssh -i /Users/fatih/.ssh/id_rsa_work -o IdentitiesOnly=yes' git clone [email protected]:.....

WSL 2

https://docs.nvidia.com/cuda/wsl-user-guide/index.html

wsl -l -v
wsl --set-default-version 2

nginx File Server

  • nginx_file.conf
server {
    listen       80;          # a customed port

    # download
    autoindex on;               # enable directory listing output
    autoindex_exact_size off;   # output file sizes rounded to kilobytes, megabytes, and gigabytes
    autoindex_localtime on;     # output local times in the directory

    location / {
        root /upload;
    }
}
  • Run docker
FILES_PATH=/home/fatihbaltaci/files
docker run -it --rm --name nginx  -p 3042:80 -v $FILES_PATH:/upload -v `pwd`:/usr/share/nginx/html -v /home/fatihbaltaci/nginx_file_server/nginx_file.conf:/etc/nginx/conf.d/default.conf nginx

Oracle Database 11g

# Download 11gR2 from https://www.oracle.com/tr/database/technologies/xe-prior-releases.html
# Required: 
# - 3G swap

docker run -it --restart always --name oracledb_11g --ipc host -p 8080:8080 -p 1521:1521 -v `pwd`:/workspace -v /home/fatihbaltaci/backup/oracle:/u01 centos:7

yum update -y && yum install -y sudo libaio* bc wget java-1.8.0-openjdk net-tools

cd /workspace/Disk1 && rpm -i oracle-xe-11.2.0-1.0.x86_64.rpm

/etc/init.d/oracle-xe configure

export LD_LIBRARY_PATH=/u01/app/oracle/product/11.2.0/xe/lib:$LD_LIBRARY_PATH
export ORACLE_SID=XE
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe
export PATH=$ORACLE_HOME/bin:$PATH

/etc/init.d/oracle-xe status
/etc/init.d/oracle-xe start 

. /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh


# 18c
alter session set "_ORACLE_SCRIPT"=true;
create user USER1 IDENTIFIED BY USER1;

GRANT create session TO USER1;
GRANT create table TO USER1;
GRANT create view TO USER1;
GRANT create any trigger TO USER1;
GRANT create any procedure TO USER1;
GRANT create sequence TO USER1;
GRANT create synonym TO USER1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment