Try doing this (using bash, brace expansion & globs):
rm -f {a..z}/*.pdf
or
rm -f [a-z]/*.pdf
if your shell lack the brace expansion feature.
Contrary to [a-z]
, {a..z}
(also supported by ksh93) is not a glob
, it's brace expansion, it's expanded (before globs
) regardless of whether files exist or not. That's like rm -f a/*.pdf b/*.pdf
..., regardless of whether a, b... exist or not. Also note that contrary to [a-z]
where the range may be locale dependant (like may include é, ś
...), {a..z}
only works with byte ranges (and reliably only in the ASCII
letter ranges, and number ranges)
(Merci Stephane Chazelas for explanations)
In BASH you can use the trailing slash (I think it should work in any POSIX shell):
rm -R -- */
Note the --
which separates options from arguments and allows one to remove entries starting with a hyphen - otherwise after expansion by the shell the entry name would be interpreted as an option by rm
(the same holds for many other command line utilities).
Add the -f
option if you don't want to be prompted for confirmation when deleting non-writeable files.
Note that by default, hidden directories (those whose name starts with .
) will be left alone.
An important caveat: the expansion of */
will also include symlinks that eventually resolve to files of type directory. And depending on the rm
implementation, rm -R -- thelink/
will either just delete the symlink, or (in most of them) delete the content of the linked directory recursively but not that directory itself nor the symlink.
If using zsh
, a better approach would be to use a glob qualifier to select files of type directory only:
rm -R -- *(/) # or *(D/) to include hidden ones
or:
rm -R -- *(-/)
to include symlinks to directories (but because, this time, the expansion doesn't have trailing /
s, it's the symlink only that is removed with all rm
implementations).
With bash
, AT&T ksh
, yash
or zsh
you can do:
set -- */
rm -R -- "${@%/}"
to strip the trailing /
.
Best Answer
Yes, changing the working directory back and forth is cumbersome and not really what you would like to do as it can lead to extremely weird situations in more complex scripts, unless you are careful.
The usual method for changing the working directory for a simple command is to put the
cd
and to invocation of the command in a sub-shell. The working directory will be changed for the sub-shell but the change is not carried over to the rest of the script as the sub-shell is executing in its own environment.Example: Executing
mycommand
inside all directories in the current working directory:or in your case, with known directories
a
andb
:I don't know the
dsmmigrate
tool, so I can't say whether running it this way is right or not.EDIT: It turns out that the
dsmmigrate
tool has a-Recursive
flag: