chdir() in C language with Examples

The chdir command is a system function (system call) that is used to change the current working directory. On some systems, this command is used as an alias for the shell command cd. chdir changes the current working directory of the calling process to the directory specified in path.

Syntax:

int chdir(const char *path);

Parameter: Here, the path is the Directory path that the user want to make the current working directory.
Return Value: This command returns zero (0) on success. -1 is returned on an error and errno is set appropriately.
Note: It is declared in unistd.h.

Example 1:

C

// chdir function is declared // inside this header // printing current working directory printf ( "%s\n" , getcwd(s, 100)); // using the command // printing current working directory printf ( "%s\n" , getcwd(s, 100)); // after chdir is executed

Output:

Note: The above program changes the working directory of a process. But, it doesn’t change the working directory of the current shell. Because when the program is executed in the shell, the shell follows fork on exec mechanism. So, it doesn’t affect the current shell.

Example 2:

C

// Main Method // changing the current // working directory(cwd) if (chdir( "/usr" ) != 0) perror ( "chdir() to /usr failed" ); // changing the cwd to /tmp if (chdir( "/tmp" ) != 0) perror ( "chdir() to /temp failed" ); // there is no /error // directory in my pc if (chdir( "/error" ) != 0) // so chdir will return -1 perror ( "chdir() to /error failed" );

Output:

Errors: There can be errors that can be returned. These depend on the filesystem.

Like Article -->

Please Login to comment.

Similar Reads

Creating Database Table Using Hive Query Language (HQL)

Hive is a data warehouse solution built on top of Hadoop. In Hive data is managed at Hadoop Distributed file system (HDFS). In this schema, on reading no constraint check is required as it is required in RDBMS. It is particularly meant to work with a very large dataset. Hive uses query language known as Hive Query Language (HQL). Steps to Create Ta

1 min read Deepl - Command Line Language Translator Tool for Linux

Deepl is the best language translator command-line tool that detects the language of input text and translates it into the desired language using advanced machine learning and neural networking algorithms. It is open source and powered by a German tech Company under the MIT license. It supports the following languages English (EN)Italian (IT)Spanis

3 min read How to Install Go Programming Language in Linux?

In this article, we will discuss how to install GoLang (Go Programming Language) in Linux. But at first let's know a brief about GoLang. GoLang is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled u

2 min read How to install Ruby Programming Language on Manjaro Linux?

Ruby is an open-source, dynamic, cross-platform, object-oriented, general-purpose programming language. It was developed by Yukihiro Matsumoto in the mid-1990s in Japan. Everything in Ruby is an object except the blocks but there are replacements for those as well, i.e procs and lambda. The language is based on many other languages, such as Perl, L

1 min read How to install Ruby Programming Language on Godaddy Server?

GoDaddy VPS is a shared server that provides computational services, databases, storage space, automated weekly backups, 99% uptime, and much more. It’s a cheaper alternative to some other popular cloud-based services such as AWS, GPC, and Azure. Ruby is an open-source, dynamic, cross-platform, object-oriented, general-purpose programming language

1 min read What Type of Language is YAML?

As we know the world of technology is filled with various modern and old languages which keep changing from time to time, each one of these languages serves a specific purpose and thus each language is used for performing certain tasks only. But when it comes to the programming language called YAML people sometimes get confused because they have tr

10 min read select command in Linux with examples

select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print

1 min read atrm command in Linux with examples

atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job. ] Options: -V : Used to print the version number atrm -V job : Job number of the job which i

1 min read expand Command in LINUX with examples

Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n

3 min read help Command in Linux with examples

If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy

3 min read rcp Command in Linux with examples

There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way

4 min read fmt command in Linux with examples

fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax

4 min read unexpand command in Linux with Examples

To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here's the syntax of unexpand command : Syntax : $unexpand [OPTION]. [FILE]. where, OPTION refers to the options compatib

2 min read uniq command in Linux with examples

The linux uniq command is basically used to remove all repeated lines in a file. This command is used when a line is repeated multiple times and replace these multiple lines with one line. This command is designed to work on sorted files. Purpose : Remove repetitious lines from the sorted 'input-file' and send unique lines to the 'output-file'. If

2 min read anacron command in linux with examples

anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j

3 min read for command in Linux with Examples

for command in Linux is used to repeatedly execute a set of command for every element present in the list. Syntax: for NAME [in WORDS . ] ; do COMMANDS; done Example: Options: help for : It displays help information.

1 min read autoheader command in Linux with Examples

autoheader command in Linux is used to create a template file of C "#define" or any other template header for configure to use. If the user will give autoheader an argument, it reads the standard input instead of reading configure.ac and also writes the header file to the standard output. This command scans the configure.ac file and figures out whi

1 min read expect command in Linux with Examples

expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script that will be needing inputs from users and then we

2 min read function command in Linux with examples

The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the user's current shell session or saved in the shell

2 min read fg command in Linux with examples

fg command in linux used to put a background job in foreground. Syntax: fg [job_spec] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this job if n

1 min read bg command in Linux with Examples

bg command in linux is used to place foreground jobs in background. Syntax: bg [job_spec . ] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this

1 min read Clear Command in Linux with Examples

clear is a standard Unix computer operating system command that is used to clear the terminal screen. This command first looks for a terminal type in the environment and after that, it figures out the terminfo database for how to clear the screen. And this command will ignore any command-line parameters that may be present. Also, the clear command

2 min read Continue Command in Linux with examples

continue is a command which is used to skip the current iteration in for, while, and until loop. It is used in scripting languages and shell scripts to control the flow of executions. It takes one more parameter [N], if N is mentioned then it continues from the nth enclosing loop. The syntax for the `continue` command in Linuxcontinue or continue [

2 min read hostid command in Linux with examples

hostid is a command in Linux that is used to display the Host's ID in hexadecimal format. It provides a quick and straightforward way to retrieve the host ID, allowing administrators to associate it with software licenses or perform system-specific operations. Syntax of `hostid` command in Linuxhostid [OPTION] Options provided by hostedhostid --hel

1 min read Dirname Command in Linux with Examples

dirname is a command in Linux that is used to remove the trailing forward slashes "/" from the NAME and prints the remaining portion. If the argument NAME does not contain the forward slash "/" then it simply prints dot ".". In other words, we can say that the `dirname` command is a useful tool for extracting the directory portion from a given path

2 min read sdiff command in Linux with Examples

sdiff command in linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical. It displays greater than sign if the line only exists in the file specified by the File2 parameter, and a | (vertical bar

2 min read Groups Command in Linux With Examples

In Linux, there can be multiple users (those who use/operate the system), and groups are nothing but a collection of users. Groups make it easy to manage users with the same security and access privileges. A user can be part of different groups. The `groups` is a powerful tool that helps administrators to view the group memberships of a user and re

2 min read factor command in Linux with examples

The factor command in Linux is used to print the prime factors of the given numbers, either given from command line or read from standard input. The numbers given through standard input may be delimited by tabs, spaces or newlines. Syntax: factor [NUMBER] Factor 100 (non-prime number): We get the prime factors 2 and 5 that make up 100. Factor 13 (p

1 min read chpasswd command in Linux with examples

chpasswd command is used to change password although passwd command can also do same. But it changes the password of one user at a time so for multiple users chpasswd is used. Below figure shows the use of passwd command. Using passwd we are changing the password of the guest user. Here first you have to enter the password of the currently signed u

2 min read dir command in Linux with examples

dir command in Linux is used to list the contents of a directory. How is dir command different from ls? dir command differs from ls command in the format of listing contents that is in default listing options. By default, dir command lists the files and folders in columns, sorted vertically and special characters are represented by backslash escape