For example, what combination of shell commands can I use to output the number of files in a directory?
Hint: it's a trick question, you can't.
For example, what combination of shell commands can I use to output the number of files in a directory?
Hint: it's a trick question, you can't.
find . -type f -depth 1 -print0 | tr -d -c '\000' | wc -c
GNU find has a -printf which would probably avoid the need to use tr. Personally, two hardlinks to the same underlying storage counts as two files to me, but I understand why you might disagree. I'd bet there's something out there that could give you only the count of unique storage areas, but I don't know how that interacts with deduplicating filesystems.I had to modify it a bit to get it to work cross-platform, but the approach does appear to be sound:
find . -maxdepth 1 -type f -print0 | tr -d -c '\000' | wc -c
I had originally written "Hint: it probably isn't what you think it is, if it's even possible", but after testing for a bit, I updated my comment in what was apparently a fit of hubris to say you couldn't.`find . -type f | wc -l`
touch foo; ln foo bar
Is this one file or two "links" to the same file? ;-0
I think of it like an old-timey 'card catalog'. You have a bunch of tiny drawers filled with cards. Some of the cards are big and blank spacers with a prominent tab sticking above the normal top edges (Directory). Sometimes you have a card that points to another card elsewhere in the catalog (link). Sometimes you find the details of a specific book on a specific shelf (block data).
Point is, they are all cards. The comment essentially asked for a command to say 'how many cards between these two spacers'. It's a "trick" question as old as usenet to spring the distinction between link inodes and list of blocks inodes and say "Ah-HAH!! gotcha", but in reality it's a silly game of jumping levels, misdirecting semantics and prey upon the distribution of understanding in a forum for personal glory.
The inode is the item, it is the card that is being counted, no matter what is printed on it. imho.
touch first$'\n'file
touch .hidden$'\n'file
mkdir folder$'\n'.
touch folder$'\n'./another$'\n'file
There are two files in the folder, or three if you want to count files in subdirectories too.Your solution incorrectly outputs 7.
Whereas large scale enterprise commercial solutions make money by selling you on complete solutions that force you to relearn everything their way from the ground up. Just look at how AWS has hijacked so many concepts from the modern web and trapped people into building “cloud-agnostic” wrappers to try and wrangle the mess. Still we’re mapping onto their redesigns of the same old stuff.
At some level it’s unavoidable, hardware vendors need to agree on instructions after all, but when looking at the situation from a high level, it’s best to keep our ideals in mind and steer the ship so-as to find ourselves in paradise not lost in a sea of pirates.
One ideal: Modularity. Promotes healthy competition. Another ideal: Simplicity is more easily achieved when modular components may be used (and tested) in isolation.
Oh.
Then it must be:
command ls -F directory | grep -v '[/@]$' | nl | tail -n 1 | cut -f 1
(-: touch first$'\n'file
touch .hidden$'\n'file
mkdir folder$'\n'.
touch folder$'\n'./another$'\n'file
mkdir normal
touch normal/whatever
This folder has two files in it, or four if you count files in subdirectories.Your solution incorrectly outputs 3.