linux Move files to folders with same name

December 25, 2010 at 01:20:02
Specs: Linux, intel

I have a list of files 000, 005, 010, 015, etc and would like to make folders with the same names
000/, 005/ , etc. Then I want to move each file to the folder with the same name.
Ex: 000 -> 000/, 005 -> 005/ etc..

I would like to write a script to make the folders based on the filenames and then perform the move operation of each file to the respective folder(with same name).

Hope some of you can help me to do this.

With thanks and regards
arun


See More: linux Move files to folders with same name

Report •

#1
December 25, 2010 at 23:49:34
If you're running Linux, you can't use a batch file (as far as I know). However:

FOR /f "tokens=*" %%a IN ('DIR /b "*.txt"') DO (
	MD "%%~na"
	MOVE "%%a" "%%~na"
)

Can I put a question here?


Report •

#2
December 25, 2010 at 23:58:04

>Can I put a question here?

Sure... you may post your question.


Report •

#3
December 26, 2010 at 00:15:29
Sigh, nobody ever gets my joke... I don;t have a question, it's a signature thing.

Did you try the script?

Can I put a question here?


Report •

Related Solutions

#4
December 26, 2010 at 07:15:48
"Sigh, nobody ever gets my joke..."

Dont worry, I do :)

Happy is ONE who says I am an OttoMAN.


Report •

#5
December 26, 2010 at 22:27:55
If you are running Linux, then this bash script might work. Execute it in the directory where your files reside. Obviously, the directory can not exist in the same directory as your files if you want the destination directory to have the same name. In this case, I choose the destination directory to be /tmp:

#!/bin/bash

fdest="/tmp"
ls -1|while read file
do
   filedest="$fdest/$file"
   mkdir $filedest 2> /dev/null
   cp $file $filedest
done


Report •

#6
December 27, 2010 at 22:22:47

Hi,

Thank you very much... it works pretty well :-)

arun


Report •

#7
December 27, 2010 at 22:35:54
Nice.. Now I liked your way of writing signature.., hope u will have a look at the reply by nails.

Report •

Ask Question