Skip to content

Instantly share code, notes, and snippets.

@TheFern2
Created September 22, 2019 07:15
Show Gist options
  • Save TheFern2/4d3d57f50a7ce48057081d9885abce78 to your computer and use it in GitHub Desktop.
Save TheFern2/4d3d57f50a7ce48057081d9885abce78 to your computer and use it in GitHub Desktop.
Linux Things

Aliases

In order to keep bashrc or bash_profile clean, aliases should go in the bash_aliases file. Add below IF to bashrc.

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

Then add aliases to bash_aliases:

alias my_alias='whatever command here'

Custom bash functions:

function_name () {
    command1
    command2
}

As an example I had built this to sort a csv file without sorting the header:

# funtion_name starting_row input_file output_file
sort_csv(){
        start_row=$1
        last_row=$1
        input_file=$2
        output_file=$3
        let "last_row -= 1"
        #echo $start_row
        #echo $last_row
        head -n $last_row $input_file > $output_file && tail -n $start_row $input_file | sort -k 1 >> $output_file      
}

sort_csv 6 sample.csv sorted.csv

Install npm packages globally without sudo on macOS and Linux

npm installs packages locally within your projects by default. You can also install packages globally (e.g. npm install -g <package>) (useful for command-line apps). However the downside of this is that you need to be root (or use sudo) to be able to install globally.

Here is a way to install packages globally for a given user.

1. Create a directory for global packages
mkdir "${HOME}/.npm-packages"
2. Tell npm where to store globally installed packages
npm config set prefix "${HOME}/.npm-packages"
3. Ensure npm will find installed binaries and man pages

Add the following to your .bashrc/.zshrc:

NPM_PACKAGES="${HOME}/.npm-packages"

export PATH="$NPM_PACKAGES/bin:$PATH"

# Unset manpath so we can inherit from /etc/manpath via the `manpath` command
unset MANPATH # delete if you already modified MANPATH elsewhere in your config
export MANPATH="$NPM_PACKAGES/share/man:$(manpath)"

Check out npm-g_nosudo for doing the above steps automagically


NOTE: If you are running macOS, the .bashrc file may not yet exist, and the terminal will be obtaining its environment parameters from another file, such as .profile or .bash_profile. These files also reside in the user's home folder. In this case, simply adding the following line to them will instruct Terminal to also load the .bashrc file:

source ~/.bashrc

See also: npm's documentation on "Fixing npm permissions".

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