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

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

Related

I am trying to assign a certain portion of text to a .txt file in 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

Automate a Vim script to write text to a file

I am writing a script that runs on the command line and I want to be able to automatically append some static text to a file using Vim.
This is a simplified version of what I have:
insert_text() {
vim -s ./text.txt new.txt;
}
Then it would run on the command line by typing insert_text. Inside text.txt I have tried things such as:
iSome Text:wq
This puts me in insert mode and writes the text but I don't know how to leave insert mode in this way. The :wq never works and is instead written to the new.txt.
If you just want to append text to a file you don't need any special tools. For instance you can just use:
echo "Some text" >> myfile.txt
or if you have a large block of text
>>myfile.txt cat <<EOF
some
long
block of text
EOF
If you want it at the start of a file you can use a temporary file to do this. For instance:
echo "Some Text" | cat - myfile.txt > /tmp/file && mv /tmp/file myfile.txt
This will append someText at the beginning of line 1:
vim -c "1 s/^/someText" -c "wq" test.txt
When you run a command from the terminal and you want Vim to exit after that, instead of :wq do +wq. For example:
vim +'SomeCommand' +qa

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

Redirect argument from a file to a Linux command

I searched the Internet, but maybe I used the wrong keyword, but I couldn't find the syntax to my very simple problem below:
How do I redirect a file as command line arguments to the Linux command "touch"? I want to create a file with "touch abc.txt", but the filename should come from the filename "file.txt" which contains "abc.txt", not manually typed-in.
[root#machine ~]# touch < file.txt
touch: missing file operand
Try `touch --help' for more information.
[root#machine ~]# cat file.txt
abc.txt
Try
$ touch $(< file.txt)
to expand the content of file.txt and give it as argument to touch
Alternatively, if you have multiple filenames stored in a file, you could use xargs, e.g.,
xargs touch <file.txt
(It would work for just one, but is more flexible than a simple "echo").

linux bash redirection with stdin and stdout the same file [duplicate]

This question already has answers here:
How can I use a file in a command and redirect output to the same file without truncating it?
(14 answers)
Closed 8 years ago.
i have a problem about bash, command is like:
echo helloworld > hello.txt
cat <hello.txt >hello.txt
at first, there are stuff in hello.txt. what i expect is that it should seem nothing happens to hello.txt after executing the command, but there is nothing in hello.txt then.
is it a mechanism of bash, or is there something i did not understand about linux file descripor?
maybe bash establish only one fd for a certain file? am i right?
can you help me?
/br
ruan
Use a temporary file
cat hello.txt > /var/tmp/tmpFile
mv -f /var/tmp/tmpFile hello.txt
cat hello.txt

Resources