Bash get parent directory of file




















Amir: Are you starting with a full path, or just a filename? If it's just a filename, the dirname command will assume it's in the current directory aka ". Amir: Shell variables are case sensitive, and PWD must be capitalized. Community Bot 1. The question has nothing to do with the current directory. There are some edge cases this doesn't handle well, mainly when there isn't a full multilevel path. But what is the parent directory of foofile? If it isn't full path can't know maybe if foofile is an existing file not only a "string".

This is useful if no parent were actually passed in, e. So, our search pattern was between the first and second , characters. Dominic van der Zypen Dominic van der Zypen 4 4 bronze badges. In case the script becomes a multiline solution, how can it be aliased to a command for cmder where should one store it? Lee James Lee James 11 1 1 bronze badge. 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. 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.

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.

To delete a specific file, you can use the command rm followed by the name of the file you want to delete e. For example, you can delete the addresses. To delete i. 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. Principle Not sure about what guarantees you have and want, so offering anyway.

Answer from simple to more complex cases of the original question. Fantastic answer. Jon Egeland Jon Egeland Running that code gives me the error bash: dirname 'Test': syntax error: invalid arithmetic operator error token is "'Test'". Linked code is also wrong. Marcelo Lacerda Marcelo Lacerda 1 1 gold badge 11 11 silver badges 29 29 bronze badges.

Good to know. It's a software part of gnu core utils which may or may not be installed on your system. Kaustubh Kaustubh 6 6 silver badges 21 21 bronze badges. This would go up to the parent folder cd.. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog. It only takes a minute to sign up. Connect and share knowledge within a single location that is structured and easy to search. With dirname :. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Learn more.



0コメント

  • 1000 / 1000