How to create folders using file names and then move files into folders?
It's not necessary to use trim or xargs:
for f in *.txt; do band=${f% - *} mkdir -p "$band" mv "$f" "$band"done
with Perl
use File::Copy move;while (my $file= <*.txt> ){ my ($band,$others) = split /\s+-\s+/ ,$file ; mkdir $band; move($file, $band);}
gregseth's answer will work, just replace trim
with xargs
. You could also eliminate the if
test by just using mkdir -p
, for example:
for f in *.txt; do band=$(echo "$f" | cut -d'-' -f1 | xargs) mkdir -p "$band" mv "$f" "$band"done
Strictly speaking the trim
or xargs
shouldn't even be necessary, but xargs
will at least remove any extra formatting, so it doesn't hurt.