0

I’m looking for the easiest way to get the list of media files ( by extensions like mp4,avi,mkv,...) recursively from a folder filtered to get only the ones with one hardlink.

My ls -lR output currently is composed by * Permissions * Nb of Hardlink * Owner * Group * Size * Date * Name

Like https://unix.stackexchange.com/questions/103114/what-do-the-fields-in-ls-al-output-mean#103118

I’m currently using a mix of ls and grep but it’s not working fine as it provides the full info and not just the filename.

Thanks for your help!

TheLazyFox
  • 103
  • 3
  • 1
    You realize that depending on what platform and exact options your ls was compiled with the number in that column varies right? eg, it may not count symlinks but does count hard links. If you want to find files and not links or directories, `find /path/to/start -type f` will show files and *hard* links to files. – ivanivan Apr 19 '19 at 12:58

1 Answers1

1

Parsing ls is not recommended. In many similar cases find is the right tool. find in Debian should support -links test. man 1 find reads:

-links n
File has n links.

This test is perfect for you. The solution:

find . -type f \( -name "*.mp4" -o -name "*.avi" -o -name "*.mkv" \) -links 1

Add whatever extensions you want. Consider -iname instead of -name to perform case insensitive matching. Or if you want to test by content, not extension, then use this approach (although it will be much slower):

find . -type f -links 1 -exec sh -c 'file --mime-type "$1" | grep -q "video/"' sh {} \; -print
Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
  • Great, I will test that. Do you have any way to exclude some filenames? – TheLazyFox Apr 22 '19 at 15:58
  • @RaspDealer Yes. `find` is quite flexible, with `!` you can negate any test. Please see [*How to ignore certain filenames using 'find'?*](https://superuser.com/q/397307/432690) While building the expression for `find` keep in mind [operator precedence may not be what you think at first](https://superuser.com/a/1314646/432690). Once you get it, you are able to build complex tests. Once you learn [`-exec` is also a test](https://superuser.com/a/1221882/432690) and [how to use it with `sh -c`](https://unix.stackexchange.com/q/156008/108618), you can test for virtually *anything*. – Kamil Maciorowski Apr 22 '19 at 17:47