I am trying to assign a certain portion of text to a .txt file in Linux - linux

$ echo -e "hello world! \hi World. \hello world" > text1.txt
will this work ?

It will work.
But you can try it out on terminal and see what magic happens.
Once you run that. Do cat text1.txt

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

how to insert ctrl+d into my linux script?

I want to make the following commands:
cat > template.txt
[enter in the terminal]
text
[Ctrl+d in the terminal]
in a script.
Is there a way to tell the script to do enter\Ctrl d?
Is there a way to create a file and write to it in script?
I didn't find anything that worked for me.
Thanks.
A Here Document is kind of like a script version of what you're talking about, I think, although it is not entirely clear to me from your description.
#!/bin/bash
cat > template.txt <<- EOF
Here
is some
text.
EOF
Ctrl-D itself is the EOF character, ASCII 4.
When you want an interactieve user enter lines and add them to your file until the user enters an ^D you can use the next script:
echo "Please give input"
while read -r line; do
echo "Enter next line or ^D"
echo "${line}" >> template.txt
done
echo "After loop"
You do not have to check for ^D, that will be recognized by read without doing something extra.So you do not need to use CTRL-V CTRL-D in vi.
No, there isn't. How should the script know when your user is finished entering text?

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

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

Linux bash Shell scripting : Creating command using string Append how to do that?

Hi I want to achieve #cat sample.txt > abc.txt
But "> abc.txt" am getting as argument.
How to cascade these two strings and execute the combined one.
I tried the below steps
/home/root# export str=" > abc.txt;"
/home/root# echo $str
> abc.txt;
/home/root# echo "cat sample.txt $str"
cat sample.txt > abc.txt;
/home/root# `echo "cat sample.txt $str"`
Hello world
cat: can't open '>': No such file or directory
cat: can't open 'abc.txt;': No such file or directory
like this?
#!/bin/bash
args="> foo"
command="date"
eval "$command $args"
cat foo
use of eval is not really a recommended method but sometimes it comes in handy for doing things in a quick and dirty way. I will probably get downrated for suggesting this.
Be aware of its side effects.
If you need to do this from command line write :
cat sample.txt | tee $str

create a file called hello.txt that contains the word "hello world"?

question is as bellow:-
create a file called hello.txt that contains the words "hello world",. can you use "cp" using "terminal" as the source file to achieve the same effect?
i already create a text file and name as hello.txt. then i type a command in linux terminal asbellow:-
cp hello.txt /home/tobenrry
but it is error and cannot get the output.
i try many command already for creating this. but it is also not work. may i knows what the the command for the question?
On Linux the current terminal is identified by the device /dev/tty, so you can do:
cp /dev/tty hello.txt
That's what the question is asking for.
copy from stdin to file:
cp /dev/stdin $HOME/test.txt
cat > $HOME/test.txt
cat /dev/tty > test.txt
say terminal = /dev/tty
type the following commands:
cat >terminal
"enter the txt here say "hello world" "
ctrl+D
cp terminal destinationfilename

Resources