What does the UNIX cat command do and how can I access hidden files?

In summary: So, for instance, if you're trying to type "ls", and there's a space in the filename, it won't do anything. Instead, you'll need to type "ls -a" (without the quotes) to see all the files. In summary, the "cat" command doubts!
  • #1
roTTer
18
1
UNIX “cat” command doubts!

I’n new to UNIX so please bear with me.

I wanted to know what does the cat file1.txt << file2.txt command do?

it doesn’t give an error, just goes accepts the keyboard as stdinput, and on ctrl+d, contents of file1 nor file2 changes.

Also one more thing in the terminal window itself, the keyboard as the stdinput, when pressing Esc twice it prints all the hidden files(starts with a period), or atleast that's what I think. What does it do.
 
Technology news on Phys.org
  • #2


Checking the man page for csh or tcsh I find:
Input/Output:
<< word Read the shell input up to a line which is identical to word. blah blah blah. The resultant text is placed in an anonymous temporary file which is given to the command as standard input.

So I expect what you should be seeing when you type in your command is a ? showing it is waiting for input. Any lines you type in except file2.txt or ctrl-d will just give you another ? showing it is still reading input. When you finally type file2.txt or ctrl-d on a line then cat will take that as input, cat will I believe ignore this meaningless input, and it will then display file1.txt as you directed and finally terminate. Testing that here appears to work as documented.

Study the documentation on your shell. Buy a book or two on it. You will be rewarded for that.
 
  • #3


If memory serves, the "<<" is called a "here" document. It allows one to put what would be input to a command into a script, but I don't think it has any function directly on the command line. I always forget how to use here documents and have to experiment. As Bill said, use the "man" Luke...from my cygwin "bash" man page:

Here Documents
This type of redirection instructs the shell to read input
from the current source until a line containing only word
(with no trailing blanks) is seen. All of the lines read
up to that point are then used as the standard input for a
command.

The format of here-documents is:

<<[-]word
here-document
delimiter

You might have been thinking of using a single "<" to redirect stdin from a file?
 
  • #4


roTTer said:
I wanted to know what does the cat file1.txt << file2.txt command do?
It collects input from the keyboard (or shell if you have redirected stdin) until the you type file2.txt on a single line. All of your typed in input goes to the bit bucket because cat when provided with one or more file names does not read from stdin. cat will type the contents of file1.txt to the screen.


That is probably not what you wanted, so what are you trying to do here?
 
  • #5


roTTer said:
Also one more thing in the terminal window itself, the keyboard as the stdinput, when pressing Esc twice it prints all the hidden files(starts with a period), or atleast that's what I think. What does it do.

That's a shell thing. Depending on the terminal program you use, and the shell that you're running, it may be the "tab" key or the "esc" key, or both. It's a feature of auto-complete.

First off, what it does is handily tries to auto-complete the thing you're typing. So if you wanted to type some crazy command like:

cd /home/rotter/someenormouslyhugefilenamethatIdrathernottype/

You can instead type:

cd /home/rotter/some(tab)

And it ought to figure out that there's only ONE possible solution to what you're typing-- so it will auto-complete it for you. Or, it will auto-complete as much as it can figure out (which is often most of it).

Now, sometimes, hitting (tab) or (esc) doesn't do the job, because it just-so-happens that there are MULTIPLE things that match what you're currently typing. For instance, if there were also a file called:

/home/rotter/someotherfile

Then typing (tab) or (esc) wouldn't help you to auto-complete, because there are two matches. So! If you hit the (tab) or (esc) a second time, it will tell you all the possible completions that there are. So it might go something like this:

> cd /home/rotter/some(tab)(tab)
someenormouslyhugefilenamethatIdrathernottype
someotherfile
> cd /home/rotter/some

Then, if you hit "e", and then (tab) again, it would auto-complete the whole thing.

There's also an extra caveat, again, depending on the shell you use. If the thing you're typing is at the BEGINNING of the command, it will recognize this as a command. So it will only auto-complete filenames that are *executable* (like directories). This means it will look in your current $PATH for any possible matches. If you're typing something in the middle, or near the end of your command, it will auto-complete any file types, regardless of the permissions, but it will NOT search the entirety of the $PATH-- just the current directory, or directory you started typing.

DaveE
 
  • #6


Your command means:
- Type contents of file1.txt
- Open the "here document". Accept user input until user input is "file2.txt" or CTRL-D and type the contents to the screen

The correct "usage for this" may be like cat << END > file2.txt
You will be prompted for input and until you type END on a single line or CTRL-D it will take your input. Then contents of file 1 will be inserted into file2.txt.

You maybe wanted to cat file1.txt >> file2.txt
- Type contents of file1.txt
- But redirect stdout to (>>) append into file2.txt

You maybe wanted to cat file1.txt > file2.txt
- Type contents of file1.txt
- But redirect stdout to (>) replace into file2.txt

Often you may see redirection like this (a useful example):
foo > /dev/null 2>&1
Run program foo and redirect standard error to standard output and redrect resulting standard output to /dev/null (black hole).
 
  • #7


Thank you for your replies, it has been really helpful.
 

1. What is the purpose of the cat command in UNIX?

The cat command in UNIX is used to display the contents of a file on the terminal. It can also be used to create, combine, and copy files.

2. How do I use the cat command to create a new file?

To create a new file using the cat command in UNIX, you can use the redirection operator (>) followed by the name of the file you want to create. For example, cat > newfile.txt will create a new file called "newfile.txt" and allow you to enter text into it.

3. Can the cat command be used to display multiple files at once?

Yes, the cat command can be used to display the contents of multiple files at once. You can simply list the names of the files after the cat command, separated by a space. For example, cat file1.txt file2.txt file3.txt will display the contents of all three files.

4. Is it possible to use the cat command to append text to an existing file?

Yes, the cat command can be used to append text to an existing file. You can use the append operator (>>) followed by the name of the file to which you want to append the text. For example, cat text.txt >> existingfile.txt will append the contents of "text.txt" to the end of "existingfile.txt".

5. Can I use the cat command to display only a specific number of lines from a file?

Yes, the cat command has an option (-n) that allows you to specify the number of lines you want to display from a file. For example, cat -n file.txt will display the contents of "file.txt" with line numbers. You can also specify a range of lines to display, such as cat -n file.txt | 5-10 to display lines 5 to 10 of the file.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
3K
Replies
15
Views
4K
  • Special and General Relativity
Replies
13
Views
2K
Back
Top