Resolving the ‘File Not Found’ Error on Linux

If you receive the File Not Found error on your Linux system, it typically means that the file or directory you are attempting to access does not exist. It could also indicate that you do not have the proper file permissions on the file or directory. There are a ways we can resolve this error, which we will look at how to do in this tutorial.

In this tutorial you will learn:

  • How to check if a file or directory exists
  • How to view permissions on a file or directory
  • How case sensitivity affects Linux commands
  • How to search for a file with find
  • How to create a new file or directory
Resolving the 'File Not Found' Error on Linux
Resolving the ‘File Not Found’ Error on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software ls, chmod, find, nano, touch
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Resolving the ‘File Not Found’ Error on Linux




Run through the steps below where we show the Linux commands necessary to check if a file or directory exists, and then verify that our user account has the appropriate permissions for access. After checking to make sure that the file or directory indeed does not exist, we will see how to search for the file or create it ourselves.

  1. Let’s start by checking to see if the file or directory appears when we execute the ls command on it. Just specify the absolute or relative path to the file in question.
    $ ls /path/to/file
    

    Does ls show that your file exists? If not, then we can move on to further steps. If it does exist, then check your other commands for typos, or proceed further to check for permission problems.

  2. Keep in mind that Linux is case sensitive. The Linux terminal is rather unforgiving about typos, so make sure that you are typing your file name or directory exactly as it appears. Remember that characters like spaces and parentheses must be escaped with a \ character. Take these commands for example, which would create three separate files that all have the same spelling but different capitalization:
    $ touch example-file
    $ touch Example-File
    $ touch ExAmPlE-fIlE
    

    Special characters must be escaped:

    $ touch example\ file
    $ touch \(example\ file\)
    
  3. The ls -l command will show you all the details about a file or directory’s permissions. Use this information to see if your account has the proper permissions to either read, write, or execute the file (whichever one you are trying to do).
    $ ls -l /path/to/file
    

    More help: Fixing the ‘Permission Denied’ Error on Linux

  4. If you have made it this far, then it is safe to say that the file indeed does not exist. Or, at least not where you thought it was supposed to be. Now it is time to use the find command to see if we can find the file we are looking for. The following syntax will search your current directory and all subdirectories for the file you want to find:


    $ find . -name "example.txt"
    
    NOTE – MORE FIND EXAMPLES
    find is a complex command that we cover in detail in our article on Locate vs find: What is the difference. You can also learn about the locate command there, which may be a better option for pinpointing the location of the file you are after.
  5. At this point, we can be sure that the file (or directory) does not exist. The only thing left to do is create it. To create a new empty file, we can use the touch command:
    $ touch file-name
    

    Or to make a new directory:

    $ mkdir some-directory
    

    You may also want to create a new plaintext file and start editing it with nano. Just use the nano command and the name of the file you want to create or edit:

    $ nano file-name
    

Closing Thoughts




In this tutorial, we saw how to resolve the File Not Found on a Linux system. There is not much mystery behind this error; it basically just means that the file you want to access does not exist. Therefore we have a few options, like trying to search for the file or just creating it from scratch. After running through the steps above, you now have a clear idea of how the Linux file system works, and how to search for files and edit the permissions as necessary.



Comments and Discussions
Linux Forum