Skip to content

Instantly share code, notes, and snippets.

@kebman
Created February 12, 2017 22:43
Show Gist options
  • Save kebman/81e511780bb747fcb3fa545d1587affb to your computer and use it in GitHub Desktop.
Save kebman/81e511780bb747fcb3fa545d1587affb to your computer and use it in GitHub Desktop.
Examples on how to manipulate Files and Folders with Bash
#!/bin/bash
# ff.sh ver 1.0
# Manipulate Files and Folders with Bash…
# minimal check of correct amount of input arguments
if [[ $# -eq 0 ]] ; then
echo "Use a file with a valid file extension, then do:"
echo "bash ff.sh [testfile.ext]"
exit 0
fi
# folder to look in
FOLDER="."
# full path to file
DIRFILE=$FOLDER"/$1"
echo $DIRFILE
# just the directory
JUSTDIR=$(dirname $DIRFILE)
echo $JUSTDIR
# just the filename and extension without path
JUSTFILE=$(basename $DIRFILE)
echo $JUSTFILE
# just the filename with extension v2
JUSTFILE2="${DIRFILE##*/}"
echo $JUSTFILE2
# just the filename without extension
JUSTNAME="${JUSTFILE%.*}"
echo $JUSTNAME
# just the extension without the filename
EXTENSION="${JUSTFILE##*.}"
echo $EXTENSION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment