0

I am beginner especially in working with Unix shell.

Is there a way to do following:

  1. for each file (around 50) in directory make a subdirectory. Subdirectory should ideally be named as file or as number (from 1-50)

  2. Move files to corresponding subdirectories

  3. Starting from main directory, for each subdirectory, use file in that subdirectory for further analysis (perform code on it, already written)

I will really appreciate your help. Thanks a lot.

2
  • You can not have a file and directory with the same name in the same location. But I would always do this over 2 places: the directory where your files are and a destination on the same level as where you do this.(so a script, a dir_toprocess/ and a dir_results/. – Rinzwind Oct 27 '20 at 17:16
  • - toss the names into a file ls > file.txt, move file.txt to the dir_results and use xargs -d '\n' mkdir -p -- < file.txt to create directories. That covers the creation of the dirs. :) – Rinzwind Oct 27 '20 at 17:19
0

Here's how, handling funny filenames:

#!/bin/bash 
files="$(\
    find . -maxdepth 1 -type f -print0 | \
        xargs -0 -r stat --format=\"%N\" \
 )"
for i in $files ; do
  mv "$i" "X_$i" && \
    mkdir "$i" && \
    mv "X_$i" "$i/$i"
done

find . -type f -print0 | \
        xargs -0 -r stat --format=\"%N\"

Read man find xargs stat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.