linux

Bash

remove files with chinese character AND spaces

#./[机器学习]Introduction to Machine Learning.pdf
#./算法竞赛入门经典(第2版) (算法艺术与信息学竞赛).pdf

find . | grep -P '[\p{Han}]'|xargs -I '{}' rm "{}"

remove space in filename

prename is a perl rewrite of rename. On archlinux its called perl-rename and needs to be installed. Its prebuilt on debian:

find . -name '*.mkv' -exec prename 's/ +/_/g' {} +

Create empty files at minimum depth 4

find . -mindepth 4  -type d -exec touch {}/state.json \;

create a securized user

useradd -m -G users -s /bin/bash <the user>

run command for a securized user

sudo -Hu <the user> <the command>

Git Prompt

Shows details about git project when in the folder

git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt --depth=1

# add to bashrc
if [ -f "$HOME/.bash-git-prompt/gitprompt.sh" ]; then
    GIT_PROMPT_ONLY_IN_REPO=1
    source $HOME/.bash-git-prompt/gitprompt.sh
fi

Improved ls

alias ll="exa -l --git --all --octal-permissions --sort modified --time-style long-iso --group --color=auto --color-scale"

Edit a file without vi

cat - > /path/to/file
# paste the content
# then ctrl+D

Create a tunnel

ssh -fNTML 9432:localhost:5432 sshusername@you-server.com
Then, just launch psql, connecting to port 9432 at localhost:

psql -h localhost -p 9432 -U <username> <dbname>

Get file magic number

18:42 $ file -i assets/images/puml.*
assets/images/puml.png:  image/png; charset=binary
assets/images/puml.webp: image/webp; charset=binary

Get last created folders in current path

find . -ctime -1|cut -d'/' -f2|sort -u

Tar

Tar from find

tar czf <file>.tgz `find . -name '*.json' -maxdepth 1`

Tar from find

tar czf <file>.tgz <folder> --exclude=<path/to/*>

Find exec multi args command

  1. write a script in the path
  2. make it executable
  3. find . -exec ./script {} ;

Make sure files created in folder inherit group ownership

chmod g+s <path/to/dif>

Rsync in place of scp

Rsync handle files with spaces in it. Just add s=--protect-args and quote the string

rsync -avs user@host:"path with spaces" /tmp/

Update apt keys

for PUBKEY in $(apt-get update 2>&1 | grep NO_PUBKEY | awk '{print $NF}')
do
 wget -q "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x${PUBKEY}" -O - | sed -n '/BEGIN/,/END/p' | apt-key add - 2>/dev/null
done

Restore deleted file

grep -a -C 2000 "a-string-present-in-the-file" /dev/sda2 | tee recovered_data

source

Kill a background task

ctrl+z # put current task in background
jobs -p|xargs kill

Config ssh for git

To avoid beeing able to connect with ssh

# create a blind user
sudo adduser  --disabled-password --shell /usr/bin/git-shell --gecos "User" git
# allow local user to access the folder
sudo usermod -a -G git natus
# init a git repo accessible by remote
git init --bare
# and in the authorized key, prefix with
no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa <then-rsa-public-key>

also secure ssh:

if [ -z "$(command grep "^ssh-users" '/etc/group')" ]; then
  command addgroup --system "ssh-users"
fi

sudo usermod -a -G ssh-users natus
command echo '
# Limit access to users of group ssh-users
AllowGroups ssh-users' >> /etc/ssh/sshd_config

Benchmark command line

hyperfine <command>

Know if using x11 or wayland

loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type

Get last modified files in any folder

# this excludes hidden files
find * -type f -printf "%T+ %p\n" | sort

Start a xsession on a server

from startx

As root, this will start a session for a given user:

# systemd-run --property PAMName=login \
--property User=<user-name> \
--property StandardInput=tty \
--property TTYPath=/dev/tty8 \
sh -c 'chvt 8 && startx /usr/bin/xterm -- :1'

Then the user is able to start programs with X support:

DISPLAY=:1 /usr/bin/chromium "https://blog.parisni.com"

from vnc server

Follow vnc install, in particular:

DISPLAY=:2 /usr/bin/chromium "https://blog.parisni.com"

React ?

This page was last modified: