Questions tagged [dirname]

138 questions
325
votes
13 answers

Getting the parent of a directory in Bash

If I have a file path such as... /home/smith/Desktop/Test /home/smith/Desktop/Test/ How do I change the string so it will be the parent directory? e.g. /home/smith/Desktop /home/smith/Desktop/
YTKColumba
  • 4,023
  • 4
  • 19
  • 21
115
votes
9 answers

How to get the last part of dirname in Bash

Suppose I have a file /from/here/to/there.txt, and want to get only the last part of its dirname to instead of /from/here/to, what should I do?
eggplantelf
  • 1,342
  • 2
  • 9
  • 7
101
votes
11 answers

Getting a directory name from a filename

I have a filename (C:\folder\foo.txt) and I need to retrieve the folder name (C:\folder) in C++. In C# I would do something like this: string folder = new FileInfo("C:\folder\foo.txt").DirectoryName; Is there a function that can be used in C++ to…
Jon Tackabury
  • 47,710
  • 52
  • 130
  • 168
98
votes
9 answers

PHP how to go one level up on dirname(__FILE__)

I have a folder structure as follows: mydomain.example ->Folder-A ->Folder-B I have a string from Database that is ../Folder-B/image1.jpg, which points to an image in Folder-B. Inside a script in Folder-A, I am using dirname(__FILE__) to fetch…
aVC
  • 2,254
  • 2
  • 24
  • 46
24
votes
7 answers

How-To get root directory of given path in bash?

My script: #!/usr/bin/env bash PATH=/home/user/example/foo/bar mkdir -p /tmp/backup$PATH And now I want to get first folder of "$PATH": /home/ cd /tmp/backup rm -rf ./home/ cd - > /dev/null How can I always detect the first…
checker284
  • 1,286
  • 3
  • 14
  • 29
20
votes
2 answers

How can I determine the current directory name in R?

The only solution I've encountered is to use regular expressions and recursively replace the first directory until you get a word with no slashes. gsub("/\\w*/","/",gsub("/\\w*/","/",getwd())) Is there anything slightly more elegant? (and more…
M. Tibbits
  • 8,400
  • 8
  • 44
  • 59
15
votes
5 answers

Python idiom to get same result as calling os.path.dirname multiple times?

I find myself needing to get a parent directory of a python file in a source tree that is multiple directories up with some regularity. Having to call dirname many times is clunky. I looked around and was surprised to not find posts on this. The…
Tim Wilder
  • 1,607
  • 1
  • 18
  • 26
14
votes
5 answers

OS X bash: dirname

I want to create a simple bash script to launch a Java program on OS X. The names of the file, the file path, and the immediate working folder all contain spaces. When I do this: #!/bin/sh cd `dirname $0` I get usage: dirname path I have also…
Dinah
  • 52,922
  • 30
  • 133
  • 149
10
votes
5 answers

Use GNU versions of basename() and dirname() in C source

How do I use the GNU C Library version of basename() and dirname()?. If you #include for dirname You're already getting the POSIX, not the GNU, version of basename(). (Even if you #define _GNU_SOURCE As far as I know there is no…
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
9
votes
5 answers

PHP dirname returns symlink path

Say I have a symlink from '/one/directory/' to '/two/directory/'. If I echo dirname(dirname(\__FILE__)), it returns '/one/directory/'. What is the best method to return '/two/directory'? Example usage: Vhost 'example.com' pointing to…
adamstrawson
  • 115
  • 1
  • 2
  • 8
8
votes
2 answers

dirname() in C: is the manual wrong?

Citing the manual here: The functions dirname() and basename() break a null-terminated pathname string into directory and filename components. In the usual case, dirname() returns the string up to, but not including, the final '/', and…
Gui13
  • 12,993
  • 17
  • 57
  • 104
7
votes
3 answers

Changing to a directory and then getcwd()

Many of my colleagues use the following commands in their BEGIN block. $scriptDir = dirname($0); chdir($scriptDir); $scriptDir = getcwd(); I have looked around and can't help but think that the third line i.e. $scriptDir = getcwd(); is redundant.…
tarunkt
  • 306
  • 2
  • 12
6
votes
2 answers

what is the purpose of require_once dirname(__FILE__) ...?

I am using a php library which has this code: require_once dirname(__FILE__) . '/config.php'; From what I've read, dirname(__FILE__) points to the current directory. So wouldn't it be easier to just write require_once 'config.php';? My only guess…
Leo Galleguillos
  • 2,429
  • 3
  • 25
  • 43
5
votes
7 answers

Equivalent of PHP's dirname(__FILE__) in JavaScript?

as I already mentioned in the title, I'm looking for a JS-function for getting the same result like I get with this PHP code: dirname(dirname(__FILE__)) Thanks in advance!
Mr. B.
  • 8,041
  • 14
  • 67
  • 117
3
votes
1 answer

PowerShell equivalent of “dirname $0” from bash

I've used dirname "$0" which is an idiom to determine the path of the running script in bash, e.g.: pushd "$(dirname "$0")" data_dir="$(dirname "$0")/data/" What's PowerShell equivalent of the above idiom?
minhee
  • 5,688
  • 5
  • 43
  • 81
1
2 3
9 10