bash snippet



I don't know about snippets, but here are all the additions to my .bashrc:
Code:
## Added Options ##
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

# Powerline 
# Powerline is a statusline plugin 
# https://github.com/powerline/powerline
#. /usr/share/powerline/bindings/bash/powerline.sh

# Midnight Commander
# GNU Midnight Commander is a visual file manager
# https://midnight-commander.org/
. /usr/lib/mc/mc.sh

# ibus-typing-booster
# Description: Completion input method to speedup typing
# https://mike-fabian.github.io/ibus-typing-booster/documentation.html#adding-to-desktop
 export GTK_IM_Module=ibus
 export XMODIFIERS=@im=ibus
 export QT_IM_Modules=ibus

# thefuck https://github.com/nvbn/thefuck
# Description: spelling corrector of console commands
# eval $(thefuck --alias)
# You can use whatever you want as an alias, like for Mondays:
# eval $(thefuck --alias FUCK)
# Experimental mode https://github.com/nvbn/thefuck#experimental-instant-mode
# eval $(thefuck --alias --enable-experimental-instant-mode)

# fzf https://github.com/junegunn/fzf
# fzf is a general-purpose command-line fuzzy finder
# Info /usr/share/doc/fzf/README.Debian
# Enable fzf keybindings for Bash 
source /usr/share/doc/fzf/examples/key-bindings.bash
# Enable fuzzy auto-completion for Bash (Not needed on Debian)
# source /usr/share/doc/fzf/examples/completion.bash

# Fix PATH
PATH=$PATH:/home/craig/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/craig/.local/bin:

# fzf-tab-completion: Tab completion using fzf
# https://github.com/lincheney/fzf-tab-completion
source /home/craig/fzf-tab-completion/bash/fzf-bash-completion.sh
bind -x '"\t": fzf_bash_completion'

# Additional key bindings for fzf, primarily Bash.
# https://github.com/atweiden/fzf-extras
[[ -e "$HOME/.fzf-extras/fzf-extras.sh" ]] \
  && source "$HOME/.fzf-extras/fzf-extras.sh"
  
#Shellfirm https://github.com/kaplanelad/shellfirm
# Source our file at the end of our bash profile (e.g. ~/.bashrc, ~/.profile, or ~/.bash_profile)
#echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc

# Load pre-exec command on shell initialized
#echo 'source ~/.shellfirm-plugin.sh' >> ~/.bashrc[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh
source ~/.shellfirm-plugin.sh

[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

. "$HOME/.cargo/env"

source ~/.bash_completion/alacritty
source ~/.bash_completion/rustup
source ~/.bash_completion/cargo

# walk https://github.com/antonmedv/walk
function lk {
  cd "$(walk "$@")"
}

### Added by surfraw. To remove use surfraw-update-path -remove
    export PATH=$PATH:/usr/lib/surfraw
### End surfraw addition.

alias dquilt="quilt --quiltrc=${HOME}/.quiltrc-dpkg"
. /usr/share/bash-completion/completions/quilt
complete -F _quilt_completion -o filenames dquilt

#Zoxide https://github.com/ajeetdsouza/zoxide?tab=readme-ov-file
eval "$(zoxide init bash)"
 
Show available TLS versions. Important for connecting to older web servers.
Code:
openssl ciphers -v | awk '{print $2}' | sort | uniq

I use to think this :

command | sort | uniq

is the same as:

command | sort -u

Is it ?
 
find /some/directory -mount -type f -size +10000000c -exec ls -l {} \; 2> /dev/null
 
Exit programs immediately and hard using a pattern, e.g. the name (killall -9 ).

Code:
kill -9 $(ps aux | grep '[PROGRAMNAME]' | grep -v 'grep' | awk '{print $2}') 1>/dev/null 2>/dev/null
kill -9 $(ps aux | grep 'freetube' | grep -v 'grep' | awk '{print $2}') 1>/dev/null 2>/dev/null #Example exit freetube
 
@Diputs was makes this command ?
It looks for big files in the path given, or subdirectories and then displays the meta info of them
The mount parameter makes it to avoid looking into other mountpoints (disk) if they happen to be mounted anywhere in the path structure given. Especially interesting if you ever search in root ( / ) but only want to look into the mount of only that one, not in all other disks.
 
Exit programs immediately and hard using a pattern, e.g. the name (killall -9 ).

Code:
kill -9 $(ps aux | grep '[PROGRAMNAME]' | grep -v 'grep' | awk '{print $2}') 1>/dev/null 2>/dev/null
kill -9 $(ps aux | grep 'freetube' | grep -v 'grep' | awk '{print $2}') 1>/dev/null 2>/dev/null #Example exit freetube

Aah, I've seen many bugs with these kind of constructs, and ALWAYS the problem was that it was killing processes it shouldn't because the filter isn't good enough.
I see you redirect all info to null, which means : I will not know what goes wrong when it goes wrong.

Instead I would ask myself: why do you need to kill processes ? Good software never kills processes, so that's a clue.
Don't get me wrong, it's good there is a kill command, but it's a command you should avoid.

One good think about Linux is that it WILL display when a process is killed, also on the box on which the kill was NOT done - or, that is my experience. So if you happen to have a box open with that server/machine, you see which process just was killed. You can't do anything about it, but you know WHEN it was killed, which is often a clue as you know or might know WHAT else was running at that time.
 
Determine the ram of the graphics card
Code:
lspci -v -s "$(lspci | grep -i 'VGA compatible controller' | head -n 1 | awk '{print $1}')" | grep -ie 'Memory at .* prefetchable'
Whilst the command offered shows a form of memory, it is the "prefectchable" memory which is distinct from the graphics card memory.

For example, the following is output by the command from a machine here:
Code:
$ lspci -v -s "$(lspci | grep -i 'VGA compatible controller' | head -n 1 |   awk '{print $1}')" | grep -ie 'Memory at .* prefetchable'              
        Memory at 4000000000 (64-bit, prefetchable) [size=128M]         
        Memory at 4008000000 (64-bit, prefetchable) [size=32M]

However the memory of the graphics card itself is much greater than the 128M and 32M shown in that output. When purchased, the graphics card was described as having 2G memory!

To find the memory of the graphics card itself, the following command usually provides it:
Code:
$ dmesg | grep -i vram                                                  
<snip>                                                                  
[    4.516772] nouveau 0000:01:00.0: DRM: VRAM: 2048 MiB
The output shown here of 2048MiB approximates to the 2G of the original specification of the graphics card by the manufacturer.

Prefetchable memory is separate from the graphics card memory. It's a type of memory that can be accessed by the CPU in advance of the CPU's need for the data. The CPU has the capacity to predict which data will be needed next and "prefetches" it into the CPU cache. This is done to try and improve performance by reducing the number of times the CPU has to wait for data to be fetched from main memory.

The graphics card memory, on the other hand, which is also known as VRAM (video random access memory), is a type of memory that is specifically designed for use by graphics cards, and in the graphics card itself. VRAM is optimised for high-speed data access and processing. That's important for rendering graphics images and videos. Videos move and graphics cards have to cope. Graphics card VRAM is usually larger in capacity than prefetchable memory, since it stores more data to use for the work it has to do. Modern graphics cards have all sorts of complicated stuff they do which probably explains why their memory capacity has increased over the years.

Here's a quote from my notes on the matter:
"The main difference between prefetchable memory and graphics card memory is that prefetchable memory is general-purpose memory that can be used by any device on the system, while graphics card memory is specifically designed for use by graphics cards."
 
Last edited:
Prevent servers or PCs with a graphical user interface from going into power-saving mode and then no longer being accessible because they switch off the network card (happened to one of my Dedis already :( )

Code:
systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

to revert this.

Code:
systemctl umask sleep.target suspend.target hibernate.target hybrid-sleep.target
 
Just some tips when you see this code of code (which I see a lot)

cd /some/path
./some_command parameters

If you don't know or want to use the PATH variable, and you need to run just a command like in the way as stated above, you CAN try to see if it also works this way:

/some/path/some_command parameters parameters

It depends from the type of command/script, but often it works as shown just above here.
Some programs look for source code or source files or whatever, from the current working path. I know that Perl is typical for that, but it may actually be anything.
In the second example, the thing that is missing there is that you don't assure to be located in a specific path. And if that is needed, the command/script may not work.
But often I see calls like this, and I KNOW that the command doesn't care from what path it is being run. To be honest, I think that should be the correct behavior anyway, but that is something else. Many many Linux commands don't care about the current working path, unless of course the action you perform actually relates to the path in a way or another.

So, bottom line, TRY to use the combined, one-liner command, and see if it works. If it does for that command, you know you can omit the CD command and the line-feed for another command.

Obviously, if the path in question is part of the PATH variable ... things are different, but then you are one level of experience up already, and you know you can omit the path of ' some_command ' anyways.
 
Use sed to remove lines that match a search pattern.
Code:
sed -i "/PATTERN/d" FILE
That command will effectively delete the original file, so if the user makes an error, or wishes to retrieve the original file, it isn't there and it's contents are lost.

There are a few safer alternatives.

One could run the sed command to retain the original file but create an output file with the changes and give the output file a related name, so that both the original and the changed files exist. Then the user can choose to delete the old file if all's well. It's a safeguard. For example:
Code:
sed -e '/PATTERN/d' FILE > FILE.new

This will leave the original FILE intact, and create the file with the changes: FILE.new.

There's quite a bit on the subject of sed in the forum for the interested reader:






 
Last edited:


Top