Skip to content

Instantly share code, notes, and snippets.

@hoolymama
Created March 20, 2011 10:51
Show Gist options
  • Save hoolymama/878265 to your computer and use it in GitHub Desktop.
Save hoolymama/878265 to your computer and use it in GitHub Desktop.
tcsh style filename modifiers in bash
# in tcsh you can do stuff like this:
# set f = /foo/bar/myfile.0076.jpg
# echo $f:r:e
# --> 0076
# echo $f:h:t
# --> bar
# In bash its a mess so I wrote some functions to make it possible.
e() # the extension
E() # everything but the extension
t() # the tail - i.e. everything after the last /
T() # everything but the tail (head)
Put the functions (at the end of the gist) in .bashrc and you can have tcsh style filename modifiers in bash:
# Examples:
# These functions can accept the filename as an argument like so:
#
# f=foo/bar/my_image_file.0076.jpg
# e $f
# --> jpg
# E $f
# --> foo/bar/my_image_file.0076
#
# or accept input from a pipe, which is the feature from tcsh that I really wanted:
#
# echo $f|E|e
# --> 0076
#
# or of course, a combination:
#
# T $f|t
# --> bar
#
# and it will accept many files through the pipe:
#
# ls foo/bar/
# --> my_image_file.0075.jpg my_image_file.0076.jpg
# ls foo/bar/ |E|e
# --> 0075
# --> 0076
# Put these functions in .bashrc
# e: the extension
function e(){
if [ $# -ne 0 ]; then
echo ${1##*.}
else
while read data; do
echo ${data##*.} ;
done
fi
}
# E: everything but the extension
function E(){
if [ $# -ne 0 ]; then
echo ${1%.*}
else
while read data; do
echo ${data%.*}
done
fi
}
# t: the tail - i.e. everything after the last /
function t(){
if [ $# -ne 0 ]; then
echo ${1##*/}
else
while read data; do
echo ${data##*/} ;
done
fi
}
# T: everything but the tail (the head)
function T(){
if [ $# -ne 0 ]; then
echo ${1%/*}
else
while read data; do
echo ${data%/*}
done
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment