C Programming

Linux chdir System Call in C

Changes to a device’s present working directory are made using the “CHDIR” method. In a variety of circumstances, you may decide to change your device’s present working folder. Or you want to run a C script from a folder instead of your present working directory. Consequently, to run that specific C code, you must modify your present working folder. Additionally, the “getcwd” method of the coding language C may be used to quickly determine when the present working folder has changed. Now, we will be using the chdir() function of C in this article to change a working directory.

Example 01:

Let’s start with the working of the chdir() function of C in the Linux operating system. For this, you have to make it pretty sure that the compiler of C is already mounted at your end. In most cases, we tend to use the “gcc” compiler of C in our Kali Linux operating system. To start with this example, we must have a C file with the “c” extension. We named it p1.c and tried to open it with the “nano” editor of Linux as presented below.

When your file gets opened in the editor, you have to start coding in it. We have been starting our C program by adding the main header libraries of C that are the must-haves in any C program. Therefore, the stdio.h and unistd.h header libraries are used to make sure that the standard input and output can flow within the program. The unistd.h header has been used to make use of some standard functions of C that are required for this example especially. Now, we have started the main() driver function of C in this code to run automatically on execution. The character type array variable “path” has been defined with a size of 100 because it stores only 100 values.

The printf() function statement has been used to simply print out the current working directory folder of this C file using the “getcwd()” function. This function uses the “path” array variable with a size of 100 to get the current working directory in it and pass it to the printf() function statement for display. Now, it’s time to use the chdir() function. We can pass any sort of characters to specify whether we want to move forward in the directory or move in the backward direction within the same directory.

So, we have tried the “..” characters in the chdir() function statement to move from the current working directory in the backward direction i.e. one level below the current working directory. Note that right now, we have been working in the project folder of our main “Linux” working directory of the Kali Linux system. Now, we are at the “Linux” directory instead of the “project” directory after using the “chdir” function. Now, we tried the “printf()” function statement once again to display the current working directory using the “getcwd” function in it. Our program ends with the return 0 statement.

After saving this C program, we need to compile it using the “gcc” compiler that we have already configured at our end. Using this gcc instruction, we have created an object file of this C file named “p1.out” using the –o option as displayed below.

Now, when you run its object file in your Linux terminal, you will first get the working directory used by the program before the use of the chdir() function. After the use of the chdir() function, we moved to the one directory below and that particular directory “home” is displayed.

Example 02:

Using the very same header libraries, we have started this program code. Within the main() function of this program, we have been using the “if” statement utilizing the chdir() function in it.

This function will be checking whether the “/user” exists in the system or not by setting the condition “/user!=0”. If the directory doesn’t exist in the current system, the perror function call will be used to display the respective error message i.e “user directory doesn’t exist. We have been utilizing another “if” statement using the chdir() function within it. The purpose of using the chdir() function here is also the same as we did in the above if statement, which is for checking whether the directory exists or not using the “/temp”!=0 condition. The only difference is the use of a new directory name “/temp” instead of “/user” as you can see from the code image below.

If the /temp directory doesn’t exist in the system, the perror function used in the “if” statement would throw an error message “/temp directory does not exist” as displayed from the image below. After the use of both the “if” statements, we have been ending this code with the C’s return 0 statement. Now, the program is over. We have been saving our C code using the Ctrl+S shortcut before its execution.

Now that the file has been saved in your system, we have to compile the C code with the “gcc” compiler we have already mounted at our end. Using this instruction, we have been creating its object file “p2.out” using the –o option in the instruction.

After executing the created object file “p2.out” with the “./” characters, we learned that both the directories mentioned in the path of “chdir” don’t exist in the system or the error displayed at the shell has been demonstrating it properly.

Conclusion

Within this article today, we have demonstrated the use of the chdir() function of C in the Kali Linux operating system. The very first example illustrates how the chdir() function can be utilized to change a working directory i.e., move from one directory to another. The last example of C demonstrates how we can check if the current working directory specified in the path of chdir() has been existing or not.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.