You are not logged in.

Announcement

Due to heavy spamming of forums registration is going in stages. If you wish to register as a new user with ArchBang Forums, first register and then send an e-mail to: archbangforums at gmail dot com. It should contain the problem you want to discuss or some other AB related content. You will be promoted from registering member with no posting rights to new member with posting rights after that. If your mail is ignored you haven't fulfilled the requirements.

#1 2012-07-03 07:26:12

Mr Green
Iso Developer
Registered: 2010-11-07
Posts: 3,748

Extended cd [bash] command

Working on a little script that will allow user to get to the directory a given file is in.

#!/usr/bin/env bash
# 

if [ -z "$1" ];
then 
  echo $"Usage: $0 <file_name>"
  exit
fi

filey=$(find $HOME -name "$1" -type f)

if [ $filey ]; 
then
  # Going to put cd command on next line
  # cd <path_to>/$filey
  echo $(dirname $filey)
else
  echo "Not found..."
  # Exit here
  exit
fi

At the moment it only displays a message if file/directory is found.

Not to sure of the way to go yet either open a terminal in the directory of the found file or run cd from .bashrc as a function.

Hoping someone might find a use for it

Offline

#2 2012-07-10 05:36:50

Mr Green
Iso Developer
Registered: 2010-11-07
Posts: 3,748

Re: Extended cd [bash] command

Version 2 using a case command to sort output from search

#!/usr/bin/env bash
# cdx  


list()
{
for i in $filey; do
  echo ${i}
done
}

cd_dir()
{
echo $(dirname $filey) 
}


[[ $# -eq 0 ]] && echo "Usage: $0 <file_name>" && exit 1

filey=$(find $HOME -type f -name "$1")

case $(echo $filey | wc -w) in
  0) echo "Not found..." # File not found
  ;;
  1) cd_dir # Cd to found directory
  ;;
  *) list # List if you find more than one file with same name
  ;;
esac

exit

Do need a way to check 0 hits and decide if not found or only command given

Offline

Board footer

Powered by FluxBB