I'm trying to find/extract directories based on an expression that applies to the file path. For example, I used the command:
find . -type d -links 2
To get a list of directories that looks something like this:
./foo/ABC/W
./foo/ABC/X
./foo/ABC/Y
./foo/ABC/Z
./foo/BCD/W
./foo/BCD/X
./foo/BCD/Y
./foo/BCD/Z
./foo/CDE/W
./foo/CDE/X
./foo/CDE/Y
./foo/CDE/Z
./bar/CDE/V
./bar/CDE/Q
./bar/BCD/V
./bar/BCD/Q
./bar/ABC/V
./bar/ABC/Q
I'm wondering how to take this list, and extract only filepaths containing say, "CDE" in them:
./foo/CDE/W
./foo/CDE/X
./foo/CDE/Y
./foo/CDE/Z
./bar/CDE/V
./bar/CDE/Q
This is what I've tried so far, which outputs nothing when I try it:
find . -type d -links 2 -name "CDE" -print
-links 2
), I'm passing this to -exec to copy files into each subdirectory. – bactro Jul 21 '20 at 19:02-print
is the default action, but I wouldn’t call it redundant. Leaving it off will sometimes cause a command to fail; seefind
with multiple-name
and-exec
executes only the last matches of-name
, Operator precedence in a find command?, find: Why is the-a
operator not commutative in combination with-print
?, and others. So don’t just forget that-print
exists; you may need it sometimes. … (Cont’d) – G-Man Says 'Reinstate Monica' Jul 22 '20 at 00:49-type d
will find all directories at all levels, including subdirectories. As mentioned by Quasímodo, on a proper Unix system, using a standard Unixy filesystem,-type d -links 2
will find only directories *that have no subdirectories.* This does not work on Cygwin, and might not work on WSL (Windows Subsystem for Linux), Ubuntu on Windows, Unix/Linux using a FAT disk or a file server like NetApp, etc. – G-Man Says 'Reinstate Monica' Jul 22 '20 at 00:49