dirname

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

半腔热情 提交于 2019-12-01 15:12:52
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 folder like the example above? "dirname $PATH" just returns "/home/user/example/foo/". Thanks in advance! :) I've found a solution: #/usr/bin/env bash DIRECTORY="/home/user/example/foo/bar" BASE_DIRECTORY=$(echo "$DIRECTORY" | cut -d "/" -f2) echo "#$BASE_DIRECTORY#"; This returns always the first directory. In this example it would return following: #home# Thanks to

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

孤街浪徒 提交于 2019-12-01 13:58:42
问题 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 folder like the example above? "dirname $PATH" just returns "/home/user/example/foo/". Thanks in advance! :) 回答1: I've found a solution: #/usr/bin/env bash DIRECTORY="/home/user/example/foo/bar" BASE_DIRECTORY=$(echo "$DIRECTORY" | cut -d "/" -f2) echo "#$BASE_DIRECTORY#";

How can I determine the current directory name in R?

二次信任 提交于 2019-12-01 02:16:55
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 portable?) Your example code doesn't work for me, but you're probably looking for either basename or dirname : > getwd() [1] "C:/cvswork/data" > basename(getwd()) [1] "data" > dirname(getwd()) [1] "C:/cvswork" If you didn't know basename (and I didn't), you could have used this: tail(strsplit(getwd(), "/")[[1]], 1) 来源: https://stackoverflow.com/questions

Getting the parent of a directory in Bash

杀马特。学长 韩版系。学妹 提交于 2019-11-30 10:08:44
问题 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/ 回答1: dir=/home/smith/Desktop/Test parentdir="$(dirname "$dir")" Works if there is a trailing slash, too. 回答2: ...but what is "seen here" is broken. Here's the fix: > pwd /home/me > x='Om Namah Shivaya' > mkdir "$x" && cd "$x" /home/me/Om Namah Shivaya > parentdir="$(dirname "$(pwd)")" > echo $parentdir

Getting the parent of a directory in Bash

寵の児 提交于 2019-11-29 18:50:45
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/ dir=/home/smith/Desktop/Test parentdir="$(dirname "$dir")" Works if there is a trailing slash, too. ...but what is "seen here " is broken. Here's the fix: > pwd /home/me > x='Om Namah Shivaya' > mkdir "$x" && cd "$x" /home/me/Om Namah Shivaya > parentdir="$(dirname "$(pwd)")" > echo $parentdir /home/me Clearly the parent directory is given by simply appending the dot-dot filename: /home/smith/Desktop

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

孤街浪徒 提交于 2019-11-28 03:32:39
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 filename and I get mydomain.com/Folder-A . Inside this script, I need to get a string that says 'mydomain.com/Folder-B/image1.jpg . I tried $path=dirname(__FILE__).'/'.'../Folder-B/image1.jpg'; This shows up as mydomain.com%2FFolder-A%2F..%2FFolder-B%2Fimage1.jpg This is for a facebook share button, and this fails to fetch the correct image. Anyone know

How to get the last part of dirname in Bash

て烟熏妆下的殇ゞ 提交于 2019-11-27 18:55:31
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? You can use basename even though it's not a file. Strip off the file name using dirname , then use basename to get the last element of the string: dir="/from/here/to/there.txt" dir="$(dirname $dir)" # Returns "/from/hear/to" dir="$(basename $dir)" # Returns just "to" Using bash string functions: $ s="/from/here/to/there.txt" $ s="${s%/*}" && echo "${s##*/}" to The opposite of dirname is basename : basename "$(dirname "/from/here/to/there.txt")" Pure

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

两盒软妹~` 提交于 2019-11-27 05:07:47
问题 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 filename and I get mydomain.com/Folder-A . Inside this script, I need to get a string that says 'mydomain.com/Folder-B/image1.jpg . I tried $path=dirname(__FILE__).'/'.'../Folder-B/image1.jpg'; This shows up as mydomain.com%2FFolder-A%2F..%2FFolder-B

How to get the last part of dirname in Bash

无人久伴 提交于 2019-11-27 04:20:19
问题 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? 回答1: You can use basename even though it's not a file. Strip off the file name using dirname , then use basename to get the last element of the string: dir="/from/here/to/there.txt" dir="$(dirname $dir)" # Returns "/from/here/to" dir="$(basename $dir)" # Returns just "to" 回答2: The opposite of dirname is basename : basename "$(dirname "/from/here/to

How to properly determine current script directory?

风格不统一 提交于 2019-11-25 22:35:40
问题 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 script is executed with exec , execfile __module__ is defined only in modules Use cases: ./myfile.py python myfile.py ./somedir/myfile.py python somedir/myfile.py execfile(\'myfile.py\') (from another script, that can be located in another