Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double quotes vs asterisk filename expansion in Bash

In the directories ~/temp/a/foo/ and ~/temp/b/foo foo/ I have some files named bar1, bar2, bar bar1, bar bar2, etc.

I am trying to write a line of Bash that copies all these files in a directory containing "foo" as last part of the name to the folder above the respective "foo" folder.

As long as there are no spaces in the file names, this is an easy task, but the two following commands fail when dealing with the foo foo directory:

for dir in `find . -type d -name '*foo'` ; do cp $dir/* "$(echo $dir|sed 's_foo__g')" ; done

(The cp command fails to see the last foo of "foo foo" as part of the same directory name.)

for dir in `find . -type d -name '*foo'` ; do cp "$dir/*" "$(echo $dir|sed 's_foo__g')" ; done

("$dir/*" is not expanded.)

Attempts like replacing $dir/* with "$(echo $dir/*)" have been even less successful.

Is there an easy way to expand $dir/* so that cp understands?

like image 792
uvett Avatar asked Mar 16 '13 23:03

uvett


2 Answers

Not only is a for loop wrong -- sed is also not the right tool for this job.

while IFS= read -r -d '' dir; do
  cp "$dir" "${dir/foo/}"
done < <(find . -type d -name '*foo' -print0)

Using -print0 on the find (and IFS= read -r -d '') ensures that filenames with newlines won't mess you up.

Using the < <(...) construct ensures that if the inside of your loop sets variables, changes directory state, or does similar things, those changes will survive (the right-hand side of a pipeline is in a subshell in bash, so piping into a while loop would mean that any changes to the shell's state made inside that while loop would be discarded on its exit otherwise).

Using ${dir/foo/} is vastly more efficient than invoking sed, as it does the string replacement internal to bash.

like image 54
Charles Duffy Avatar answered Oct 11 '22 13:10

Charles Duffy


The problem here is not with cp, but with for, because by default it splits the output of your subshell by words, not by directory names.

A lazy workaround is to use while instead and process the list of directories line by line like this:

find . -type d -name '*foo' | while read dir; do cp "$dir"/* "$(echo $dir | sed 's_foo__g')" ; done

This should fix your problem with spaces, but this is by no means a foolproof solution.

See Charles Duffy's answer for a more accurate solution.

like image 24
janos Avatar answered Oct 11 '22 13:10

janos