0

So far I have tried

extension=$(find /home/path-to-dir -type f -name '*.*' | sed 's/^.*\.//' | sort -u)

It is giving me extension right after . but it is failing for text.data.json ( I only need json)

similar example stack/~.hello.py ( I only need py)


also I m trying to find the creation date of a file ? is it possible ?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
chris evans
  • 229
  • 1
  • 2
  • 8
  • Only one question to a question, please -- creation date should be asked separately (and the answer depends on which filesystem you're using, but _usually_ the answer is no, it's not possible -- typical UNIX filesystems only store last-modified time). – Charles Duffy Mar 29 '21 at 17:12
  • Beyond that... in general, using a string variable to store a list of things isn't a great idea -- lists should be stored in arrays. That way you can ask how many things there are in the array, refer to them by number, etc. – Charles Duffy Mar 29 '21 at 17:14
  • 1
    I'm surprised, though, that your current regex _isn't_ stripping all the way to the end. They're greedy by default, and `sed` doesn't provide a way to turn that off -- if you run `echo 'foo.bar.baz' | sed 's/^.*\.//'`, you get only `baz`, not `bar.baz`. Can you provide a [mre] that lets others see your problem? – Charles Duffy Mar 29 '21 at 17:15
  • Could use : `for i in *;do [[ -f $i ]] && echo ${i##*.};done|sort -u` – Andre Gelinas Mar 29 '21 at 17:27

2 Answers2

0

As an alternative to the sed command, we can use parameter expansion to get the string after the last .;

find /home/path-to-dir -type f -name '*.*' -exec bash -c 'echo ${0##*.}' {} \;

Small example:

--> ls
ahahha~bla.py  bla.bla.json  ok.py  test.json  text.data.json
-->
-->
--> find . -type f -name '*.*' -exec bash -c 'echo ${0##*.}' {} \; | sort -u
json
py

Extract filename and extension in Bash

0stone0
  • 34,288
  • 4
  • 39
  • 64
0

Using this i solved it

find . -type f -name "[^.]*.*" -exec bash -c 'printf "%s\000" "${@##*.}"' argv0 '{}' + | sort -uz | tr '\0' '\n'
chris evans
  • 229
  • 1
  • 2
  • 8