Get current folder linux

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:

Источник

Earth Data Analytics Online Certificate

Intro to earth data science textbook
intro-to-earth-data-science-textbook Home

Lesson 2. Bash Commands to Manage Directories and Files

  • Run Bash commands to complete the following tasks:
    • print the current working directory ( pwd )
    • navigate between directories on your computer ( cd )
    • create new directories ( mkdir )
    • print a list of files and subdirectories within directories ( ls )
    • delete files ( rm ) and directories ( rm -r )
    • copy files ( cp ) and directories ( cp -r ) to another directory
    • easily create new files using a single command ( touch )

How to Run Bash Commands in the Terminal

In the previous section on Terminal Sessions, you learned that the terminal displays a prompt that shows you that Bash is waiting for input.

Recall that depending on your computer’s set-up, you may see a different character as a prompt and/or additional information before the prompt, such as your current location within your computer’s file structure (i.e. your current working directory).

When typing commands (either from this textbook or from other sources), do not type the dollar sign (or other character prompt). Only type the commands that follow it.

Note: In the examples on this page, the indented lines that follow a prompt and do not start with a dollar sign ($) are the output of the command. The results of the commands below on your computer will be slightly different, depending on your operating system and how you have customized your file system.

Useful Bash Commands to Manage Directories and Files

Your current working directory is the directory where your commands are being executed. It is typically printed as the full path to the directory (meaning that you can see the parent directory).

To print the name of the current working directory, use the command pwd .

As this is the first command that you have executed in Bash in this session, the result of the pwd is the full path to your home directory. The home directory is the default directory that you will be in each time you start a new Bash session.

Windows users: note that the Terminal uses forward slashes ( / ) to indicate directories within a path. This differs from the Windows File Explorer which uses backslashes ( \ ) to indicate directories within a path.

Change Current Working Directory ( cd )

Often, you may want to change the current working directory, so that you can access different subdirectories and files.

To change directories, use the command cd followed by the name of the directory (e.g. cd downloads ). Then, you can print your current working directory again to check the new path.

For example, you can change the working directory to an existing documents directory under your home directory, and then check that the current working directory has been updated.

You can go back to the parent directory of any current directory by using the command cd .. , as the full path of the current working directory is understood by Bash .

You can also go back to your home directory (e.g. /users/jpalomino ) at any time using the command cd

(the character known as the tilde).

Create a New Directory ( mkdir )

The first step in creating a new directory is to navigate to the directory that you would like to be the parent directory to this new directory using cd .

Then, use the command mkdir followed by the name you would like to give the new directory (e.g. mkdir directory-name ).

For example, you can create a new directory under documents called assignments . Then, you can navigate into the new directory called assignments , and print the current working directory to check the new path.

Notice that mkdir command has no output. Also, because assignments is provided to Bash as a relative path (i.e., doesn’t have a leading slash or additional path information), the new directory is created in the current working directory (e.g. documents ) by default.

Data Tip: Directory vs Folder: You can think of a directory as a folder. However, recall that the term directory considers the relationship between that folder and the folders within it and around it.

Data Tip: Notice that you are creating an easy to read directory name. The name has no spaces and uses all lower case to support machine reading down the road.

To see a list of all subdirectories and files within your current working directory, use the command ls .

In the example above, ls printed the contents of the home directory which contains the subdirectories called documents and downloads and the files called addresses.txt and grades.txt .

You can continue to change your current working directory to a subdirectory such as documents and print a new list of all files and subdirectories to see your newly created assignments directory.

You can also create a new subdirectory under assignments called homeworks , and then list the contents of the assignments directory to see the newly created homeworks .

Delete a File ( rm )

To delete a specific file, you can use the command rm followed by the name of the file you want to delete (e.g. rm filename ).

For example, you can delete the addresses.txt file under the home directory.

Delete a Directory ( rm -r )

To delete (i.e. remove) a directory and all the sub-directories and files that it contains, navigate to its parent directory, and then use the command rm -r followed by the name of the directory you want to delete (e.g. rm -r directory-name ).

For example, you can delete the assignments directory under the documents directory because it does not meet the requirement of a good name for a directory (i.e. not descriptive enough — what kind of assignments?).

The rm stands for remove, while the -r is necessary to tell Bash that it needs to recurse (or repeat) the command through a list of all files and sub-directory within the parent directory.

Thus, the newly created homeworks directory under assignments will also be removed, when assignments is deleted.

Copy a File ( cp )

You can also copy a specific file to a new directory using the command cp followed by the name of the file you want to copy and the name of the directory to where you want to copy the file (e.g. cp filename directory-name ).

For example, you can copy grades.txt from the home directory to documents .

Note that the original copy of the file remains in the original directory, so you would now have two copies of grades.txt , the original one in the home directory and the copy under documents .

Copy a Directory and Its Contents ( cp -r )

Similarly, you can copy an entire directory to another directory using cp -r followed by the directory name that you want to copy and the name of the directory to where you want to copy the directory (e.g. cp -r directory-name-1 directory-name-2 ).

Similar to rm -r , -r in cp -r is necessary to tell Bash that it needs to recurse (or repeat) the command through a list of all files and sub-directory within the parent directory.

Once again, the original copy of the directory remains in the original directory.

Create a New File Using a Single Command ( touch )

You can create a new empty file using the single command touch (e.g. touch file-name.txt ). This command was originally created to manage the timestamps of files. However, if a file does not already exist, then the command will make the file.

This is an incredibly useful way to quickly and programmatically create a new empty file that can be populated at a later time.

Project organization is integral to efficient research. In this challenge, you will use Bash to create an earth-analytics directory that you will use throughout this textbook.

You will then create a data directory within the earth-analytics directory to save all of the data that you will need to complete the homework assignments and follow along with the course.

Create a Directory for earth-analytics

Begin by creating an earth-analytics directory (or folder) in your home directory. Recall that this is the default directory in which the Terminal opens.

  • Create a new directory called earth-analytics .
  • Next, change your working directory to the earth-analytics directory, and create a new directory within it called data .
  • Last, go back to the home directory and confirm that you can then access the directories you just made.

Updated: September 03, 2020

The Intro to earth data science textbook course is subject to the CC BY-NC-ND 4.0 License . Citation DOI: https://doi.org/10.5281/zenodo.3382162

Share on

Twitter Facebook Google+ LinkedIn

Leave a Comment

You May Also Enjoy

Plot Data With Matplotlib

Calculate Seasonal Summary Values from Climate Data Variables Stored in NetCDF 4 Format: Work With MACA v2 Climate Data in Python

Learn how to calculate seasonal summary values for MACA 2 climate data using xarray and region mask in open source Python.

Calculate Summary Values Using Spatial Areas of Interest (AOIs) including Shapefiles for Climate Data Variables Stored in NetCDF 4 Format: Work With MACA v2 Climate Data in Python

Climate datasets stored in netcdf 4 format often cover the entire globe or an entire country. Learn how to subset climate data spatially and by time slices u.

How to Open and Process NetCDF 4 Data Format in Open Source Python

Historic and projected climate data are most often stored in netcdf 4 format. Learn how to open and process MACA version 2 climate data for the Continental U.

Источник

What is the current directory in a batch file?

I want to create a few batch files to automate a program.

My question is when I create the batch file, what is the current directory? Is it the directory where the file is located or is it the same directory that appears in the command prompt, or something else?

9 Answers 9

From within your batch file:

dp0 refers to the full path to the batch file’s directory (static)
%

f0 both refer to the full path to the batch directory and file name (static).

dp0 gives the full path to the directory that the executing batch file is in. %

dpnx0 (which is equivalent to %

f0) gives the full path to the batch file. See robvanderwoude.com/parameters.php for more details.

dp0 is the working directory not the batch files directory, Found this out the hard way.

dp0 gives the batch file directory with trailing slash.

dp0 is referencing drive and path of argument 0 of the batch file arguments. It is NOT an environment variable reference. It is a batch file argument reference. %

dp0 expands to full path of currently processed batch file. A batch file C:\Temp\Devlopement & Test()!\Main.bat with %

dp0 expanding to C:\Temp\Devlopement & Test()!\ on execution can call the batch file C:\Temp\SubBatch.cmd , for example with call «%

dp0..\SubBatch.cmd» , containing also %

dp0 expanding on execution to C:\Temp\ . Run call /? in a cmd window for help.

It usually is the directory from which the batch file is started, but if you start the batch file from a shortcut, a different starting directory could be given. Also, when you’r in cmd, and your current directory is c:\dir3 , you can still start the batch file using c:\dir1\dir2\batch.bat in which case, the current directory will be c:\dir3 .

In a batch file, %cd% is the most commonly used command for the current directory, although you can set your own variable:

So say you were wanting to open Myprog.exe. If it was in the same folder, you would use the command:

That would open Myprog from the current folder.

The other option is to make a directory in C: called AutomatePrograms. Then, you transfer your files to that folder then you can open them using the following command:

dp0 is more consistent.

Say you were opening a file in your current directory. The command would be:

I hope I answered your question.

It is the directory from where you run the command to execute your batch file.

As mentioned in the above answers you can add the below command to your script to verify:

It is the directory from where you start the batch file. E.g. if your batch is in c:\dir1\dir2 and you do cd c:\dir3 , then run the batch, the current directory will be c:\dir3 .

%__CD__% , %CD% , %=C:%

There’s also another dynamic variable %__CD__% which points to the current directory but alike %CD% it has a backslash at the end. This can be useful if you want to append files to the current directory.

With %=C:% %=D:% you can access the last accessed directory for the corresponding drive. If the variable is not defined you haven’t accessed the drive on the current cmd session.

Источник

Читайте также:  Rainmeter windows 10 не запускается
Оцените статью