Questions tagged [dirname]

115 questions
303
votes
15 answers

How do you properly determine the current script directory?

I would like to see what is the best way to determine the current script directory in Python. I discovered that, due to the many ways of calling Python code, it is hard to find a good solution. Here are some problems: __file__ is not defined if the…
bogdan
  • 7,114
  • 10
  • 35
  • 42
247
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
  • 3,233
  • 4
  • 17
  • 21
85
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,040
  • 1
  • 7
  • 7
83
votes
9 answers

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

I have a folder structure as follows: mydomain.com ->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 the…
aVC
  • 2,056
  • 2
  • 19
  • 44
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,075
  • 7
  • 40
  • 58
18
votes
6 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…
user2966991
  • 1,011
  • 2
  • 12
  • 27
12
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,477
  • 1
  • 16
  • 26
12
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
  • 48,876
  • 29
  • 126
  • 149
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
  • 7
7
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
  • 16,401
  • 19
  • 81
  • 129
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
  • 186
  • 12
7
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
  • 11,951
  • 16
  • 49
  • 98
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
  • 1,810
  • 3
  • 16
  • 38
3
votes
2 answers

Unix C - Compiling for 64 bit breaks "dirname"

I'm using dirname from libgen.h to get the directory path from a filename's path. This is it's signature: char * dirname (char *path) When compiling on a 32 bit machine or using -m32 with gcc, it all works fine. My code looks like this: char* path…
jonathanpeppers
  • 24,837
  • 21
  • 94
  • 178
3
votes
2 answers

Going up two or three folders using dirname twice or three times

Take a look at the code $link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; echo dirname(dirname($link)); Question 1. Is it elegant to use dirname twice to go two levels up? Question 2. In case you want to go three levels up, would it be a…
Julian
  • 3,177
  • 2
  • 29
  • 43
1
2 3 4 5 6 7 8