Reputation: 547
Suppose there is a directory with many sub-directories aaa
, bbb
, ccc
...
In one (and only one!) of these directories is a file called x.txt
(but I don't know in which of the directories).
I wanted to move that file to an alternative directory using:
mv */x.txt {target_dir}
However this doesn't work: No such file or directory
As a solution I ended up looping over all sub-directories and checking if the file is there with [ -f ]
, and moving the file once located.
However, I was wondering if there is a simpler solution?
Upvotes: 1
Views: 662
Reputation: 3623
From the bash manpage:
globstar
If set, the pattern ** used in a pathname expansion context will match
all files and zero or more directories and subdirectories. If the
pattern is followed by a /, only directories and subdirectories match.
You could try enabling the 'globstar' option, and using
shopt -s globstar
echo **/x.txt
If the echo finds the file, so will the equivalent
mv **/x.txt {target_dir}
Note: globstar is a bash only option (added in bash 4.0) If you use an older release (such as the bash 3 which is standard on the MAC) this will not work.
Upvotes: 1