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 ?

4
  • 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). Commented Mar 29, 2021 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. Commented Mar 29, 2021 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 minimal reproducible example that lets others see your problem? Commented Mar 29, 2021 at 17:15
  • Could use : for i in *;do [[ -f $i ]] && echo ${i##*.};done|sort -u Commented Mar 29, 2021 at 17:27

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

this looks good but we could enhance it by getting unique values
Sorry, totally forgot about the sort, I've added it!
do you know about finding creation date of a file in linux ? is it possible ?
Of course you can get the creation date. However, that's out-of-scope for your current question. Take a look here
0

Using this i solved it

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.