How can display txt file in terminal through a linux command? - linux

I forgotten how do this...
the command is something who this:
~$"command" gladiator.txt
When a Roman general is betrayed and his family murdered by an emperor's corrupt son, he comes to Rome as a gladiator to seek revenge.
~$

For small file
cat fileName.txt
or
cat path/fileName.txt
directly shows a text file in the terminal.
For large file
less fileName.txt
or
less path/fileName.txt

You can use
$ cat file.txt
have a nice day

You can use:
cat FILENAME
Hope it helps.

enter these in your terminal
cat textfile.txt
cat command shows the contents of a file

Related

Linux save string to file without ECHO command

I want to save a command to a file (for example I want to save the string "cat /etc/passwd" to a file) but I can't use the echo command.
How can I create and save string to a file directly without using echo command?
You can redirect cat to a file, type the text, and press Control-D when you're done, like this:
cat > file.txt
some text
some more text
^D
By ^D I mean to press Control-D at the end. The line must be empty.
It will not be part of the file, it is just to terminate the input.
Are you avoiding ECHO for security purposes (e.g. you're using a shared terminal and you don't want to leave trace in the shell history of what you've written inside your files) or you're just curious for an alternative method?
Simple alternative to echo:
As someone said, redirecting cat is probably the simpler way to go.
I'd suggest you to manually type your end-of-file, like this:
cat <<EOF > outputfile
> type here
> your
> text
> and finish it with
> EOF
Here's the string you're asking for, as an example:
cat <<EOF > myscript.sh
cat /etc/passwd
EOF
You probably don't want everyone to know you've peeked into that file, but if that's your purpose please notice that wrapping it inside an executable file won't make it more private, as that lines will be logged anyway...
Security - Avoiding history logs etc..
In modern shell, just try adding a space at the beginning of every command and use freely whatever you want.
BTW, my best hint is to avoid using that terminal at all, if you can. If you got two shells (another machine or even just another secure user in the same machine), I'd recommend you using netcat. See here: http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner
{ { command ls $(dirname $(which cat)) |
grep ^ca't$'; ls /etc/passwd; } |
tr \\n ' '; printf '\n'; } > output-file
But it's probably a lot simpler to just do : printf 'cat /etc/passwd\n'
To be clear, this is a tongue-in-cheek solution. The initial command is an extraordinarily convoluted way to get what you want, and this is intended to be a humorous answer. Perhaps instructive to understand.
I am not sure I understood you correctly but
cat /etc/passwd > target.file
use the > operator to write it to file without echoing
If you need to use it, inside a program :
cat <<EOF >file.txt
some text
some more text
EOF
I would imagine that you are probably trying to print the content of a string to a file, hence you mentioned echo.
You are avoiding this:
echo "cat /etc/passwd" > target.file
You can use a here string combined with cat.
cat > target.file <<< "cat /etc/passwd"
Now the file target.file will contain a string cat /etc/passwd.
$ cat target.file
cat /etc/passwd
$
To create string:
var1=your command
to save a file or variable in a file without echo use:
cat $FILE/VAR1 > /new/file/path

Missing newline character?

I've copied a file from HDFS into my local file system (all on RH linux). However, after the copy, if I cat the file, I see the following:
[me#ac12 ~]$ cat file_copy
0|name|string
1|phone|string
2|age|string[me#ac12 ~]$
What I expected was this:
[me#ac12 ~]$ cat file_copy
0|name|string
1|phone|string
2|age|string
[me#ac12 ~]$
You can see that a newline seems to be missing in the first cat, and the shell prompt is on the same line as the last line. Why would this be and how can I diagnose the issue?
EDIT: I can't edit the output file (well, maybe I could but I really don't want to - I'd rather fix the problem at the source). I want to know why there is no newline character..
You can use this sed to add a newline after last line:
sed -i.bak $'$s/$/\\n/' file_copy
EDIT:
Or else use (thanks to #JonathanLeffler):
echo '' >> file_copy

Command to open a file which contains the given data

I had this question in interview.
He put a situation in front of me that there are 12 files in your Linux operating system.
Give me a command which will open a file containing data "Hello"..
I told him I just know grep command which will give you the names of files having "Hello" data.
Please tell me if there is any command to open a file in this way..
Assuming it will be only one file containing the word hello:
less $(grep -H "hello" *.txt | sed s/:.*//)
Here it is first capturing the file name using grep with -H parameter. Then using sed removing everything except the filename. And finally its using less to open the file.
Maybe this could help:
$ echo "foo" > file1.txt
$ echo "bar" > file2.txt
$ grep -l foo * | xargs cat
foo
You have 2 files, and you are looking for the one with the string "foo" in it. Change cat with your command of choice to open files. Might try vi, emacs, nano, pico... (no, another flame war!)
You may want to try a different approach if there are several files that contains the string you are looking for... Just thought of only one file containing the string.

Append text to file from command line without using io redirection

How can we append text in a file via a one-line command without using io redirection?
If you don't mind using sed then,
$ cat test
this is line 1
$ sed -i '$ a\this is line 2 without redirection' test
$ cat test
this is line 1
this is line 2 without redirection
As the documentation may be a bit long to go through, some explanations :
-i means an inplace transformation, so all changes will occur in the file you specify
$ is used to specify the last line
a means append a line after
\ is simply used as a delimiter
If you just want to tack something on by hand, then the sed answer will work for you. If instead the text is in file(s) (say file1.txt and file2.txt):
Using Perl:
perl -e 'open(OUT, ">>", "outfile.txt"); print OUT while (<>);' file*.txt
N.B. while the >> may look like an indication of redirection, it is just the file open mode, in this case "append".
You can use the --append feature of tee:
cat file01.txt | tee --append bothFiles.txt
cat file02.txt | tee --append bothFiles.txt
Or shorter,
cat file01.txt file02.txt | tee --append bothFiles.txt
I assume the request for no redirection (>>) comes from the need to use this in xargs or similar. So if that doesn't count, you can mute the output with >/dev/null.
You can use Vim in Ex mode:
ex -sc 'a|BRAVO' -cx file
a append text
x save and close
On Linux/GNU systems, the simplest and cleanest solution is:
dd of=oldfile oflag=append conv=notrunc
Simple and clean, because no quoting or backslashitis is required.
Unfortunately, this also doesn't work on BSD (and so, on Darwin), because their dd has no oflag . Argh! Can anyone suggest how to do it with the BSD dd ?

What's a simple method to dump pipe input to a file? (Linux)

I'm looking for a little shell script that will take anything piped into it, and dump it to a file.. for email debugging purposes. Any ideas?
The unix command tee does this.
man tee
cat > FILENAME
You're not alone in needing something similar... in fact, someone wanted that functionality decades ago and developed tee :-)
Of course, you can redirect stdout directly to a file in any shell using the > character:
echo "hello, world!" > the-file.txt
The standard unix tool tee can do this. It copies input to output, while also logging it to a file.
Use Procmail. Procmail is your friend. Procmail is made for this sort of thing.
Use <<command>> | tee <<file>> for piping a command <<command>> into a file <<file>>.
This will also show the output.
If you want to analyze it in the script:
while /bin/true; do
read LINE
echo $LINE > $OUTPUT
done
But you can simply use cat. If cat gets something on the stdin, it will echo it to the stdout, so you'll have to pipe it to cat >$OUTPUT. These will do the same. The second works for binary data also.
If you want a shell script, try this:
#!/bin/sh
exec cat >/path/to/file
If exim or sendmail is what's writing into the pipe, then procmail is a good answer because it'll give you file locking/serialization and you can put it all in the same file.
If you just want to write into a file, then
- tee > /tmp/log.$$
or
- cat > /tmp/log.$$
might be good enough.
Huh? I guess, I don't get the question?
Can't you just end your pipe into a >> ~file
For example
echo "Foobar" >> /home/mo/dumpfile
will append Foobar to the dumpfile (and create dumpfile if necessary). No need for a shell script... Is that what you were looking for?
if you don't care about outputting the result
cat - > filename
or
cat > filename

Resources