Leave a comment

A Better Link

Warning: This script needs help, don’t use it :)

I started on this thinking this would be an easy task. See, I can never remember the order I have to do a link (ln -s file/folder location-to), so I decided to build a script that would give me usage and display the link once it’s been made. This turned out to be a pretty good task as: ‘ln’ needs a path if trying to link a file in the current directory, getting a full path isn’t readily available and considering names that have spaces in them.

Anyways, enough of the chatter. Here’s ‘lnk’. It will display usage if no arguments are given, display the link when done, and check to see if the name already exists.

You’ll need likely to install ‘realpath‘ as it isn’t installed on most distributions by default.

#!/bin/bash
# lnk - link files/folders without broken links and feedback
# Author: Gen2ly

# Text color variables
TXTBLD=$(tput bold)     # Bold
TXTUND=$(tput sgr 0 1)  # Underline
TXTRED=$(tput setaf 1)  # Red
TXTGRN=$(tput setaf 2)  # Green
TXTYLW=$(tput setaf 3)  # Yellow
TXTBLU=$(tput setaf 4)  # Blue
TXTPUR=$(tput setaf 5)  # Purple
TXTCYN=$(tput setaf 6)  # Cyan
TXTWHT=$(tput setaf 7)  # White
TXTRST=$(tput sgr0)     # Reset

# Display usage if full argument if isn't given.
if [[ -z "$2" ]]; then
  echo " lnk <file-or-folder> <link-to-location>"
  exit
fi

# Check if file or folder exists
if [[ ! -f $1 ]] && [[ ! -d $1 ]]; then
  echo " File/folder does not exist"
  exit
fi

# Variables to check if link points to a folder or to a new link
LINKDIR=${2%/}/${1##*/}
LINKNEW=$2

# Check if the link name matches another link
if [[ -L $LINKDIR ]] || [[ -L $LINKNEW ]]; then
  echo " Link already exists:"
  if [[ -L $LINKDIR ]]; then
    echo " $(ls -la --color=always $LINKDIR | awk '{printf $8" "$9" "$10}')"
  fi
  if [[ -L $LINKNEW ]]; then
    echo " $(ls -la --color=always $LINKNEW | awk '{printf $8" "$9" "$10}')"
  fi
  exit
fi

# Check if link name matches a file name
if [[ -f $LINKDIR ]] || [[ -f $LINKNEW ]]; then
    echo " File already exists with that name:"
  if [[ -f $LINKDIR ]]; then
    echo " $(ls -la --color=always $LINKDIR | awk '{printf $1" "$8}')"
  fi
  if [[ -f $LINKNEW ]]; then
    echo " $(ls -la --color=always $LINKNEW | awk '{printf $1" "$8}')"
  fi
  exit
fi

# Create symbolic link
#'ln' needs path argument for linking a file in the current directory
# realpath extracts '\'s before spaces
FPATH=`realpath "$1" | sed -e 's: :\\ :g'`
ln -s "$FPATH" $2

# Display colors for full file path, link same path, link new path
FPATHDIS=${TXTBLD}${TXTGRN}$FPATH${TXTRST}
LINKDIRDIS="${TXTBLD}${TXTCYN}$(realpath -s "$LINKDIR")${TXTRST}"
LINKNEWDIS="${TXTBLD}${TXTCYN}$(realpath -s $LINKNEW)${TXTRST}"

# Display linked file
if [[ -L $LINKDIR ]]; then
  echo " $FPATHDIS -> $LINKDIRDIS"
fi

if [[ -L $LINKNEW ]]; then
  echo " $FPATHDIS -> $LINKNEWDIS"
fi

# Limitations
# because lnk must check if the link points to a folder or a new linkname
# if creating a link of a file that has the same name as a directory
# and the link has the same name as a link in that directory, lnk will
# fail from a link check
About these ads

About Gen2ly

<3's linux

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers

%d bloggers like this: