Search code examples
bashmkdirls

mkdir from result of ls


I want to list all folders from /media/movies and create folders with the same name in /tmp/movies
The following snippet works fine for me except for names with spaces, not sure how to quote here

mkdir /tmp/movies
cd /tmp/movies/
ls -1 -d /media/share/*/ | xargs mkdir

In the names are also parentheses ( and ) and single qutes '

Example

You're Next (2011) (tt1853739)


Solution

  • cd /media/movies
    for dir in */
    do
      mkdir "/tmp/movies/$dir"
    done
    

    Or on one line:

    cd /media/movies && for dir in */; do mkdir "/tmp/movies/$dir"; done