Home > Net >  mkdir with ascending sequential suffix and mv one file into each folder [duplicate]
mkdir with ascending sequential suffix and mv one file into each folder [duplicate]

Time:09-21

ls -1 | sort -V > txt_list

and

cat txt_list

gives

ASOFJ.txt
BJWIF.txt
CWORI.txt
...

I want to mkdir with suffix in ascending order like folder_1, folder_2, folder_3

and mv ASOFJ.txt folder_1 , mv BJWIF.txt folder_2, mv CWORI.txt folder_3

I have tried

num=0 ; while read p ; do ( num=$(expr $num   1) && mkdir "folder_"$num && mv $p "folder_"$num ) ; done < txt_list

But it didn't work.

Do you have any idea on a one-liner that executes the above?

Thanks.

CodePudding user response:

let c=0; for i in $('ls'); do c=$((c 1)); mkdir -p folder_$c; mv $i folder_$c; done

This should do it, I think.

Explanation:

  • define variable c
  • go over each element in ls
    • add 1 to c
    • create folder with suffix c
    • move element to folder

CodePudding user response:

#!/bin/bash

counter=1
while read line
do
    mkdir "folder_${counter}" && mv $line "folder_${counter}"
    ((counter  ))
done <<< $(ls -1 | sort -V)
  • Related