7 ms·
The useful use of cat
- ttyprintk 2y agoA better example is ‘pv -l’. Seeing cat in a heavily-used pipeline means pv is not available.
- terminalbraid 2y agoMy main use of cat is for clipboard pasting (if I can't leverage pbpaste or something more appropriate). If I want to get something into a file or get into a pipe as stdin `cat > file` or `cat | cmd`, paste, then end with cmd-D to send an EOF and terminate the cat. For files it circumvents "helpful" formatting you would get pasting into an editor.
- codesnik 2y agoif you're on os x, you can also do pbpaste > file and if you're using zsh, just > file is sufficient, without cat
- fifilura 2y ago"A la test driven development. " I would not call that test driven development (where are the testcases?) Rather more like REPL driven programming.
- delichon 2y agoWhether there are formal examples or not, the thing you're doing during REPL driven programming is testing. It's just rapid, iterative and informal.
- bityard 2y agoTDD refers to a specific kind of testing methodology, not just any and all testing. Manual testing is not TDD.
- deleted 2y ago[deleted]
- wwalexander 2y agoYou can still just begin with <somefile instead of cat somefile |. Does it really matter? No, but I don’t think pointing out useful and concise built-ins is necessarily asinine.
- jchw 2y agoYeah, there are still some edge cases where cat is useful, but more often than not I just begin the redirection. I reckon some don't realize you can put it at the beginning, which does sometimes feel more ergonomic in a pipeline.
- JoshTriplett 2y agoIt's not that it feels awkward to write the redirection first. It's that it feels awkward to write `< file cmd`, with no operator between the filename and the command, and the redirection operator not pointing at the command.
- jchw 2y agoTo me, the < and > redirections never point toward or away from the command. Rather, they point into or out of the file. And really that's because they do: like, >file is really 1>file, and <file is really 0<file. I think if your intuition on how redirections work is based on whether it points at the command it's worth rethinking how you look at redirections.
- kortilla 2y agoA pipeline without pipes connecting is just a field of shit
- MathMonkeyMan 2y agoIO redirection doesn't construct a pipe, but piping does. # Open foo.txt and use it as fd 0 in jq <foo.txt jq '[1,2,3]' # Create a pipe(). Use the write end as cat's fd 1 and the read end as jq's fd 0 cat foo.txt | jq '[1,2,3]' Best thing would be a directed graph where each node is a process or a file. A process has at most one edge going into it (stdin) and at most two coming out (stdout and stderr). A file has exactly one edge touching it. Hard to do in text, though. Would look like lisp.
- echoangle 2y ago> Because for me it'd only be a local maxima. Nitpick: this should be „maximum“, maxima is the plural.
- Arcuru 2y agoUsing `cat file.txt | ...` has always felt more natural to me, so I still use it. If I need to build a long command I've been using the excellent `up` tool to do it, e.g. `cat file.txt | up` https://github.com/akavel/up https://github.com/akavel/up
- REDS1736 2y agoWow that looks amazing, thanks for telling me about this!
- agumonkey 2y agoIndeed. A good addition to fzf and bat
- latexr 2y ago> Using `cat file.txt | ...` has always felt more natural to me Use whatever you prefer¹, but if what you care for is the logic of reading left to right, you can do so without `cat` by doing instead `< file.txt | ...` ¹ It’s absurd this needs to be spelled out, but here we are.
- oneeyedpigeon 2y agoThey're saying they find "cat" more natural than "<" which I can understand. Unless you regularly use "<", it's probably easier to remember "cat" and it's more self-documenting.
- latexr 2y ago> They're saying they find "cat" more natural than "<" which I can understand. I don’t think that’s what they’re saying. I think they’re making the point that going left to right with the file first is what makes sense to them, as opposed to `... < file.txt`.
- oneeyedpigeon 2y ago
- sim7c00 2y agocat all the things. i cat | grep and im not ashamed of it! cat | less - why not.? its not going to melt my pc more or less if i cat | more | less...
- moffkalast 2y agoIt's the cat's meow!
- Lockal 2y agoYou may miss some optimizations for cat | tail. And you can't --follow a cat, obviously!
- PLenz 2y agoI honestly use tac more then cat
- yjftsjthsd-h 2y agoI'm not sure I have ever used tac for real. Could you give an example?
- chgs 2y agoReading a log file from most recent entry for example Cat *log|grep error|tac for example Tail -500 log | tac
- sgarland 2y agoWhen you want to tail a file, but you want it in descending order and you’re not sure how much of it you want: tac file | less
- yjftsjthsd-h 2y agoI guess the difference is that I expect things to read top to bottom, so I'd do that as less foo.log G
- sgarland 2y agoFair enough, if that’s how you want it to read. less on its own is certainly quite capable.
- unnah 2y agoI'll just stick with tail -f until they implement tac -f
- shaftoe444 2y agoIn my experience it's best to do cat file | tac
- 2y ago
- tptacek 2y agoDoes anyone take the "don't use cat" stuff seriously?
- tux3 2y agoWell, this is the internet! We had just gotten over tabs vs spaces after barely a thousand years of noticing the other side is objectively wrong. Something new that you're definitely doing wrong to blog and argue and bicker about; it can't be helped!
- jonjonsonjr 2y agoI tend to agree with this sentiment but this year I came across two situations where the use of cat is actually harmful and not just useless: https://github.com/jonjohnsonjr/til/blob/main/post/readat.md#harmful-use-of-cat https://github.com/jonjohnsonjr/til/blob/main/post/readat.md... I still instinctively start my pipelines with cat half the time, but now I have complicated feelings about it.
- rzzzt 2y agoShellCheck does (did): https://www.shellcheck.net/wiki/SC2002 https://www.shellcheck.net/wiki/SC2002
- Ferret7446 2y agoIt's more of a symptom of someone who doesn't understand what's actually going on, i.e. cargo cult scripting. Specifically, it indicates some combination of: 1. isn't aware of processes/pipes 2. isn't aware of cat's "primary" functionality 3. isn't aware of shell input redirection 4. isn't aware that shell input redirection can be put before the command
- cybrox 2y ago90% of the time, I care more about someone like that understanding my script than the tiny performance impact of an extra process or 'misusing' a function.
- deleted 2y ago[deleted]
- cocodill 2y agoand a cat is also good against mice
- kelnos 2y agoPeople who get up in arms about "useless" uses of cat need to chill out. I read my pipelines left to right. Using a '<' redirection to pull the data in after the command means I have to swap my reading direction back and forth, and why bother with that? I have better things to do with my brain energy.
- bloppe 2y agoAs others have mentioned, you can put `<` anywhere in the command, including at the very beginning. But, I agree that getting up in arms about this is really dumb.
- codesnik 2y agoif you prefer to use cat just to read your pipeline from left to right, you can just start your command with file redirection. It doesn't have to be at the end of the command. <file grep something | do something else ... this works in bash too, but if you're using zsh, there're a couple of nice shortcuts <file on it's own works as more file and > file ... ... ^D allows you to put something into the file quickly without firing up the editor. Though > file << end ... end will give you nicer line editing experience. more zsh redirection tricks here: https://zsh.sourceforge.io/Doc/Release/Redirection.html#Multios https://zsh.sourceforge.io/Doc/Release/Redirection.html#Mult...
- globular-toast 2y agoIf I saw <file grep ... It would give me pause. If I saw cat file | grep ... I would understand it instantly, as would any other Unix user. Therefore the latter is better code.
- deleted 2y ago[deleted]
- globular-toast 2y agocat concatenates files together. I often use it for that. Who cares if "files" is just one file? Using cat regardless keeps things uniform. I like it. People who get funny about it are just trying to be clever. Yes I know there are other ways to do it. But you understood what cat did, otherwise you wouldn't have commented on it.
- Svoka 2y agoliterally what cat stands for. conCATenate.
- x3n0ph3n3 2y agocat | sudo tee somefile > /dev/null is still my favorite way to paste text into an ssh session and save it to a protected file.
- pbaam 2y agoThe cat command can be omitted there, as tee reads from standard input by default, even if stdin points to a terminal. I was going to comment an actually useful (and unavoidable in bash) use of cat and ssh, which is to do do nothing with standard input and redirect it to a file: <file ssh 'cat >file' And you could just use scp, but I've found clients without scp and servers with the SFTP subsystem disabled.
- x3n0ph3n3 2y agoI want do paste from my clipboard, not copy another file.
- pbaam 2y agoI know, it's intentionally unrelated. But if you read my first sentence, you can do what you are interested in without using cat. sudo tee somefile > /dev/null And you will be able to paste from your clipboard or write anything you want. Without cat or piping.
- its-summertime 2y agoThe underlying topic for this is that, most shells treat files/redirections as part of the individual commands and not part of the pipelines. Files should be sometimes treated as commands instead, they plug into the fds of a process the same way as commands do. `cat x | head` attaches `cat x`'s stdout to `head`'s stdin, `< x head` just attaches `x`'s fd to stdin. In this way, treating a file like a "light-weight" command (light-weight because no backing process) makes more sense, `open x | head` perhaps Though I will note that, in a way, bash supports this, as it has a modules system, and one of the pre-existing modules in the source tree is a `cat` replacement, through my lack of understanding of C prevents me from working out if its optimal https://git.savannah.gnu.org/cgit/bash.git/tree/examples/loadables/cat.c https://git.savannah.gnu.org/cgit/bash.git/tree/examples/loa...
- rolfan 2y agoFrom the file you linked, the built-in just copies the input files one by one, sequentially, into stdin. All done in chunks of 4096 bytes, a common page size. Is it as optimal as just attaching the file descriptor? No, because you will have to pipe the contents. Is it optimal (or at least optimal enough)? Considering how pipes work, yes.
- smitty1e 2y agoI'm a believer in the ditt-k-pow, DTTCPW, the Dumbest Thing That Could Possibly Work. What, exactly, are the arguments against `cat`?
- cybrox 2y agoThe argument is that it's redundant because 'cat f | head' does the same as 'head f' I don't mind either way but I find starting with a cat and reading LTR to be very easy to understand.
- js2 2y ago> Some people insist that we should never use a cat Rather, people argue against useless use of cat, which is different. Personally, I'd never argue against it on the command-line even though some do. It's once you commit your pipeline to a script where you may as well remove it. There are also suitable uses of cat that I don't think anyone would argue against. Besides the obvious use of outputting one or more files, it's also often used with here docs.
- userbinator 2y agoThe logic of the author's argument seems rather convoluted. To me, cat or not is a simple decision: does the first useful command in the pipeline accept input from a file, or only stdin?
- lifeline82 2y ago[dead]
- nullc 2y agoI usually start with cat -- later the head doesn't just get replace the cat may too, e.g. replacing it with some command whose output was only temporarily stored in a file for development purposes.
- kreyenborgi 2y agohttps://news.ycombinator.com/item?id=39457875 https://news.ycombinator.com/item?id=39457875 another one
- xelxebar 2y agoSpeaking of redirections... Here's another fut bit of lesser-known shell: f(){ cat;} <file f # prints out contents of file I.e. You can bind redirections to function definitions that end up applying at call time.
- Brian_K_White 2y agowhat is a use of that vs f () { cat file; } Why make the shell open and read the file and pipe in to the cat process when cat would open and read the file itself directly? I guess the point is not cat but the redirection itself. ie, if the contents of f() were not simply cat, and maybe there was no handy place to put the filename within. Though, I think I still struggle to think up an example even then.
- xelxebar 2y agoIn this example? It's clearly useless and somewhat tongue-in-cheek. Redirecting stdout has some straightforward uses, e.g. log(){ foo "$1" bar "$2" } >>logfile Redirecting stdin is a little more subtle: contents(){ maybeReadStdinA maybeReadStdinB } <input where the first command to read stdin gets the contents of input and subsequent commands see nothing.
- deleted 2y ago[deleted]
- deleted 2y ago[deleted]