Search code examples
bashsedmkdir

mkdir with sed in bash script


I want to create directories which names should correspond to a list of zipped files in the parent directory. Additionally I want to get rid of the file extension in the resulting name of the directory.

e.g. archive01.gz should result in a directory with name archive01

My script so far:

#!/bin/bash
for file in *.gz; do
    echo $file | sed 's/.gz//' | mkdir
done

The error message is:

mkdir: missing operand

However,

echo $file | sed 's/.gz//'

results in the correct name for the directory. How do I pipe it to mkdir?


Solution

  • A better way to do this would be to use parameter substitution instead of a pipe/subshell:

    #!/bin/bash
    for file in *.gz; do
        mkdir ${file%.gz}
    done