Readerlook
Readerlook

Reputation: 63

how to short file path in linux shell script output

i am taking output of below shell script as an email. in which file name time size are there.

pint file name i am getting whole directory which is too long.

some how is it possible to keep only last one or two directory name and file name. ?

#!/bin/bash

monitor_dir=/path/to/dir
[email protected]

files=$(find "$monitor_dir" -maxdepth 1 | sort)
IFS=$'\n'

while true
do
  sleep 5s

  newfiles=$(find "$monitor_dir" -maxdepth 1 | sort)
  added=$(comm -13 <(echo "$files") <(echo "$newfiles"))

  [ "$added" != "" ] &&
    find $added -maxdepth 1 -printf '%Tc\t%s\t%p\n' |
    mail -s "incoming" "$email"

  files="$newfiles"
done 

Upvotes: 0

Views: 647

Answers (2)

Zombo
Zombo

Reputation: 1

Perhaps this

find $added -maxdepth 1 -printf '%Tc\t%s\t%p\n' |
  awk '{printf "%s/%s\n", $(NF-1), $NF}' FS=/

Upvotes: 0

Michael
Michael

Reputation: 2726

This prints the last two directories in the path plus the file name:

echo "/the/long/path/to/your/file" | awk -F/ '{printf "/";for (i=NF-2;i<=NF;i++){printf $i"/"}}'

To control how many you get modify the loop variable initializer (i.e. NF-2).

Upvotes: 1

Related Questions