Cris
Cris

Reputation: 796

Moves "n" files, each to a specific folder with command line or bash in ubuntu

I have multiple files in a folder. Every file has different names and have different extensions. I need a way to move each of the files into folders called Archive_1, Archive_2, Archive_n, and so on. It doesn't matter the order of the file but I need one file per folder.

I was looking for something like sorting the files by name and based on that move the first one to Archive_1, and then the second one to Archive_2, etc. Couldn't find it.

Any help?

Upvotes: 1

Views: 51

Answers (1)

Rany Albeg Wein
Rany Albeg Wein

Reputation: 3474

Assuming all files are in PWD, you can execute:

i=0
for f in ./*; do
    new_dir=Movie_$((++i))
    mkdir -p "$new_dir"
    mv "$f" "$new_dir"
done

Test ( I created a script called sof with the above command ):

$ touch a b c
$ ./sof
$ tree
.
├── Movie_1
│   └── a
├── Movie_2
│   └── b
└── Movie_3
    └── c

3 directories, 3 files

Upvotes: 1

Related Questions