Artificial intelligent assistant

Dispatching files to directories using name pattern matching Consider: $ ls about-bar about-bar.pdf about-foo about-fou.pdf $ for f in *about-*.pdf; do mv "$f" "$(echo $f | sed -E 's:(about-.*).pdf:\1:')";done $ find . . ./about-bar ./about-bar/about-bar.pdf ./about-foo ./about-fou What would be an efficient way to skip files that don't have a corresponding directory? In this case, only `about-bar.pdf` would be moved, and `about-fou.pdf` wouldn't be renamed (specifically, `.pdf` stripped). Thank you.

No `sed` needed here; we can use standard `bash` expression of `${file%.pdf}` to strip off the `.pdf` extension


$ ls
about-bar/ about-bar.pdf about-foo/ about-fou.pdf

$ for a in *.pdf
> do
> dir=${a%.pdf}
> if [ -d "$dir" ]
> then
> mv -i "$a" "$dir"
> fi
> done

$ ls
about-bar/ about-foo/ about-fou.pdf

$ ls *
about-fou.pdf

about-bar:
about-bar.pdf

about-foo:

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy c4f4b8125e890ef7fe49ccb00db10ae4