Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

0 votes
3 answers
68 views
Does a command exist that lists all the directories where a word appears in a file or directory name?
When I don't remember where a file or a folder is, I sometime use the `locate` command (that finds more occurrences, allow more candidates than a `find`, in my mind. But maybe I'm mistaking). But then there's a lot of responses, of course: ```bash locate clang ``` ```log /data/sauvegardes/dev/Java/E...
When I don't remember where a file or a folder is, I sometime use the locate command (that finds more occurrences, allow more candidates than a find, in my mind. But maybe I'm mistaking). But then there's a lot of responses, of course:
locate clang
/data/sauvegardes/dev/Java/Experimentations/Angular4/bikes/node_modules/blocking-proxy/.clang-format
/data/sauvegardes/dev/Java/Experimentations/Angular4/bikes/node_modules/node-gyp/gyp/tools/Xcode/Specifications/gyp.xclangspec
/data/sauvegardes/dev/Java/Experimentations/Angular6/ng6-proj/node_modules/blocking-proxy/.clang-format
/data/sauvegardes/dev/Java/Experimentations/Angular6/ng6-proj/node_modules/node-gyp/gyp/tools/Xcode/Specifications/gyp.xclangspec
/data/sauvegardes/dev/Java/Experimentations/blog-demo/node/node_modules/npm/node_modules/node-gyp/gyp/tools/Xcode/Specifications/gyp.xclangspec
/data/sauvegardes/dev/Java/Experimentations/blog-demo/node_modules/blocking-proxy/.clang-format
/data/sauvegardes/dev/Java/Experimentations/blog-demo/node_modules/node-gyp/gyp/tools/Xcode/Specifications/gyp.xclangspec
/data/sauvegardes/dev/Java/Experimentations/ol-ext-angular/.metadata/.plugins/ts.eclipse.ide.server.nodejs.embed.win32.win32.x86_64/node-v6.9.4-win-x64/node_modules/npm/node_modules/node-gyp/gyp/tools/Xcode/Specifications/gyp.xclangspec

(201 responses)
I piped this command, with dirname, sort and uniq to list only directories having such word in their name or carrying one or more file having it.
locate clang | xargs -L1 dirname | sort | uniq
it works...
/home/lebihan/dev/Java/comptes-france/metier-et-gestion/AdapterInboundWebEtude/etude/node_modules/node-gyp/gyp/tools/Xcode/Specifications
/home/lebihan/dev/Java/comptes-france/metier-et-gestion/AdapterInboundWebEtude/etude/node/node_modules/npm/node_modules/node-gyp/gyp/tools/Xcode/Specifications
/usr/include/boost/align/detail
/usr/include/boost/config/compiler
/usr/include/boost/predef/compiler
/usr/lib/linux-kbuild-6.1/scripts
/usr/lib/llvm-14/lib
/usr/lib/postgresql/14/lib/bitcode/postgres/commands
/usr/lib/x86_64-linux-gnu
/usr/local/go/misc/ios
/usr/local/go/src/debug/dwarf/testdata
/usr/local/go/src/debug/elf/testdata
/usr/local/go/src/debug/macho/testdata
/usr/share/doc
/usr/share/doc/libclang1-14
/usr/share/doc/libclang-cpp14

(108 responses)
But does _Linux_ have a command doing the same, more easily?
Marc Le Bihan (2353 rep)
Oct 31, 2023, 07:34 AM • Last activity: Oct 31, 2023, 10:20 AM
1 votes
1 answers
258 views
Why is this script treating the files with the same names as the same files?
``` #!/usr/bin/bash install_wm() { echo "$(dirname "$0")" cd "$(dirname "$0")" && pwd mkdir -p /root/.config && cd /root/.config && git clone https://git.suckless.org/dwm && cd dwm && pwd && diff "$(dirname "$0")/config.def.h" /root/.config/dwm cp -f "$(dirname "$0")/config.def.h" /root/.config/dwm...
#!/usr/bin/bash

install_wm() {
    echo "$(dirname "$0")"
    cd "$(dirname "$0")" && pwd
    mkdir -p /root/.config && cd /root/.config &&
    git clone https://git.suckless.org/dwm  && cd dwm && pwd &&
    diff "$(dirname "$0")/config.def.h" /root/.config/dwm
    cp -f "$(dirname "$0")/config.def.h" /root/.config/dwm &&
}

install_wm
When I run this script as root I get
.
/home/jim/CS/SoftwareDevelopment/MySoftware/Bash/ubuntu-server-LTS
Cloning into 'dwm'...
remote: Enumerating objects: 6504, done.
remote: Counting objects: 100% (6504/6504), done.
remote: Compressing objects: 100% (3216/3216), done.
remote: Total 6504 (delta 3733), reused 5933 (delta 3287), pack-reused 0
Receiving objects: 100% (6504/6504), 6.18 MiB | 8.86 MiB/s, done.
Resolving deltas: 100% (3733/3733), done.
/root/.config/dwm
cp: './config.def.h' and '/root/.config/dwm/config.def.h' are the same file
I run this script as root. $(dirname "$0")/config.def.h is my config file that has different content, and is located in the same directory as the script, than the one in the cloned repo. Why do I get from cp './config.def.h' and '/root/.config/dwm/config.def.h' are the same file if the files only have the same names and not the content? Also when I run diff on the two files manually, outside of the script, I get the output that shows the difference between them:
22,23c22,23
 static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
> 
30,34c30,31
 	{ "Gimp",     NULL,       NULL,       0,            1,           -1 },
> 	{ "Firefox",  NULL,       NULL,       1  /* commands */
66,71c61
 static const char *termcmd[]  = { "st", NULL };
whereas I don't get any output when diff is run from within my script. What is happening here?
John Smith (827 rep)
Oct 12, 2023, 08:18 AM • Last activity: Oct 12, 2023, 08:34 AM
0 votes
2 answers
784 views
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 contain valid hyperlinks. To test this: Create a subdi...
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 contain valid hyperlinks. To test this: Create a subdirectory ./s1 Create a text file ./s1/s1.txt Contents of ./s1/s1.txt: www.google.com This is the one-liner: find . -type f -name "*.txt" -exec bash -cx "wget -i \"{}\" -P $(dirname \"{}\") " \; The problem is that $(dirname \"{}\") does not expand correctly. The bash command being excuted is: + wget -i ./s1/s1.txt -P . So $(dirname \"{}\") returns . The effect is that a new **directory** ./s1/s1.txt is created. So the downloaded file is stored as ./s1/s1.txt/index.html When I replace $(dirname \"{}\") with $(echo \"{}\") the output becomes: + wget -i ./s1/s1.txt -P ./s1/s1.txt So parameter passing as such is correct. So I assume the result dirname is not properly returned to the calling bash shell. Or dirname is not evaluated at all. When I execute only the bash command bash -cx "wget -i ./s1/s1.txt -P $(dirname ./s1/s1.txt)" (so outside the find command) the command is executed as expected: + wget -i ./s1/s1.txt -P ./s1 What is the correct way to make this one-liner work?
Johannes Linkels (169 rep)
Apr 16, 2023, 12:11 PM • Last activity: Apr 16, 2023, 03:15 PM
7 votes
6 answers
3449 views
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 pipe in multiple paths, and get unique result...
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 pipe in multiple paths, and get unique results back, e.g.:
toolimlookingfor << EOF
dir1/file1
dir1/file2
foo/bar/moocow
EOF
Output:
dir1
dir1/file1
dir1/file2
foo
foo/bar
foo/bar/moocow
dirname is close to what I'm looking for, but it only gives the immediate parent. I'm looking for the path it's self and all parents.
Gary van der Merwe (1830 rep)
Aug 1, 2020, 09:09 AM • Last activity: Feb 12, 2022, 03:55 PM
0 votes
2 answers
247 views
Map over file tree, preserving structure
I have a file tree that looks like: ``` $ tree src src ├── bible │&#160;&#160; ├── index.md │&#160;&#160; └── 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 should be rooted at...
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 should be rooted at out, like so:
$ tree out
out
├── bible
│   ├── index.html
│   └── README.html
├── index.html
└── other.html
Ideally, I'd like to do this via make(1). This is what I have so far:
SRC_DIR=src
OUT_DIR=out

.PHONY: all
all: $(SRC_DIR)/*.md
	find . -name "*.md" -exec pandoc '{}' -o '{}'.html \;
	find -name "*.html" -exec bash -c 'mv {} $(OUT_DIR)/dirname {}/basename {} .md.html.html' \;
	# mv $(SRC_DIR)/*.html $(OUT_DIR)
	firefox $(OUT_DIR)/index.html

.PHONY: clean
clean:
	rm $(OUT_DIR)/*.html
This fails:
$ make
find . -name "*.md" -exec pandoc '{}' -o '{}'.html \;
find . -name "*.html" -exec bash -c 'mv {} basename {} .md.html.html' \;
mv src/*.html out
mv: cannot stat 'src/*.html': No such file or directory
make: *** [Makefile:8: all] Error 1
jmcph4 (75 rep)
Jul 16, 2021, 03:28 AM • Last activity: Jul 16, 2021, 04:19 PM
1 votes
3 answers
2406 views
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 split it two steps: copying and changin...
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 split it two steps: copying and changing the filename, but I would like to learn how to evaluate an expression with an argument from xargs.
Change-the-world (111 rep)
Feb 12, 2019, 07:24 AM • Last activity: Mar 19, 2021, 12:23 AM
1 votes
2 answers
1651 views
Apply readlink to a non-symlink file?
This code [from Stack Overflow][156458] 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: [156458]://stackoverflow.com/a/920834/156458 if [ -L $0 ] ; then DIR=$(dirname $(readlink -f $0)) ; else DIR=$(dirname $0) ; fi ; Is th...
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 ; Is the above code equivalent to DIR=$(dirname $(readlink -f $0)) ; i.e. is there really a need to test if $0 is a symlink, and run different commands if yes and if no? Why not just use $(dirname $(readlink -f $0)) regardless of whether $0 contains symlink or not? Specifically, if $0 contains symlink, then it is perfect to use $(dirname $(readlink -f $0)). if $0 is not a symlink, then it seems perfect to me to just use $(dirname $(readlink -f $0)): - $(readlink -f $0) returns the absolute path of $0, and $(dirname $(readlink -f $0)) returns the absolute path to the parent directory of $0 - $(dirname $0) returns a path to the parent of $0, whether the return is an absolute or relative path depending on if $0 is an absolute or relative path. - I don't think whether the value of DIR is an absolute or relative path is important in the original code, because in either case, the value of DIR refers to the parent directory of $0. So are $(dirname $(readlink -f $0)) and $(dirname $0) equivalent in the original code? Thanks.
Tim (106420 rep)
Mar 21, 2018, 04:35 AM • Last activity: Jan 23, 2021, 10:12 AM
-2 votes
1 answers
2259 views
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' is: ./pkg_test.go ./v3/organization/organization_test.go ./v3/common/pkg_test.go ./v3/model/boil_main_test.go ./v3/model/boil_queries_test.go ./v3/model/adv...
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' is: ./pkg_test.go ./v3/organization/organization_test.go ./v3/common/pkg_test.go ./v3/model/boil_main_test.go ./v3/model/boil_queries_test.go ./v3/model/advances_test.go ./v3/model/boil_suites_test.go ./v3/model/psql_main_test.go ./v3/model/psql_suites_test.go anyone know how I can get the dirname of the file? Maybe I need absolute paths?
Alexander Mills (10734 rep)
Jan 20, 2020, 09:18 PM • Last activity: Jan 20, 2020, 09:51 PM
9 votes
2 answers
5341 views
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 baz/index.js | xargs dirname usage: dirname...
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 baz/index.js | xargs dirname usage: dirname path I would expect foo bar baz as output. What am I missing? I'm on Darwin 18.2.0 (macOS 10.14.3).
davidchambers (193 rep)
Mar 2, 2019, 10:39 AM • Last activity: Mar 2, 2019, 01:25 PM
1 votes
2 answers
138 views
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?
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 (10734 rep)
Oct 19, 2018, 06:52 AM • Last activity: Oct 23, 2018, 01:14 AM
1 votes
1 answers
2117 views
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 path to relative path, relative to git root git diff HEAD:"$f...
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 path to relative path, relative to git root git diff HEAD:"$file" remotes/origin/dev:"$file" If I pass an absolute path to this script, it needs to be able to handle it. What is the canonical way to do that? To check if it's an absolute file path, do we just check if the first character is "/"?
Alexander Mills (10734 rep)
Sep 27, 2018, 07:53 PM • Last activity: Sep 27, 2018, 08:06 PM
0 votes
2 answers
453 views
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 \.gif >> "$img_fil" locate -i image | grep \.tif >...
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 \.gif >> "$img_fil" locate -i image | grep \.tif >> "$img_fil" locate -i image | grep \.png >> "$img_fil" But when I start processing the dump file for this, most of the paths contain blanks so this does not work: while read -r fline do if [ ! -e "$fline" ]; then echo "F=> $fline" mv "$fline" "$img_dir" else fpath="$(dirname $fline)" fname="$(basename $fline)" echo "F=> $fname P=> $fpath" fi done The dirname and basename always parse at the blanks so will not process right. How do I get this to work?
OldManRiver (1 rep)
Feb 25, 2018, 09:17 PM • Last activity: Feb 25, 2018, 09:46 PM
0 votes
1 answers
72 views
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 found $ readlink /usr/local/bin/sdkmanager /u...
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 found $ readlink /usr/local/bin/sdkmanager /usr/local/Caskroom/android-sdk/3859397,26.0.1/tools/bin/sdkmanager How can I set $ANDROID_HOME from this value? So that I have $ANDROID_HOME=/usr/local/Caskroom/android-sdk/3859397,26.0.1 ? $ dirname readlink /usr/local/bin/sdkmanager /usr/local/Caskroom/android-sdk/3859397,26.0.1/tools/bin Now how can I append ../..?
Jin Kwon (564 rep)
Sep 14, 2017, 03:40 AM • Last activity: Sep 17, 2017, 11:59 AM
0 votes
3 answers
1708 views
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 like so would be ideal: cdx 3 && since `cd` has no id...
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 like so would be ideal: cdx 3 && since cd has no idea that 3 is not a file or directory.
Alexander Mills (10734 rep)
Jul 17, 2017, 07:23 PM • Last activity: Jul 19, 2017, 05:49 AM
Showing page 1 of 14 total questions