You are not logged in.
Pages: 1
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
fiAt 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
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
exitDo need a way to check 0 hits and decide if not found or only command given
Offline
Pages: 1