The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 12,436β€…    Votes:  14β€…
Tags: command-line   redirect   cat  
Link: πŸ” See Original Answer on Ask Ubuntu πŸ”—

Title: What is the difference between "cat < filename" and "cat filename"?
ID: /2019/11/22/What-is-the-difference-between-_cat-_-filename_-and-_cat-filename__
Created: November 22, 2019    Edited:  December 12, 2019
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false

One Big Difference

One big difference is with the *, ?, or [ globbing characters (wildcards) or anything else the shell may expand into multiple filenames. Anything the shell expands into two or more items, rather than treating as a single filename, cannot be opened for redirection.

Without redirection (ie no <), the shell passes multiple filenames to cat, which outputs the files’ contents one after another. For example this works:

$ ls hello?.py

$ cat hello?.py

# Output for two files '' and '' appear on your screen

But with redirection (<) an error message occurs:

$ ls < hello?.py
bash: hello?.py: ambiguous redirect

$ cat < hello?.py
bash: hello?.py: ambiguous redirect

One Tiny Difference

I thought with redirection it would be slower but there is no perceivable time difference:

$ time for f in * ; do cat "$f" > /dev/null ; done

real	0m3.399s
user	0m0.130s
sys 	0m1.940s

$ time for f in * ; do cat < "$f" > /dev/null ; done

real	0m3.430s
user	0m0.100s
sys 	0m2.043s


⇧ Does rsync requires changing UUID after copying a partition? Is there a markdown "rich text" editor?  β‡©