Linux current dir name

get_current_dir_name(3) — Linux man page

getcwd, getwd, get_current_dir_name — get current working directory

Synopsis

Description

These functions return a null-terminated string containing an absolute pathname that is the current working directory of the calling process. The pathname is returned as the function result and via the argument buf, if present.

The getcwd() function copies an absolute pathname of the current working directory to the array pointed to by buf, which is of length size.

If the length of the absolute pathname of the current working directory, including the terminating null byte, exceeds size bytes, NULL is returned, and errno is set to ERANGE; an application should check for this error, and allocate a larger buffer if necessary.

As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc(3) if buf is NULL. In this case, the allocated buffer has the length size unless size is zero, when buf is allocated as big as necessary. The caller should free(3) the returned buffer.

get_current_dir_name() will malloc(3) an array big enough to hold the absolute pathname of the current working directory. If the environment variable PWD is set, and its value is correct, then that value will be returned. The caller should free(3) the returned buffer.

getwd() does not malloc(3) any memory. The buf argument should be a pointer to an array at least PATH_MAX bytes long. If the length of the absolute pathname of the current working directory, including the terminating null byte, exceeds PATH_MAX bytes, NULL is returned, and errno is set to ENAMETOOLONG. (Note that on some systems, PATH_MAX may not be a compile-time constant; furthermore, its value may depend on the file system, see pathconf(3).) For portability and security reasons, use of getwd() is deprecated.

Return Value

On success, these functions return a pointer to a string containing the pathname of the current working directory. In the case getcwd() and getwd() this is the same value as buf.

On failure, these functions return NULL, and errno is set to indicate the error. The contents of the array pointed to by buf are undefined on error.

Errors

Permission to read or search a component of the filename was denied.

buf points to a bad address.

The size argument is zero and buf is not a NULL pointer.

getwd(): buf is NULL. ENAMETOOLONG getwd(): The size of the null-terminated absolute pathname string exceeds PATH_MAX bytes. ENOENT

The current working directory has been unlinked.

The size argument is less than the length of the absolute pathname of the working directory, including the terminating null byte. You need to allocate a bigger array and try again.

Conforming To

getcwd() conforms to POSIX.1-2001. Note however that POSIX.1-2001 leaves the behavior of getcwd() unspecified if buf is NULL.

getwd() is present in POSIX.1-2001, but marked LEGACY. POSIX.1-2008 removes the specification of getwd(). Use getcwd() instead. POSIX.1-2001 does not define any errors for getwd().

Читайте также:  Сколько всего свободного места windows

get_current_dir_name() is a GNU extension.

Notes

Under Linux, the function getcwd() is a system call (since 2.1.92). On older systems it would query /proc/self/cwd. If both system call and proc file system are missing, a generic implementation is called. Only in that case can these calls fail under Linux with EACCES.

These functions are often used to save the location of the current working directory for the purpose of returning to it later. Opening the current directory («.») and calling fchdir(2) to return is usually a faster and more reliable alternative when sufficiently many file descriptors are available, especially on platforms other than Linux.

Источник

Linux World

Pages

C program in linux to find the current working directory

Here are three ways in which we can get the path to the current working directory using a c program,

1. we can make use of the function «getcwd».
Header file required : unistd.h
The syntax of getcwd is

buf: Is the character pointer in which the path of current working directory is stored. size: Is the maximum size of the path in bytes, if the size exceeds this then a NULL is returned

Here is a program that shows the usage of getcwd.

Save the program is get_cwd.c. Compile and execute it.

Header file required : unistd.h

The function returns the absolute path to the current working directory. The function itself mallocs for an array big enough to hold the current directory name, thus we don’t need to do malloc in the program. This function returns the value of the environment variable PWD, thus if it is not set the function might fail to return the current value.
Here is a program which uses get_current_dir_name to get the current working directory.

Save the file as get_current_dir.c. Compile and execute it.

3. The third way of getting the current working directory is using the command «pwd» along with the function «system». The system function executes which ever command is passed to it and prints the result on the screen. Thus we can execute the command system and pass the command «pwd» as the argument, the result of which will be the current working directory being printed on the screen.

The following program shows how the same can be implemented.

Источник

How can I get the current working directory? [duplicate]

I want to have a script that takes the current working directory to a variable. The section that needs the directory is like this dir = pwd . It just prints pwd how do I get the current working directory into a variable?

5 Answers 5

There’s no need to do that, it’s already in a variable:

The PWD variable is defined by POSIX and will work on all POSIX-compliant shells:

Set by the shell and by the cd utility. In the shell the value shall be initialized from the environment as follows. If a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory that is no longer than bytes including the terminating null byte, and the value does not contain any components that are dot or dot-dot, then the shell shall set PWD to the value from the environment. Otherwise, if a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory, and the value does not contain any components that are dot or dot-dot, then it is unspecified whether the shell sets PWD to the value from the environment or sets PWD to the pathname that would be output by pwd -P. Otherwise, the sh utility sets PWD to the pathname that would be output by pwd -P. In cases where PWD is set to the value from the environment, the value can contain components that refer to files of type symbolic link. In cases where PWD is set to the pathname that would be output by pwd -P, if there is insufficient permission on the current working directory, or on any parent of that directory, to determine what that pathname would be, the value of PWD is unspecified. Assignments to this variable may be ignored. If an application sets or unsets the value of PWD, the behaviors of the cd and pwd utilities are unspecified.

For the more general answer, the way to save the output of a command in a variable is to enclose the command in $() or ` ` (backticks):

Of the two, the $() is preferred since it is easier to build complex commands like:

Источник

How do I change directories in the terminal, and know which directory I’m in?

How do I tell what is in which directory and how do I navigate the directory. I know I must be leaving out something simple.

3 Answers 3

In your session ls displays the content of the current directory (but not hidden files starting with a dot in the filename). You navigate into another directory by typing cd dirname . Here you have to substitude «dirname» by a directory’s name you want to change into. Normally you start out with your home directory as current working directory. In your case that is most likely /home/christy . If ls shows you a directory called «Template» you can change into «Template» (=make it your current working directory) by typing (mind the space) the command:

you will change your current working directory to «Templates» or print «No such file or directory», if a dir of that name does not exist in your current direcotry. Typing pwd will always give you the full patch to your current working directory including parents.

You can supply optional arguments to each command. For cd the string «Templates» was such an optional argument. If you type ls -a for instance, the ls command will print out hidden files as well. Or with ls Templates it will print the content of the Tempaltes directory instead of you current working dir’s content. Arguments must be separated by at least one space from the command name and from each other. The lack of a space between the two was the reason your command did not work.

Since the space character has a special meaning, you will need to quote or escape it, if it’s part of an argument. So if for instance you want to change your current working directory to «Source Files» you need to type one of these commands:

The upper line being «escaped» while the lower line is «quoted».

If you want to learn more the better guide for you might be: Introduction to Linux (pdf) (html)

Edit:

Okay now, sorry I didn’t see your link there. Your post was a bit misleading, now I think I got you:

So you want to install tex-live from the source archive that is linked in the «Getting Started» guide you posted. To do that you must first remove any pre-existing installation of tex-live. You do that by opening a terminal doing this:

At this point you have to enter you password and will be rewarded with a few boring messages, that I will omit here. Then you’ll have to download the archive named install-tl-unx.tar.gz (you already did that and don’t have to download it again, but I show you how to do it none the less just in case):

Now that you have downloaded the archive you can unpack it

Now that you’ve unpacked the archive you can change into the directory that was unpacked.

The number «20120701» is a timestamp saying which version of tex-live you have. This number might be different for you but the command ls | grep install-t1- will tell you what to use instead. If all this worked, you can run the install script from here:

From here on just answer the questions you’re asked by the install script and you should be fine.

Источник

Save current directory in variable using Bash?

What I’m trying to do is find the current working directory and save it into a variable, so that I can run export PATH=$PATH:currentdir+somethingelse . I’m not entirely sure if they have a variable that contains cwd by default.

How do I save the current directory in variable using Bash?

9 Answers 9

This saves the absolute path of the current working directory to the variable cwd :

In your case you can just do:

I have the following in my .bash_profile:

so anytime I want to remember a directory, I can just type, e.g. mark there .

Then when I want to go back to that location, I just type cd $there

Your assignment has an extra $ :

for a relative answer, use .

The first ls will show you everything in the current directory, the second will show you everything in the root directory ( / ).

current working directory variable ie full path /home/dev/other

print the full path

On a BASH shell, you can very simply run:

No need to save the current working directory into a variable.

One more variant:

You can use shell in-build variable PWD , like this:

Similar to solution of mark with some checking of variables. Also I prefer not to use $variable but rather the same string I saved it under

save your folder/directory using save dir sdir myproject and go back to that folder using goto dir gdir myproject

in addition checkout the workings of native pushd and popd they will save the current folder and this is handy for going back and forth. In this case you can also use popd after gdir myproject and go back again

another handy trick is to combine the two pushd/popd and sdir and gdir wher you replace the cd in the goto dir function in pushd. This enables you to also fly back to your previous folder when making the jump to the saved folder.

Источник

Оцените статью