Live data from Hacker News

Useful use of cat(1)

in-ulm.de

11–20 of 46 posts

Re: Useful use of cat(1)

#11
post #4

Earlier quoted context omitted.

Or, at least when running Bash (possibly sh, didn't try), just >foo also works to create file "foo".

That does not work, but :>foo (the happy file creation operator) does.

What do you mean it doesn't work? ">file" will work on all POSIX shells, it even works on Solaris /bin/sh!

Also ":>" is not an operator, it's ":" (a built-in command that does nothing and returns true) and a redirection.

Re: Useful use of cat(1)

#13
post #3

cat > file vs. touch file cat file | cmd vs. cmd So even some of the thing listed are still not the best use case for cat

| cat > file vs. touch file With 'cat > file' you can actually type in the file's contents (then press ^d to finish). 'touch file' merely creates an empty file. (Not that I think 'cat > file' is much faster than 'vi file')

Ok, I should read more carefully. I thought the article uses cat > file followed by ^D to create empty files.

Of course when having some text cat > file is just fine.

Re: Useful use of cat(1)

#15
post #10

ps has a nasty habit of truncating its output. Piping it into cat is a way around this. E.G. $ echo $COLUMNS 118 $ ps -fp 14706 UID PID PPID C STIME TTY TIME CMD root 14706 14705 0 Jan29 pts/3 00:00:00 sudo -p [local sudo] Password: python /usr/lib/sshuttle/main.py pytho $ ps -fp 14706 | cat UID PID PPID C STIME TTY TIME CMD root 14706 14705 0 Jan29 pts/3 00:00:00 sudo -p [local sudo] Password: python /usr/lib/sshutt…

Using the -ww flag will produce unlimited width output

Re: Useful use of cat(1)

#16
Often I find myself dealing with tab-separated files; and cat -n is very handy there.

To see which columns a particular value occurs in:

    awk -F'\t' 

Re: Useful use of cat(1)

#17
post #2

You may also create new empty files by saying `touch filename`. This saves three keystrokes (which includes an annoying `>` symbol, requiring the shift key) as compared to the aforementioned `cat` method!

You can also use 'echo "hello" > file'

Re: Useful use of cat(1)

#18

Don't forget "tac" - the reverse of "cat".

No, that just concatenates a file reading bytes from the end, backwards.

The real opposite of cat would be a putative 'trunc'. So if you typed

    $ cat somefile
It would print the file to your terminal. And then if you said

     $ trunc somefile
The printed lines would be removed from your terminal. :)

Re: Useful use of cat(1)

#19
post #6
post #2

You may also create new empty files by saying `touch filename`. This saves three keystrokes (which includes an annoying `>` symbol, requiring the shift key) as compared to the aforementioned `cat` method!

what do you mean? "cat > file" allows you to create simple text file not just empty files. As long as you don't need advanced editing, it is the quickest way to do it (well I think).

You are correct; my comment solves a different problem than the one in the article!

Re: Useful use of cat(1)

#20
post #3

cat > file vs. touch file cat file | cmd vs. cmd So even some of the thing listed are still not the best use case for cat

>* cat file | cmd vs. cmd I tend to prefer the cat version as the flow of the command follows the left-to-right direction in which I generally read, and it makes things easier for less experienced users to read and understand for the same reason.

It does make things less efficient of course, as there is at least one extra memory-copy of everything and extra process context switching, but in most cases your drives or network are the bottleneck not CPU speed or memory bandwidth. Also, when working on the command line you can drop in useful tools like pv in place of cat.

Post reply on HN