bash

bash - string / file name manipulation

In the following some useful bash snippets to manipulat strings / file names are listed.

Extract substrings

1. crop a consecutive substring ${string:position:length}

name=abcd123FGHI;
subStr=${name:4:3};
echo $subStr; # result is 123

2. remove beginning or end of a string

str=/path/to/my/file/123_complicated_9034.txt;

removes shortest matching suffix pattern %

safer use of the shell / terminal

I included following lines into my ~/.bashrc to have a more secure shell experience.

1) Do not remove (unlink) a file, but move it to the trash-can. You have to install trash-cli,
 

sudo apt-get install trash-cli
alias rm="echo 'NOTE: !! not removing, but moving to trash-can!! '; trash-put"

If you want to use the normal rm command, use

/bin/rm file1ToRemove.txt file2ToRemove.png

2) ask before overwriting a file, if you accidentaly copy or move a file with the same name.