Questions tagged [dirname]

12 questions
9
votes
2 answers

dirname appears not to work with xargs

xargs and basename work together as I would expect: $ printf '%s\n' foo/index.js bar/index.js baz/index.js | xargs basename index.js index.js index.js xargs and dirname, though, appear not to work together: $ printf '%s\n' foo/index.js bar/index.js…
davidchambers
  • 193
  • 1
  • 5
2
votes
6 answers

What can I use to get the paths of all parent directories for a given path

I'm looking for a tool that I can use from a bash script that can give me the list of parent directories for a given path. e.g. given the input foo/bar/moocow I would like to get out: foo foo/bar foo/bar/moocow It would also be great if I could…
Gary van der Merwe
  • 1,630
  • 2
  • 14
  • 28
1
vote
3 answers

How can I use an argument from xargs to evaluate another expression?

I would like to evaluate a basename expression given an argument from xargs. I tested: find . -name '*.txt' | xargs -I f cp f DIR_OUT/copied_$(basename f) which gives no file or directory because $(basename f) was not evaluated correctly. I may…
1
vote
2 answers

Get container directory more easily

I have a bash script here: $GOPATH/ src/ build.sh and in build.sh I have: export GOPATH="$(cd $(dirname "$BASH_SOURCE") && pwd)" is there a shorter way to get the containing dir of build.sh?
Alexander Mills
  • 9,330
  • 19
  • 95
  • 180
1
vote
1 answer

Using bash, resolve absolute path to relative path

Say I have this simple bash script: #!/usr/bin/env bash file="$1"; if [ -z "$file" ]; then echo "Must pass relative file path as the first argument."; fi git_root=`git rev-parse --show-toplevel`; # => need to resolve file from an absolute…
Alexander Mills
  • 9,330
  • 19
  • 95
  • 180
1
vote
2 answers

Apply readlink to a non-symlink file?

This code from Stack Overflow is to get the pathname to the parent directory of a script inside the script, even if the script is run via a symlink to it: if [ -L $0 ] ; then DIR=$(dirname $(readlink -f $0)) ; else DIR=$(dirname $0) ; fi…
Tim
  • 98,580
  • 191
  • 570
  • 977
0
votes
2 answers

Problem using dirname in a subshell

I am writing a (one-line) script which should recurse thru subdirectories. Find .txt files containing hyperlinks. Use wget to get the contents and download it in the same directory where the text file is located. Assume all text files found only…
0
votes
2 answers

Map over file tree, preserving structure

I have a file tree that looks like: $ tree src src ├── bible │   ├── index.md │   └── README.md ├── index.md └── other.md I want to render every Markdown file within this file tree to HTML via pandoc(1) -- preserving structure. This new file tree…
jmcph4
  • 75
  • 6
0
votes
2 answers

Bash for file processing

I am writing a bash script to move all images into a central file. I create the list of image files with: img_fil='/home/files/img_dump.txt' locate -i image | grep \.jpg > "$img_fil" locate -i image | grep \.jpeg >> "$img_fil" locate -i image | grep…
0
votes
1 answer

How can I set ENV from a path

I just installed android-sdk using Homebrew. The path is /usr/local/Caskroom/android-sdk/3859397,26.0.1. I know I can export ANDROID_HOME with that value. How can I set it dynamically using the real path of a command such as `sdkmanager'? So far, I…
Jin Kwon
  • 480
  • 1
  • 5
  • 14
0
votes
3 answers

cd 3 times to great-grandparent dir

I have this: cd $(dirname $(dirname $(dirname "$0"))) && which will cd to project root. Is there shorthand for this somehow, where I can just be like: cd 3 && # not quite, but you get the idea or whatever, I mean why not you know? Maybe a command…
Alexander Mills
  • 9,330
  • 19
  • 95
  • 180
-2
votes
1 answer

How to get dirname from path

I have this: find . -type f -name '*_test.go' | xargs dirname but I get: usage: dirname path the output of $ find . -type f -name '*_test.go'…
Alexander Mills
  • 9,330
  • 19
  • 95
  • 180