onlyforbopi
1/2/2017 - 8:42 AM

Linux.Bash.File.FileDirProcess

Linux.Bash.File.FileDirProcess #linux #bash #sh #file #directory #folder #wordcount #sizecount #charcount #usage #diskusage #iterate

BASH_FILE_DIRECTORY

typeoffil=`file $FILE_IN`
echo "File Type      = $typeoffil"
sizecount=`ls -lah $FILE_IN | awk -F " " '{print $5}'`
echo "Sizecount      = $sizecount2"
echo -n "Enter file name : "
read file
 
# find out if file has write permission or not
[ -w $file ] && W="Write = yes" || W="Write = No"
 
# find out if file has excute permission or not
[ -x $file ] && X="Execute = yes" || X="Execute = No"
 
# find out if file has read permission or not
[ -r $file ] && R="Read = yes" || R="Read = No"
 
echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"
md5sumrep=`md5sum $FILE_IN | awk -F " " '{print $1}'`
echo "MD5sum         = $md5sumrep"
linecount=`wc -l $FILE_IN | awk -F " " '{print $1}'`
echo "Linecount      = $linecount"

#######################################################
# There are many ways to get absolute path of file

# Way 1
readlink -f file.txt

# Way 2 - Works
echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")
path=`echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")`

# Way 3 - call on python script
python -c 'import os; print(os.path.abspath("cat.wav"))'

# Way 4 - call using grep and find
find $PWD -type f | grep "filename"
find $PWD -type f -name "*filename*"
find / -samefile file.txt -print

# Way 5 - Function form
getAbsolutePath(){
    [[ -d $1 ]] && { cd "$1"; echo "$(pwd -P)"; } || 
    { cd "$(dirname "$1")" || exit 1; echo "$(pwd -P)/$(basename "$1")"; }
}
#!/bin/bash

#findpipe.bsh



# Looks into the current directory for the file, XXXXX can be filename, regexp, wildcard, ie fil*, *ile, *.txt
find ~ -name XXXXX 

# Looks into the root directory for the file, XXXXX can be filename, regexp, wildcard, ie fil*, *ile, *.txt
find / -name XXXXX

# Pipe the output of find into other commands with xargs
find -name XXXXX | xargs stat
find -name XXXXX | xargs md5sum
find -name XXXXX | xargs wc
find -name XXXXX | xargs file
find -name XXXXX | xargs du
flineleng=`wc -L $FILE_IN | awk -F " " '{print $1}'`
echo "Longest Line   = $flineleng"
fiencodin=`file -bi $FILE_IN`
echo "Encoding       = $fiencodin"
fdiskusag=`du -hca $FILE_IN | awk -F " " ' NR==2 {print $1}'`
CreateTempfileName ()  # Creates a "unique" temp filename.
{                      # From "ex51.sh" example.
  prefix=temp
  suffix=`eval date +%s`
  Tempfilename=$prefix.$suffix
}
count_input_dir()
{
  
  # Function    : count_input_dir
  # Description : count files in given directory
  # Input       : string - input directory
  # Output      : count of files in input directory
  # Usage       : count_input_dir %directory
  
	file_in=$1
	counter=`wc -l | awk '{ print $1 }'`
	echo $counter	
}
chk_empty_dir()
{
  target=$1
  if test "$(ls -A "$target")"; then
      echo not empty, do something
      return 0
  else
      echo The directory $target is empty '(or non-existent)'
      return 1
  fi
}
You can check if a directory is empty like this:

#!/bin/sh
target=$1
test "$(ls -A "$target" 2>/dev/null)" || echo The directory $target is empty
Or better yet:

-

#!/bin/sh
target=$1
if test "$(ls -A "$target")"; then
    echo not empty, do something
else
    echo The directory $target is empty '(or non-existent)'
fi
UPDATE

-

If the directory contains many files, this can be slow. In that case, this should be faster:

#!/bin/sh
target=$1
if find "$target" -mindepth 1 -print -quit | grep -q .; then
    echo not empty, do something
else
    echo The directory $target is empty '(or non-existent)'
fi
charcount=`wc -m $FILE_IN | awk -F " " '{print $1}'`
echo "Charcount      = $charcount"
changedir()
{
	DIR_NAME=$1
	# Check if the directory exist?
	[ -d "$DIR_NAME" ] || {
		echo Dir: $DIR_NAME does not exist 
		exit 1
	}

	# Check if the directory is readable
	[ -r "$DIR_NAME" ] || {
		echo Dir: $DIR_NAME not readable
		exit 2
	}

	# Check if we have execute perms on directory
	[ -x "$DIR_NAME" ] || {
		echo Dir: cannot cd to $DIR_NAME
		exit 3
	}

	# Check if the directory is writable
	[ -w "$DIR_NAME" ] || {
		echo Dir: $DIR_NAME not writeable
		exit 4
	}

	cd $DIR_NAME
	echo "Present directory $DIR_NAME"
}
dirsize () 
{

	# Gets directory size
	# Call as dirsize $dir_address
	
	
du -shx * .[a-zA-Z0-9_]* 2> /dev/null | \
egrep '^ *[0-9.]*[MG]' | sort -n > /tmp/list
egrep '^ *[0-9.]*M' /tmp/list
egrep '^ *[0-9.]*G' /tmp/list
rm -rf /tmp/list
}
READ A FILE INTO ARRAY

You can use a loop to read each line of your file and put it into the array

# Read the file in parameter and fill the array named "array"
getArray() {
    array=() # Create array
    while IFS= read -r line # Read a line
    do
        array+=("$line") # Append line to the array
    done < "$1"
}

getArray "file.txt"
How to use your array :

# Print the file (print each element of the array)
getArray "file.txt"
for e in "${array[@]}"
do
    echo "$e"
done
_basename() { #portable basename 
    [ -z "${1}" ] && return 1 || _basename__name="${1}" 
    [ -z "${2}" ] || _basename__suffix="${2}" 
    case "${_basename__name}" in 
        /*|*/*) _basename__name="$(expr "${_basename__name}" : '.*/\([^/]*\)')" ;; 
    esac 


    if [ -n "${_basename__suffix}" ] && [ "${#_basename__name}" -gt "${#2}" ]; then 
        if [ X"$(printf "%s" "${_basename__name}" | cut -c"$((${#_basename__name} - ${#_basename__suffix} + 1))"-"${#_basename__name}")" \ 
           = X"$(printf "%s" "${_basename__suffix}")" ]; then 
            _basename__name="$(printf "%s" "${_basename__name}" | cut -c1-"$((${#_basename__name} - ${#_basename__suffix}))")" 
        fi 
    fi 


    printf "%s" "${_basename__name}" 
} 
#==================================================== Example ==== 
#_basename /path/file #print file 
#================================================================= 


_basename /home/tede/n55115/PD/files/test.bsh
##################################
# First way - Simple
while read p; do
  echo $p
done <peptides.txt

###################################
# Second way - Different file descriptor
while read -u 10 p; do
  echo $p
done 10<peptides.txt

#!/bin/bash
filename='peptides.txt'
exec 4<$filename
echo Start
while read -u4 p ; do
    echo $p
done

##########################################
# Third Way - Using cat + pipe
cat peptides.txt | while read line
do
  echo $line
   # do something with $line here
done

###########################################
# Using FOR loop 
#!/bin/bash
filename='peptides.txt'
filelines=`cat $filename`
echo Start
for line in $filelines ; do
    echo $line
done

###########################################
# Using IFS and read -r
while IFS= read -r line; do
   echo "$line"
done <file

###########################################
# Using IFS to parse delimited files
# ':' is the delimiter here, and there are three fields on each line in the file
# IFS set below is restricted to the context of `read`, it doesn't affect any other code
while IFS=: read -r field1 field2 field3; do
  # process the fields
  # if the line has less than three fields, the missing fields will be set to to an empty string
  # if the line has more than three fields, `field3` will get the all the values, including the third field, including the delimiter(s)
done < input.txt

############################################
#Reading from more than one file at a time

while read -u 3 -r line1 && read -u 4 -r line2; do
  # process the lines
  # note that the loop will end when we reach EOF on either of the files, because of the `&&`
done 3< input1.txt 4< input2.txt

#############################################
# Reading a whole file into an array (Bash version 4+)

readarray -t my_array < my_file
or

mapfile -t my_array < my_file
And then

for line in "${my_array[@]}"; do
  # process the lines
done

##############################################
# MOST PRACTICAL WAY + SAFE

#If you don't want your read to be broken by newline character, use -

#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
    echo "$line"
done < "$1"

##############################################
# Using For + Cat

#!/bin/bash
#
# Change the file name from "test" to desired input file 
# (The comments in bash are prefixed with #'s)
for x in $(cat test.txt)
do
    echo $x
done
#APPEND AT THE BEGINNING OF Filename

The >> operator appends lines to a file. What if you wish to prepend a line to an existing file, that is, to paste it in at the beginning?

file=data.txt
title="***This is the title line of data text file***"

echo $title | cat - $file >$file.new
# "cat -" concatenates stdout to $file.
#  End result is
#+ to write a new file with $title appended at *beginning*.
#Returns true if...

#################################################
#-e file exists
if [ -e "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -e ABAP.docx  ] && echo "aaa" || echo "bbbb"


[ $# -eq 0 ] && directorys=`pwd` || directorys=$@


#################################################
#-a file exists
#This is identical in effect to -e. It has been "deprecated," [1] and its use is discouraged.
if [ -a "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -a ABAP.docx  ] && echo "aaa" || echo "bbbb"

#################################################
#-f file is a regular file (not a directory or device file)
if [ -f "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -e ABAP.docx  ] && echo "regular" || echo "irregular"



#################################################
#-s file is not zero size
if [ -s "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -s ABAP.docx  ] && echo "zero" || echo "non zero"



#################################################
#-d file is a directory
if [ -d "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -e ABAP.docx  ] && echo "directory" || echo "not a directory"



#################################################
#-b file is a block device
if [ -b "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -b ABAP.docx  ] && echo "block" || echo "non block"



#################################################
#-c file is a character device
  #device0="/dev/sda2"    # /   (root directory)
  #if [ -b "$device0" ]
  #then
  #  echo "$device0 is a block device."
  #fi

  # /dev/sda2 is a block device.
  #device1="/dev/ttyS1"   # PCMCIA modem card.
  #if [ -c "$device1" ]
  #then
  # echo "$device1 is a character device."
  #fi

  # /dev/ttyS1 is a character device.


#################################################
-p file is a pipe
#function show_input_type()
#{
#   [ -p /dev/fd/0 ] && echo PIPE || echo STDIN
#}

#show_input_type "Input"                           # STDIN
#echo "Input" | show_input_type                    # PIPE

# This example courtesy of Carl Anderson.

#-h file is a symbolic link
if [ -h "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -e ABAP.docx  ] && echo "slink" || echo "irregular"

#################################################
#-L file is a symbolic link


#################################################
#-S file is a socket
if [ -S "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -S ABAP.docx  ] && echo "regular" || echo "irregular"


#################################################
#-t file (descriptor) is associated with a terminal device
if [ -t "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -e ABAP.docx  ] && echo "regular" || echo "irregular"

#This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script is a terminal.


#################################################
#-r file has read permission (for the user running the test)
if [ -r "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -e ABAP.docx  ] && echo "regular" || echo "irregular"


#################################################
#-w file has write permission (for the user running the test)
if [ -w "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -e ABAP.docx  ] && echo "regular" || echo "irregular"


#################################################
#-x file has execute permission (for the user running the test)
if [ -x "$file" ]
then
  echo "..."
fi
# if file exists echo aaa, else echo bbbb
[  -e ABAP.docx  ] && echo "regular" || echo "irregular"

#-g set-group-id (sgid) flag set on file or directory
#If a directory has the sgid flag set, then a file created within that directory belongs to the group that owns the directory, not necessarily to the group of the user who created the file. This may be useful for a directory shared by a workgroup.

#-u set-user-id (suid) flag set on file
#A binary owned by root with set-user-id flag set runs with root privileges, even when an ordinary user invokes it. [2] This is useful for executables (such as pppd and cdrecord) that need to access system hardware. Lacking the suid flag, these binaries could not be invoked by a non-root user.
#	      -rwsr-xr-t    1 root       178236 Oct  2  2000 /usr/sbin/pppd
#A file with the suid flag set shows an s in its permissions.

#-k sticky bit set
#Commonly known as the sticky bit, the save-text-mode flag is a special type of file permission. If a file has this flag set, that file will be kept in cache memory, for quicker access. [3] If set on a directory, it restricts write permission. Setting the sticky bit adds a t to the permissions on the file or directory listing. This restricts altering or deleting specific files in that directory to the owner of those files.
#drwxrwxrwt    7 root         1024 May 19 21:26 tmp/
#If a user does not own a directory that has the sticky bit set, but has write permission in that directory, she can only delete those files that she owns in it. This keeps users from inadvertently overwriting or deleting each other's files in a publicly accessible directory, such as /tmp. (The owner of the directory or root can, of course, delete or rename files there.)

#-O you are owner of file

#-G group-id of file same as yours

#-N file modified since it was last read

#f1 -nt f2 file f1 is newer than f2

#f1 -ot f2 file f1 is older than f2

#f1 -ef f2 files f1 and f2 are hard links to the same file

#!"not" -- reverses the sense of the tests above (returns true if condition absent).

# Function Form:


function FileChecksExp(){


	FILE_IN=$1
	result=0
	
	# Simple File Exists
	[ -e $1 ] && echo "File: $1 Exists" ||  echo "File: $1 not exist."
	[ -f $1 ] && echo "File: $1 is a regular file." || echo "File: $1 is not regular file."
	[ -s $1 ] && echo "File: $1 is not zero size." || echo "File: $1 is zero size."
	[ -d $1 ] && echo "File: $1 is a directory." || echo "File: $1 is a directory."
	[ -b $1 ] && echo "File: $1 is a block device." || echo "File: $1 is not a block device."
	[ -c $1 ] && echo "File: $1 is a character device." || echo "File: $1 is a character device."
	[ -p $1 ] && echo "File: $1 is a pipe." || echo "File: $1 is not a pipe."
	[ -h $1 ] && echo "File: $1 is a symbolic link." || echo "File: $1 is not a symbolic link."
	# Reversed condition
	[ ! -S $1 ] && echo "File: $1 is a socket." || echo "File: $1 is not a socket."
	[ -t $1 ] && echo "File: $1 associated with terminal dev." || echo "File: $1 is not associated with a terminal dev."
	
	
	return $result
}


function FileChecksPerm(){

	
	# readperm="-"
	# writeperm="-"
	# execperm="-"
	# base=$readperm$writeperm$execperm
	FILE_IN=$1
	[ -r $1 ] && echo "File: $1 has read permissions." || echo "File: $1 does not have read permissions."; result=1 
	[ -w $1 ] && echo "File: $1 has write permissions." || echo "File: $1 does not have write permissions."; result=1
	[ -x $1 ] && echo "File: $1 has execute permissions." || echo "File: $1 does not have execute permissions."; result=1
	
	return $result
}





FileChecksExp $1
echo $?
FileChecksPerm $1
echo $?
1. count_input_dir()       : Count files in given directory
2. get_dir_size()          : Get size of input directory
3. bash_array_file()       : Store file into array
4. create_temp_file()      : Creates temporary filename with unique name
5. mkdirandmove()          : Make directory and move to it
6. _basename()             : Portable basename - returns name of file without path
7. _dirname()              : Portable dirname - returns full path to file
8. chk_empty_dir()         : Checks if a directory is empty
9. Find_file()             : Searches for file and prints info
10. Change_dir()           : Change directory safely
11. abs_path_fle()         : Get absolute path to the file
12. linecount.bash         : Get linecount of file
13. wordcount.bash         : Get wordcount of file
14. charcount.bash         : Get character count of file
15. sizecount.bash         : Get size of file
16. md5sum.bash            : Get md5sum of file
17. typeoffile.bash        : Get type of file
18. fileencoding.bash      : Get encoding of file
19. filediskusage.bash     : Get disk usage of file
20. maxlinelength.bash     : Get max line length of file
21. filestat.bash          : Get various information on file
22. FILETESTS.BASH         : Various test constructs for files
23. check_empty_dir.bash   : Check if a directory is empty
24. Prepend_tofile.bash    : Prepend a line at beginning of file
25. ReadLineByLine.bash    : Various ways to read line by line
26. Permissions.bash       : Check if file has given permissions
wordcount=`wc -w $FILE_IN | awk -F " " '{print $1}'`
echo "Wordcount      = $wordcount"

######################################


_dirname() { 
    #return string containing dirname on success, 1 on failure 
    [ -z "${1}" ] && return 1 


    #http://www.linuxselfhelp.com/gnu/autoconf/html_chapter/autoconf_10.html 
    case "${1}" in 
        /*|*/*) _dirname__dir=$(expr "x${1}" : 'x\(.*\)/[^/]*' \| '.' : '.') 
                printf "%s\\n" "${_dirname__dir}" ;; 
             *) printf "%s\\n" ".";; 
    esac 
} 
#==================================================== Example ==== 
#_dirname /path/file #print /path 
#================================================================= 



_dirname /home/tede/n55115/PD/files/test.bsh
#################################
#################################

Filecount - count files in a directory

function filecount(){
numfiles=($DIR_IN/*)
numfiles=${#numfiles[@]}

}	
# Make dir and move to it

mcd () {
    mkdir -p $1
    cd $1
}
81 _basename() { #portable basename 
82     [ -z "${1}" ] && return 1 || _basename__name="${1}" 
83     [ -z "${2}" ] || _basename__suffix="${2}" 
84     case "${_basename__name}" in 
85         /*|*/*) _basename__name="$(expr "${_basename__name}" : '.*/\([^/]*\)')" ;; 
86     esac 
87 
 
88     if [ -n "${_basename__suffix}" ] && [ "${#_basename__name}" -gt "${#2}" ]; then 
89         if [ X"$(printf "%s" "${_basename__name}" | cut -c"$((${#_basename__name} - ${#_basename__suffix} + 1))"-"${#_basename__name}")" \ 
90            = X"$(printf "%s" "${_basename__suffix}")" ]; then 
91             _basename__name="$(printf "%s" "${_basename__name}" | cut -c1-"$((${#_basename__name} - ${#_basename__suffix}))")" 
92         fi 
93     fi 
94 
 
95     printf "%s" "${_basename__name}" 
96 } 
97 #==================================================== Example ==== 
98 #_basename /path/file #print file 
99 #================================================================= 
100 
 
101 _basename2() { #alternative portable basename, faster but with 
102                #possible drawbacks 
103     [ -z "${1}" ] && return 1 || _basename2__name="${1}" 
104     [ -z "${2}" ] || _basename2__suffix="${2}" 
105     case "${_basename2__name}" in 
106         /*|*/*) _basename2__name="${_basename2__name##*/}" 
107     esac 
108 
 
109     if [ -n "${_basename2__suffix}" ] && [ "${#_basename2__name}" -gt "${#2}" ]; then 
110         _basename2__name="${_basename2__name%$_basename2__suffix}" 
111     fi 
112 
 
113     printf "%s" "${_basename2__name}" 
114 } 
115 #==================================================== Example ==== 
116 #_basename2 /path/file #print file 
117 #================================================================= 
118 
266 _dirname() { 
267     #return string containing dirname on success, 1 on failure 
268     [ -z "${1}" ] && return 1 
269 
 
270     #http://www.linuxselfhelp.com/gnu/autoconf/html_chapter/autoconf_10.html 
271     case "${1}" in 
272         /*|*/*) _dirname__dir=$(expr "x${1}" : 'x\(.*\)/[^/]*' \| '.' : '.') 
273                 printf "%s\\n" "${_dirname__dir}" ;; 
274              *) printf "%s\\n" ".";; 
275     esac 
276 } 
277 #==================================================== Example ==== 
278 #_dirname /path/file #print /path 
279 #================================================================= 
280 
672 _mkdir_p() { #portable mkdir -p function 
673     [ -z "${1}" ] && return 1 
674     for _mkdir_p__dir; do 
675         _mkdir_p__IFS="$IFS" 
676         IFS="/" 
677         set -- $_mkdir_p__dir 
678         IFS="$_mkdir_p__IFS" 
679         ( 
680         case "$_mkdir_p__dir" in 
681             /*) cd /; shift ;; 
682         esac 
683         for _mkdir_p__subdir; do 
684             [ -z "${_mkdir_p__subdir}" ] && continue 
685             if [ -d "${_mkdir_p__subdir}" ] || mkdir "${_mkdir_p__subdir}"; then 
686                 if cd "${_mkdir_p__subdir}"; then 
687                     : 
688                 else 
689                     printf "%s\\n" "_mkdir_p: Can't enter ${_mkdir_p__subdir} while creating ${_mkdir_p__dir}" 
690                     exit 1 
691                 fi 
692             else 
693                 exit 1 
694             fi 
695         done 
696         ) 
697     done 
698     unset _mkdir_p__dir 
699 } 
700 #==================================================== Example ==== 
701 #_mkdir_p "foo/bar/daa" #creates the directory hier 
702 #================================================================= 
703 
if [ -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY exists.
fi
Or to check if a directory doesn't exist:

if [ ! -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY doesn't exist.
fi
However, as Jon Ericson points out, subsequent commands may not work as intended if you do not take into account that a symbolic link to a directory will also pass this check. E.g. running this:

ln -s "$ACTUAL_DIR" "$SYMLINK"
if [ -d "$SYMLINK" ]; then 
  rmdir "$SYMLINK" 
fi
Will produce the error message:

rmdir: failed to remove `symlink': Not a directory
So symbolic links may have to be treated differently, if subsequent commands expect directories:

if [ -d "$LINK_OR_DIR" ]; then 
  if [ -L "$LINK_OR_DIR" ]; then
    # It is a symlink!
    # Symbolic link specific commands go here.
    rm "$LINK_OR_DIR"
  else
    # It's a directory!
    # Directory command goes here.
    rmdir "$LINK_OR_DIR"
  fi
fi
function mkdir_timestamp()
{

	# Called as: mkdir_timestamp $main_store $BTCH_DATE_s0
	# Called as: mkdir_timestamp <base directory> <datepattern>


	local base_dir=$1
	local date_in=$2
	local compound=$base_dir$date_in
	echo $compound
	
	# check if dir exists
	
	if [ -d "$base_dir" ]; then
		if [ ! -d "$compound" ]; then
			mkdir $compound
		else
			echo "Directory: $compound already exists. Terminating"
			exit 1
		fi
	else	
		echo "Directory: $base_dir could not be located. Terminating"
		exit 1
	fi


}
filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
#Alternatively, you can focus on the last '/' of the path instead of the '.' 
#which should work even if you have unpredictable file extensions:
filename="${fullfile##*/}"


############
~% FILE="example.tar.gz"
~% echo "${FILE%%.*}"
example
~% echo "${FILE%.*}"
example.tar
~% echo "${FILE#*.}"
tar.gz
~% echo "${FILE##*.}"
gz




################

#!/bin/bash
for fullpath in "$@"
do
    filename="${fullpath##*/}"                      # Strip longest match of */ from start
    dir="${fullpath:0:${#fullpath} - ${#filename}}" # Substring from 0 thru pos of filename
    base="${filename%.[^.]*}"                       # Strip shortest match of . plus at least one non-dot char from end
    ext="${filename:${#base} + 1}"                  # Substring from len of base thru end
    if [[ -z "$base" && -n "$ext" ]]; then          # If we have an extension and no base, it's really the base
        base=".$ext"
        ext=""
    fi

    echo -e "$fullpath:\n\tdir  = \"$dir\"\n\tbase = \"$base\"\n\text  = \"$ext\""
done

#################

pax> FILE=a.b.js
pax> NAME=$(echo "$FILE" | sed 's/\.[^.]*$//')
pax> EXTENSION=$(echo "$FILE" | sed 's/^.*\.//')
pax> echo $NAME
a.b
pax> echo $EXTENSION
js


######################
f='/path/to/complex/file.1.0.1.tar.gz'

# Filename : 'file.1.0.x.tar.gz'
    echo "$f" | awk -F'/' '{print $NF}'

# Extension (last): 'gz'
    echo "$f" | awk -F'[.]' '{print $NF}'

# Extension (all) : '1.0.1.tar.gz'
    echo "$f" | awk '{sub(/[^.]*[.]/, "", $0)} 1'

# Extension (last-2): 'tar.gz'
    echo "$f" | awk -F'[.]' '{print $(NF-1)"."$NF}'

# Basename : 'file'
    echo "$f" | awk '{gsub(/.*[/]|[.].*/, "", $0)} 1'

# Basename-extended : 'file.1.0.1.tar'
    echo "$f" | awk '{gsub(/.*[/]|[.]{1}[^.]+$/, "", $0)} 1'

# Path : '/path/to/complex/'
    echo "$f" | awk '{match($0, /.*[/]/, a); print a[0]}'
    # or 
    echo "$f" | grep -Eo '.*[/]'

# Folder (containing the file) : 'complex'
    echo "$f" | awk -F'/' '{$1=""; print $(NF-1)}'

# Version : '1.0.1'
    # Defined as 'number.number' or 'number.number.number'
    echo "$f" | grep -Eo '[0-9]+[.]+[0-9]+[.]?[0-9]?'

    # Version - major : '1'
    echo "$f" | grep -Eo '[0-9]+[.]+[0-9]+[.]?[0-9]?' | cut -d. -f1

    # Version - minor : '0'
    echo "$f" | grep -Eo '[0-9]+[.]+[0-9]+[.]?[0-9]?' | cut -d. -f2

    # Version - patch : '1'
    echo "$f" | grep -Eo '[0-9]+[.]+[0-9]+[.]?[0-9]?' | cut -d. -f3

# All Components : "path to complex file 1 0 1 tar gz"
    echo "$f" | awk -F'[/.]' '{$1=""; print $0}'

# Is absolute : True (exit-code : 0)
    # Return true if it is an absolute path (starting with '/' or '~/'
    echo "$f" | grep -q '^[/]\|^~/'
    
################################################




cut -c 26 JJ_ALL_X.txt.xstY.sort.ot2 | sort -u > TEST1.TXT
function chk_dir()
{
	local dir_check=$1
	if [ ! -d "$dir_check" ]; then
		echo "Directory $dir_check could not be located. Terminating..."
		exit 25
	else
		echo "Directory $dir_check located."
		return 0
	fi

}


function chk_file()
{
	local file_check=$1
	if [ -f "$file_check" ]; then
		echo "File: $file_check successfully located."
		return 0
	else
		echo "File: $file_check could not be located."
		echo "Critical error. Terminating"
		exit 25
	fi




}