I'm currently trying to cleanup my home directory by moving files like .vimrc
, .bash_profile
, etc. to a directory .dotfiles
in my home directory.
The idea is to use symbolic links to these files afterwards:
ln -s ~/.dotfiles/.vimrc ~/.
This works quiet fine, but I'd like to automate this process by writing my first bash script and I ran into some trouble.
The script currently looks something like this:
#!//bin/bash
# Make script executable with: chmod u+x brew.sh
# Ask for the administrator password upfront.
sudo -v
# Keep-alive: update existing `sudo` time stamp until the script has finished.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
# Create '.other'-folder
echo "--> ~/.other"
if [ -d ~/.other ];
then
echo "Directory ~/.other exists..."
else
echo "Creating directory ~/.other..."
mkdir ~/.other
fi
echo ""
# TRASH
echo "--> ~/.Trash"
if [ -d ~/.Trash ];
then
echo "Directory ~/.Trash does exists. Moving it to ~/.other..."
mv ~/.Trash ~/.other/
else
echo "Directory ~/.Trash doesn't exists. Creating it in ~/.other..."
mkdir ~/.other/.Trash
fi
echo "Linking ~/.other/.Trash to ~/.Trash..."
ln -s ~/.other/.Trash ~/.
echo ""
# BASH_HISTORY
echo "--> ~/.bash_history"
if [ -a ~/.bash_history ];
then
echo "File ~/.bash_history does exists. Moving it to ~/.other..."
mv ~/.bash_history ~/.other/
else
echo "File ~/.bash_history doesn't exists. Creating it in ~/.other..."
touch ~/.other/.bash_history
fi
echo "Linking ~/.other/.bash_history to ~/.bash_history..."
ln -s ~/.other/.bash_history ~/.
echo ""
# BASH_SESSIONS
echo "--> ~/.bash_sessions"
if [ -d ~/.bash_sessions ];
then
echo "Directory ~/.bash_history does exists. Moving it to ~/.other..."
mv ~/.bash_sessions ~/.other/
else
echo "Directory ~/.bash_history doesn't exists. Creating it in ~/.other..."
mkdir ~/.other/.bash_sessions
fi
echo "Linking ~/.other/.bash_sessions/ to ~/.bash_sessions/..."
ln -s ~/.other/.bash_sessions ~/.
echo ""
# .LOCAL
echo "--> ~/.local"
if [ -d ~/.local ];
then
echo "Directory ~/.local does exists. Moving it to ~/.other..."
mv ~/.local ~/.other/
else
echo "Directory ~/.local doesn't exists. Creating it in ~/.other..."
mkdir ~/.other/.local
fi
echo "Linking ~/.other/.local/ to ~/.local/..."
ln -s ~/.other/.local ~/.
echo ""
# .CONFIG
echo "--> ~/.config"
if [ -d ~/.config ];
then
echo "Directory ~/.config does exists. Moving it to ~/.other..."
mv ~/.config ~/.other/
else
echo "Directory ~/.config doesn't exists. Creating it in ~/.other..."
mkdir ~/.other/.config
fi
echo "Linking ~/.other/.config/ to ~/.config/..."
ln -s ~/.other/.config ~/.
echo ""
As you can see the code is pretty repetitive, but first things first. The code should work roughly like this. Check whether file (for example init.vim
) exists in my home directory. If it does exists move it to either ~/.other
(not so important files) or to ~/.dotfiles
(important files). If it doesn't exist create a file (or directory) in ~/.dotfiles
or ~/.other
. Symlink afterwards.
So far the theory. The problem is, if the file does't exist in my home-directory yet - the script just creates a file in ~/.dotfiles
/~/.other
, and links the name in the home directory to it. This doesn't really work out in practice, since some files need specific permissions. For example neovim
doesn't recognize some files if they are created with this script and it's not very efficient to just create the files before they are even used.
Is there a way to fix this (for example by creating a link, without creating the target file - I tried it once to link .bash_history
to .other/.bash_history
, the link worked fine, but bash
couldn't write to a non existing file)? It would be optimal if new files would be created in the right place and I only need to specify the right place before?
PS: When the file already exists the script works fine (it just moved the file to the new location and links it).
touch
command that will create the file if it doesn't exist, andchmod
to give it the appropriate permissionscleanup
and it loops over all files in the home directory and asks the user if he wants to move it to~/.dotfiles
or~./other
, but then the user had to actually do something and had to execute the code multiple times (if knew files are added to the home directory - I can't prevent the creation in the home directory).permission denied
, when I tried to create a dir. I'm honestly not sure about it. I should probably remove it. Thanks!