Veja uma solução que deve funcionar com as Plugin ExternalTools recomendado pelo @Rinzwind:
#!/bin/bash
FILE="$GEDIT_CURRENT_DOCUMENT_PATH"
FILENAME="${FILE##*/}"
EXTENSION_PRE="${FILENAME#*.}"
BASENAME="${FILENAME%%.*}"
DIRNAME="${FILE%/*}"
if [[ "$FILENAME" = "$EXTENSION_PRE" ]] # handle files without extension
then
EXTENSION=""
else
EXTENSION=".$EXTENSION_PRE"
fi
cp -v "$FILE" "$DIRNAME/${BASENAME}_copy$EXTENSION"
Testei-o para arquivos sem extensão (por exemplo, Untitled Document
), com uma única extensão (por exemplo, Untitled Document.txt
) e extensões compostas (por exemplo, Untitled Document.text.txt
).
Veja como eu o configurei com as ferramentas externas do gEdit (essa configuração mostrará a saída (detalhada) do cp no painel inferior):
Editar
Aquiestáumaexplicaçãopasso-a-passodoqueocódigofaz:
#!/bin/bash
# Note: we are using /bin/bash and not /bin/sh
# /bin/sh is the DASH shell, /bin/bash the BASH shell
#
# We need BASH to perform the string manipulations on
# the file path.
#
# More information on the differences may be found here:
# - http://stackoverflow.com/a/8960728/1708932
# - https://wiki.ubuntu.com/DashAsBinSh
FILE="$GEDIT_CURRENT_DOCUMENT_PATH" # assign the full file path provided
# by gedit to a shorter variable
# What follows are a number of bash string manipulations.
# These are very well documented in the following article:
# - http://linuxgazette.net/issue18/bash.html
# 1.) remove everything preceding (and including) the last slash
# in the file path to get the file name with its extension
# (e.g. /home/test/file.tar.gz → file.tar.gz)
FILENAME="${FILE##*/}"
# 2.) remove everything in the file name before (and including)
# the first dot to get the extension
# (e.g. file.tar.gz → tar.gz)
EXTENSION_PRE="${FILENAME#*.}"
# 3.) remove everything in the file name after (and including)
# the first dot to get the basename
# (e.g. file.tar.gz → file)
BASENAME="${FILENAME%%.*}"
# 4.) remove everything after (and including) the last slash
# in the file pathto get the directory path
# (e.g. /home/test/file.tar.gz → /home/test)
DIRNAME="${FILE%/*}"
# If there's no extension in the filename the second string manipulation
# will simply print the filename. That's why we check if $FILENAME and
# $EXTENSION_PRE are identical and only assign EXTENSION to a value if
# they aren't.
if [[ "$FILENAME" = "$EXTENSION_PRE" ]]
then
EXTENSION=""
else
EXTENSION=".$EXTENSION_PRE"
fi
# in the last step we compose the new filename based on the string
# manipulation we did before and pass it to cp
cp - v "$FILE" "$DIRNAME/${BASENAME}_copy$EXTENSION"