Linux

No such file or directory Error

1. Introduction

When you run commands, sometimes part of the command is incorrect or the command is unable to execute the action. In case of unexpected behavior, an error will arise, like “No such file or directory Error”. Most of the time the errors should be self-explanatory for the problem of the executed command. In Linux, when you run some programs, in case of an error it gives you some error numbers which doesn’t say too much information.

You can see a table with all common errors in Linux here.

2. Cause of this problem

There can be multiple causes that this error appears. We will go through all of them step by step.

2.1. Reasons for appearing that error

The first reason for showing that error is because the file or directory upon which we want to execute a command does not exist. Please make sure the given file exists and there is not any typo in the path or on the file name. If you are using a relative path when executing a file try to use an absolute path instead.

The second reason might be, in case you are using the wrong interpreter. The first line in your script defines which interpreter to use. This is called shebang(#!/bin/bash). That line of code tells the operating system which shell to use, to parse the file. In the example below we will show you that, in case you defined a wrong interpreter the: “No such file or directory” will show up.

Error with No such file or directory, in case of a wrong interpreter.
Fig. 1: Error with No such file or directory, in case of a wrong interpreter.

Third reason for showing that error is because of some missing libraries on your system. To examine the file, use the file command: file ./testFile.sh. If the file is a 32-bit executable, you will need to install some libraries to execute it on a 64-bit architecture OS.

2.2. Installing dos2unix

In the Unix system, the line feed is: \n as the end of the line and, compared with Windows it uses carriage return and line feed \n\r. Running a script that was written on Windows might cause that error. To clear that carriage return to execute that file on Unix you can use the dos2unix utility.

In case that utility is not present on your Unix distribution you can run this command: sudo apt-get install dos2unix. To remove the carriage return just simply run that utility on your executable file: dos2unix testFile.sh and after that run your file: ./testFile.sh. In the below image we will show how to use the dos2unix utility. Even if in this example there is not any “No such file or directory Error” we can see that after running that tool again the carriage won’t show up.

The dos2unix utility.
Fig. 2: The dos2unix utility.

3. ldd Linux Command

Ldd is a powerful command-line tool that allows you to print the shared object dependencies of a file. In Linux systems, usually, we work with executable files either from terminal or graphical applications. Most of the executables contain shared libraries which are files that are shared across programs. In Windows, libraries are formatted in DLL files compared with Linux where they are in the form of .o or .so files. Libraries are one or more pre-compiled resources such as function, subroutines, classes, or values. In Linux, libraries are usually located in the /lib or /usr/lib. There are two types of libraries: Static libraries and Dynamic or Shared Libraries.

Usually, this command is available on most Unix distributions. In case you want to install it run: sudo apt-get install libc-bin.

To see what dynamic loader is required, you can run on your file this command: ldd <your_file>.

The output of the ldd command executed on a file:

Output of the command ldd executed on a file.
Fig. 3: Output of the command ldd executed on a file.

More information about this command you can find in the official documentation.

4. readelf Linux Command

Every time when you compile a source code an output file (object file) will be generated with the help of a linker. This object file will be converted to a binary file which only the machine will understand. The readelf will help you to display information about one or more ELF (Executable and Linkable Format) files.

There are multiple options that you can display. These can be controlled from the available options that this command offers. Some available options available e.g.: display file header (-h or –file-header), program headers (-l, –program-headers or –segments), sections (-s, –sections or –section-headers), display all information equivalent to all of these flags: -file-header, –program-headers, –sections, –symbols, –relocs, –dynamic, –notes, –version-info, –arch-specific, –unwind, –section-groups and –histogram (-a or -all) or other available options that you can find in the official documentation.

This powerful tool should be already present on most Unix distributions. In case you want to install it run this command: sudo apt-get install binutils. This utility will install both tools readelf and objdump.

The output of the readelf shows all the file headers for an executable file.

Output of the command readelf showing all
Fig. 4: Output of the command readelf showing all.

More information about this command you can find in the official documentation.

5. objdump Command

With the objdump command you can extract information about an object file. This is useful for the programmers who work on compilers but also a handy tool for normal programmers in case they want to debug a program. This command can control what particular information to display based on the available options that this command offers.

Here are some available options e.g.: show archive headers (-a or –archive-header), show debugging information(-g or –debugging), debugging tags (-e or debugging-tags), disassemble the input file in the machine instructions (-d, –disassemble) or other available options that you can find in the official documentation.

As we already mentioned, in the below sections, this tool should be already present on most Unix distributions. In case you want to install it run this command: sudo apt-get install binutils.

The output of the objdump command executed on a file to show all existing section headers:

Output of the objdump command
Fig. 5: Output of the objdump command.

For more information about this powerful tool, you can find it in the official documentation.

6. Final word

In this article, we went to the most common known error when working with files and directories known as “No such file or directory error”. Errors are very important and we have to be able to handle them to know the cause of this error.

Besides the above error, we got familiar with some useful commands to view an executable file shared dependencies (ldd), to display information about one or more ELF (Executable and Linkable Format) format object files (readelf) and the last command is used for disassembling a binary file (objdump). With these in mind, it will help you to gain some knowledge of Linux.

Iulian Timis

My name is Iulian and I graduated Faculty of Electronics, Telecommunications, and Information Technology. My master's degree was in Computer Science, Information Security. I am passionate about technology and the security area. During my career I was working as a Java Developer, on the backend and frontend side, sometimes doing some DevOps tasks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button