Skip to main content
added 55 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242
cd /path/to/dir
(   set -- *\ *
    printf 'd="%s" ; mkdir ./"${d%d%%.doc}"\n' "$@"
) | . /dev/stdin

You can set a subshell's parameters with shell globs and then feed a pipe with printf, and . source the pipe as a script.

And if you want to move the files in each directory:

cd /path/to/dir
(   set -- *\ *
    printf 'f="%s" ; d="${f%%.doc}"
        mkdir ./"$d" ; echo "mv ./\"$f\" ./\"$d\"/\"$f\""\n' "$@"
) | . /dev/stdin

Note: I've intentionally hamstrung the above with echo because I want you test the output before you dive-in.

FIXED - I forgot to use two %percents for printf.

I tested this, by the way:

% printf 'touch ./"%s file.doc"\n' first second third fourth fifth |
    . /dev/stdin
% ls
> fifth file.doc  first file.doc  fourth file.doc  second file.doc  third file.doc

% ( set -- *\ *
    printf 'f="%s" ; d="${f%%.doc}"
    mkdir ./"$d" ; mv ./"$f" ./"$d"\n' "$@"
) | . /dev/stdin

% ls
> fifth file  first file  fourth file  second file  third file

% cd fifth\ file ; ls
> fifth file.doc
cd /path/to/dir
(   set -- *\ *
    printf 'd="%s" ; mkdir ./"${d%.doc}"\n' "$@"
) | . /dev/stdin

You can set a subshell's parameters with shell globs and then feed a pipe with printf, and . source the pipe as a script.

And if you want to move the files in each directory:

cd /path/to/dir
(   set -- *\ *
    printf 'f="%s" ; d="${f%%.doc}"
        mkdir ./"$d" ; echo "mv ./\"$f\" ./\"$d\"/\"$f\""\n' "$@"
) | . /dev/stdin

Note: I've intentionally hamstrung the above with echo because I want you test the output before you dive-in.

FIXED - I forgot to use two %percents for printf.

cd /path/to/dir
(   set -- *\ *
    printf 'd="%s" ; mkdir ./"${d%%.doc}"\n' "$@"
) | . /dev/stdin

You can set a subshell's parameters with shell globs and then feed a pipe with printf, and . source the pipe as a script.

And if you want to move the files in each directory:

cd /path/to/dir
(   set -- *\ *
    printf 'f="%s" ; d="${f%%.doc}"
        mkdir ./"$d" ; echo "mv ./\"$f\" ./\"$d\"/\"$f\""\n' "$@"
) | . /dev/stdin

Note: I've intentionally hamstrung the above with echo because I want you test the output before you dive-in.

FIXED - I forgot to use two %percents for printf.

I tested this, by the way:

% printf 'touch ./"%s file.doc"\n' first second third fourth fifth |
    . /dev/stdin
% ls
> fifth file.doc  first file.doc  fourth file.doc  second file.doc  third file.doc

% ( set -- *\ *
    printf 'f="%s" ; d="${f%%.doc}"
    mkdir ./"$d" ; mv ./"$f" ./"$d"\n' "$@"
) | . /dev/stdin

% ls
> fifth file  first file  fourth file  second file  third file

% cd fifth\ file ; ls
> fifth file.doc
added 55 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242
cd /path/to/dir
(   set -- *\ *
    printf 'd="%s" ; mkdir ./"${d%.doc}"\n' "$@"
) | . /dev/stdin

You can set a subshell's parameters with shell globs and then feed a pipe with printf, and . source the pipe as a script.

And if you want to move the files in each directory:

cd /path/to/dir
(   set -- *\ *
    printf 'f="%s" ; d="${f%f%%.doc}"
        mkdir ./"$d" ; echo "mv ./\"$f\" ./\"$d\"/\"$f\""\n' "$@"
) | . /dev/stdin

Note: I've intentionally hamstrung the above with echo because I want you test the output before you dive-in.

FIXED - I forgot to use two %percents for printf.

cd /path/to/dir
(   set -- *\ *
    printf 'd="%s" ; mkdir ./"${d%.doc}"\n' "$@"
) | . /dev/stdin

You can set a subshell's parameters with shell globs and then feed a pipe with printf, and . source the pipe as a script.

And if you want to move the files in each directory:

cd /path/to/dir
(   set -- *\ *
    printf 'f="%s" ; d="${f%.doc}"
        mkdir ./"$d" ; echo "mv ./\"$f\" ./\"$d\"/\"$f\""\n' "$@"
) | . /dev/stdin

Note: I've intentionally hamstrung the above with echo because I want you test the output before you dive-in.

cd /path/to/dir
(   set -- *\ *
    printf 'd="%s" ; mkdir ./"${d%.doc}"\n' "$@"
) | . /dev/stdin

You can set a subshell's parameters with shell globs and then feed a pipe with printf, and . source the pipe as a script.

And if you want to move the files in each directory:

cd /path/to/dir
(   set -- *\ *
    printf 'f="%s" ; d="${f%%.doc}"
        mkdir ./"$d" ; echo "mv ./\"$f\" ./\"$d\"/\"$f\""\n' "$@"
) | . /dev/stdin

Note: I've intentionally hamstrung the above with echo because I want you test the output before you dive-in.

FIXED - I forgot to use two %percents for printf.

added 218 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242
cd /path/to/dir
(   set -- *\ *
    printf 'd="%s" ; mkdir ./"${d%.doc}"\n' "$@"
) | . /dev/stdin

You can set a subshell's parameters with shell globs and then feed a pipe with printf, and . source the pipe as a script.

And if you want to move the files in each directory:

cd /path/to/dir
(   set -- *\ *
    printf 'f="%s" ; d="${f%.doc}"
        mkdir ./"$d" ; echo "mv ./\"$f\" ./\"$d\"/\"$f\""\n' "$@"
) | . /dev/stdin

Note: I've intentionally hamstrung the above with echo because I want you test the output before you dive-in.

cd /path/to/dir
(   set -- *\ *
    printf 'd="%s" ; mkdir ./"${d%.doc}"\n' "$@"
) | . /dev/stdin

You can set a subshell's parameters with shell globs and then feed a pipe with printf, and . source the pipe as a script.

cd /path/to/dir
(   set -- *\ *
    printf 'd="%s" ; mkdir ./"${d%.doc}"\n' "$@"
) | . /dev/stdin

You can set a subshell's parameters with shell globs and then feed a pipe with printf, and . source the pipe as a script.

And if you want to move the files in each directory:

cd /path/to/dir
(   set -- *\ *
    printf 'f="%s" ; d="${f%.doc}"
        mkdir ./"$d" ; echo "mv ./\"$f\" ./\"$d\"/\"$f\""\n' "$@"
) | . /dev/stdin

Note: I've intentionally hamstrung the above with echo because I want you test the output before you dive-in.

Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242
Loading