How to Safely Organize Directories with .bashrc and xdg-ninja?

0
0
Asked By CuriousCoder42 On

I've set up a script in my ~/.bashrc to help organize my directories and files after running xdg-ninja. I'm looking for some feedback from more experienced users to ensure it's working as it should. Here's a simplified version of what I've created, specifically focusing on the 'cargo' example:

```bash
#! /usr/bin/env dash

# xdg-ninja alias
alias 'xdg-ninja'='xdg-ninja --skip-ok --skip-unsupported' ;

# XDG Base Directory Specification
export XDG_CACHE_HOME="${HOME}/.cache" ;
export XDG_CONFIG_HOME="${HOME}/.config" ;
export XDG_DATA_HOME="${HOME}/.local/share" ;
export XDG_STATE_HOME="${HOME}/.local/state" ;

# xdgmv function to move files/directories
xdgmv () {
test "${#}" -ne '2' && return ; test -e "${1}" || return ;
if test -d "${2%}" ;
then
mv --backup='numbered' --force "${1}" "${2}" ;
else
mkdir -p "${2%}" && mv --backup='numbered' --force "${1}" "${2}" ;
fi ;
}

# Move cargo directory
xdgmv "${HOME}/.cargo" "${XDG_DATA_HOME}/cargo" &&
export CARGO_HOME="${XDG_DATA_HOME}/cargo" ;

# Clear xdgmv function
unset xdgmv ;
```

Would love to hear any thoughts on this configuration!

1 Answer

Answered By TechWhiz99 On

Your setup generally looks solid! While nothing is entirely foolproof, I don’t see major issues. Just a couple of tips:
1. It’s usually better to provide some feedback when a command fails. This helps prevent users from mistakenly thinking a command was successful when it wasn’t.
2. Try to minimize duplicated code for better maintainability. For example, you can check if the destination directory exists and create it in one go, then carry on with the move command. Here’s a quick refactor:
```bash
if test -d "${2%/*}" ;
then
mkdir -p "${2%/*}"
fi

mv --backup='numbered' --force "${1}" "${2}" ;
```

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.