Create and write file in single Linux/macOS command


If you want to create a fine and write to it in a single command you can do it in various ways, one of the way is to use the echo command and use > redirect the text to a file, but doing so the file will be created and echoed text will be written to the file.


Example:
bash $ echo "This will be written to the file" > myFile.txt

bash $ cat myFile.txt
This will be written to the file


Create and wirte a file in one command


You can in this way also redirect the output of a command to a file.

Example:
bash-3.2$ cd /
bash-3.2$ ls > /Users/code2care/Desktop/myFile.txt

bash-3.2$ cat /Users/code2care/Desktop/myFile.txt
Applications
Library
System
Users
Volumes
bin
cores
dev
etc
home
opt
private
sbin
tmp
usr
var

Comments:

  • Thanks!
    03 Jul 2022 17:07:05 GMT
  • Further comments disabled!
Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap