0

I am working with Ubuntu version 18.04.4 LTS. I tried to create a file using the following command: sudo cat > filename.yaml and received a "Permission denied error" I can create the file using the VIM editor and sudo. Please advise.

rcaaron
  • 1
  • 1

2 Answers2

0

I'm not an expert on cat but I suspect the command as you've entered it above will produce an error by design. Cat is used to print and concatenate files.

Example commands with cat are cat filename.yaml which will print the contents of filename.yaml to standard output (i.e. the terminal); while cat file1.yaml file2.yaml > filename.yaml will combine the contents of the existing files file1.yaml and file2.yaml into a new file called filename.yaml.

Concrete_Buddha
  • 171
  • 1
  • 6
  • 2
    Aside from the "permission denied" error (which is from the *shell*, not from `cat`, and reflects the user's lack of rights in the target directory), the command `cat > filename` will indeed create `filename` - however, it will hang waiting to read file contents from standard input. Simply hitting Ctrl-D will terminate the command, resulting in creation of an empty file, similar to `touch filename` or `> filename` – steeldriver Jun 11 '20 at 22:18
  • A good point, thank you; I've learnt something. As Ctrl-D means EOF it makes sense that this termination would result in an empty file. However having said that `cat` still probably won't be the first tool out of the box next time I want to create an empty file ;) – Concrete_Buddha Jun 12 '20 at 08:45
0

If you want to create a file, you can run:

touch filename.yaml (if the current directory is owned by your user)

or

sudo touch filename.yaml (if you are not the owner of the directory from where you are tunning the command)

For the reason why cat is not the proper command, you can refer to the other answer (ad related comments) by Concrete_Buddha user

Lorenz Keel
  • 8,362
  • 8
  • 36
  • 49