Skip to content

Instantly share code, notes, and snippets.

@darioseidl
Last active September 7, 2020 09:03
Show Gist options
  • Save darioseidl/722f40a99ced2e247e1d9731dfbd9c8f to your computer and use it in GitHub Desktop.
Save darioseidl/722f40a99ced2e247e1d9731dfbd9c8f to your computer and use it in GitHub Desktop.
Some useful shell commands and tools

Remove .txt extension from all files in working directory

for f in $(ls); do move $f ${f%.txt}; done

Add .txt extension to all files in working directory

for f in $(ls); do move "$f" "${f}.txt"; done

Get first non-empty line in $f

header=$(grep . "$f" | head -n1)

Escape forward slahes in variable $path

${path//'/'/'\/'}

e.g.

path="/etc/apt/sources.list"; echo ${path//'/'/'\/'}

returns

\/etc\/apt\/sources.list

Replace A with B in all files in working directory

for f in $(ls); do sed 's/A/B/g' < $f > tmp; move tmp "$f"; done

Replace colors in nanorcs

cd ~/.nano
for f in $(ls); do sed 's/color brightwhite/color brightgreen/g' < $f > tmp; move tmp $f; done
for f in $(ls); do sed 's/color brightyellow/color brightgreen/g' < $f > tmp; move tmp $f; done
for f in $(ls); do sed 's/color white/color green/g' < $f > tmp; move tmp $f; done
for f in $(ls); do sed 's/color yellow/color green/g' < $f > tmp; move tmp $f; done

Pygmentize

Preview all styles for lexer 'sh', formatter 'terminal256'

styles="monokai manni perldoc borland colorful default murphy vs trac tango fruity autumn bw emacs pastie friendly native";\
for s in $styles; do echo; echo "$s:"; tail ~/.bashrc | pygmentize -f 256 -l sh -O style=$s; done

Use pygmentize with lexer 'sh', formatter 'terminal256' in combination with 'less -R'

pygmentize -f terminal256 -l sh -F whitespace:spaces=True,tabs=True -O style=borland ~/.bashrc | less -R

Recursive grep with max depth

grep TEXT $(find -maxdepth 1 -type f)

Batch convert with imagemagick

e.g. convert SVGs to PNGs:

for img in *svg; do base=`basename $img .svg`; convert "$img" "$base.png"; done;

Print multiple pdf pages per sheet

pdfnup <sourcefile> --nup 2x3 --frame true --delta "1cm 1cm" --scale 0.91

Check if device is mounted

use mountpoint command to see if a directory is a mount point e.g.

if ! mountpoint -q /mnt/backup; then
    mount /mnt/backup
fi

or use grep -c (count) and mount e.g.

if [ $(mount | grep -c /mnt/backup) != 1 ]; then
    mount /mnt/backup
fi

Environment variables for bash

for login shell: bash reads first /etc/profile for values that are defined for all users. afterwards it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

for non-login shell: If you start another shell within the login shell (yes that's possible), the second one is a non-login shell. it will not read named start-up files but searches non-login start-up script from user's home directory instead. with bash this is called ~/.bashrc.

to avoid specifying same values in two places, make the login-shell start-up script ~/.bash_profile includes the ~/.bashrc at the end of its execution. to implement include following into your ~/.bash_profile:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc;
fi

my current setup:

  • system-wide config in /etc/profile
  • user config (login) in ~/.profile (no ~/.bash_profile -> this is important, otherwise ~/.profile would not be read)
  • user config (non-login) in ~/.bashrc, which is also executed from ~/.profile
  • manually written user config for environment variable in ~/.bash_environment, which is executed from ~/.bashrc
  • manually written user config for aliases in ~/.bash_aliases, which is executed from ~/.bashrc

to add environment variables add the following line in ~/.bash_environment

export VARNAME=VALUE

or to append a value to an existing variable

export VARNAME=$VARNAME:VALUE

to add an alias add the following line in ~/.bash_aliases

alias ALIASNAME='COMMAND'

e.g.

alias ll='ls -l'

1 2

Some useful shell-related commands

show current shell

$ echo $SHELL

list installed shells

$ cat /etc/shells

change login shell

$ chsh

SSH file system (sshfs)

install the 'sshfs' package, then add your user to the fuse group

$ sudo adduser <username> fuse

then restart the session, and create a mountpoint with

$ sshfs <username>@host:hostpath localpath

if a user or group on the host does not exist on the client, you can map the file permissions to another user/group. use

$ sshfs <username>@host:hostpath localpath -o idmap=user -o uid=<userid> -o gid=<groupid>

the id command is useful in this case:

$ sshfs <username>@host:hostpath localpath -o idmap=user -o uid=$(id -u) -o gid=$(id -g)

1 2

SSH without passwort

First log in on A as user a and generate a pair of authentication keys. Do not enter a passphrase:

a@A:~> ssh-keygen -t rsa

Now use ssh to create a directory ~/.ssh as user b on B. (The directory may already exist, which is fine):

a@A:~> ssh b@B mkdir -p .ssh

Finally append a's new public key to b@B:.ssh/authorized_keys and enter b's password one last time:

a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'

From now on you can log into B as b from A as a without password

http://linuxproblem.org/art_9.html

Split lossless audio by cue file

install 'shntool' ('flac' and 'cuetools' will get installed too) and split with

cuebreakpoints sample.cue | shnsplit -o flac sample.flac

for ape install 'monkeys-audio' (from debian-multimedia) and split with

cuebreakpoints sample.cue | shnsplit -o ape sample.ape

1

Convert ape to flac

install 'shntool' and 'monkeys-audio' (from debian-multimedia) convert with

shntool conv -o flac *.ape

or

shnconv -o flac *.ape

1

Default browser on Debian

  1. update-alternatives

for the GUI browser

sudo update-alternatives --config x-www-browser
sudo update-alternatives --config gnome-www-browser

for the cli browser

sudo update-alternatives --config www-browser

xdg-open and sensible-browser should open the selected browser now

  1. desktop environment defaults

Xfce4: xfce4-settings (Preferred Applications)

Gnome: gnome-control-center (System Settings -> Details -> Default Applications)

KDE: systemsettings (KDE System Settings -> Applications)

set the browser for text/html and application/xhtml+xml

Kupfer and Thunderbird/Icedove use the Gnome setting, Yakuake uses the KDE setting.

  1. BROWSER environment variable

you can also set the BROWSER environment variable, which is used as the first choice in sensible-browser

  1. /etc/mailcap

add your browser in ~/.mailcap or in /etc/mailcap.order

1

Send a mail with swaks

swaks \
 --to 'ping@tools.mxtoolbox.com' \
 --from 'FROM' \
 --server 'smtp.world4you.com' \
 --auth 'LOGIN' \
 --auth-user 'FROM' \
 --auth-password 'PASSWORD' \
 -tls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment