fix permission denied error in using cat command

How to Fix Permission Denied Error While Using Cat Command

Cat command is commonly used to display file contents as well as write to files. But sometimes, especially in newer versions of Linux, you will get ‘permission denied’ error when you try to run cat command in terminal. In this article, we will learn about the reasons for this error and different ways to fix permission denied error while using cat command.


How to Fix Permission Denied Error While Using Cat Command

Open terminal and try to run the following cat command.

$ sudo cat > list
bash: l: Permission denied

If you get permission denied error, then you need to follow the steps mentioned below to fix it. In the above command we are simply creating a new empty file called list. But we are being denied to do so. In most cases, you will find that you get this error only when you use cat command to write or append content to a file. For example, the following command may not give you any error, and will show you the contents of file1.

$ cat file1

You will see similar errors, when you try to concatenate contents of multiple files and write the output to another file.

$ cat file1 file2 > file3
bash: l: Permission denied

This happens mainly because you don’t have the permission to write to the output file mentioned in the above commands.

We will look at the different ways to do this. Here is a command that spawns a bash shell and executes the cat command within that shell. It generally overcomes the permission issue.

$ sudo bash -c 'cat file1 file 2 > file3'

You can also use it to append contents of one file to another.

$ sudo bash -c 'cat file1 >> file2'

If the above command doesn’t work on your system, you can try using tee command as shown below.

$ cat file1 | sudo tee -a file2

In this case, we use cat command to display the content of file1 and use tee command to append it to file2.

Alternatively, instead of using cat command, you can also use echo command to write to a file.

$ echo > list

If none of the above methods work for you, the most basic way is to change the file permission to make it writable first, use cat command to write command, and then change back its permissions to the original one. For example, here is the command to view the file permissions.

$ ls -a file3
-rwxr-xr-x.  6 root          root           4096 Apr 19 05:03 .

As you can see, the above file has write permission for root but it does not have write permission for user. So use chmod command to make the file writable for user.

$ chmod 777 file3
$ ls -a file3
-rwxrwxrwx.  6 root          root           4096 Apr 19 05:03 .

Now if you run cat command, it will work properly.

$ cat file1 file2 > file3

Once you have written to file3 command, you can change back the file’s permissions back to its original values.

$ chmod 755 file3
$ ls -a file3
-rwxr-xr-x.  6 root          root           4096 Apr 19 05:03 .

Alternatively, you can switch to root user using su command, use cat command to write to your desired file, and then switch user back to your previous one.

In this article, we have learnt several ways to fix ‘permission denied’ error in using cat command. This is a common issue on most Linux systems these days and can be quite annoying especially for beginners.

Also read:

How to Limit User Commands in Linux
How to Backup Website to Amazon S3
How to Check MD5 Checksum of Installed Packages
How to Check Bad Sectors in HDD in Ubuntu
How to Encrypt & Decrypt Files Using OpenSSL

Leave a Reply

Your email address will not be published. Required fields are marked *