LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2013, 12:19 PM   #1
nouse
LQ Newbie
 
Registered: Sep 2013
Posts: 21

Rep: Reputation: Disabled
Moving files to corresponding folders


Hi there,

i have the following problem. I have many files in a folder, called 1......200 (the same extension, some bioinformatic stuff).
I would like to do the following:

For every file named i, mkdir a folder called i, and move that file into that folder.
I tried different For loops in a sh-executable, but i am always getting the message "cant create folder, file already exists".

Any ideas?
 
Old 09-16-2013, 12:23 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
You can't make a directory the same name as the file in the same location as the file, because the file already has that name. However, you can make a temporary directory, move the file into it, then rename the directory to what you want. Something like:

Code:
for i in *; do
   if [[ ! -d "$i" ]]; then
      mkdir temp
      mv "$i" temp/
      mv temp "$i"
   fi
done
 
Old 09-16-2013, 12:37 PM   #3
nouse
LQ Newbie
 
Registered: Sep 2013
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks, you explained my troubles then. In Linux, a folder cant have the same name as a file at the same level? I wasnt aware.
Your script worked wonders, thank you very much.

Last edited by nouse; 09-16-2013 at 12:42 PM.
 
Old 09-16-2013, 12:50 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,061

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by nouse View Post
In Linux, a folder cant have the same name as a file at the same level? I wasnt aware.
This is also true in Windows. Perhaps it can be less obvious because the graphical file browser hides file extensions by default, so a file can look like it has the same name even though it doesn't.
 
Old 09-16-2013, 12:50 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
You can't have a file and folder with the same name in Windows either, or in OSX as far as I know. It's a pretty universal restriction.

edit: too slow

Last edited by suicidaleggroll; 09-16-2013 at 12:52 PM.
 
Old 09-16-2013, 12:54 PM   #6
nouse
LQ Newbie
 
Registered: Sep 2013
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
This is also true in Windows. Perhaps it can be less obvious because the graphical file browser hides file extensions by default, so a file can look like it has the same name even though it doesn't.
That was my obvious misconception.

Would it be possible then to circumvent the temporary folder by naming the folders according to the filename without extension?
Like move 1.txt to 1 etc.
 
Old 09-16-2013, 01:13 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
Sure. There are several ways of dynamically generating the extension-less version of the name, the one to use depends on what your extensions look like. For example, if it's always .txt, or always a 3 letter extension, or the extension always follows the first period in the filename, or the extension always follows the last period in the filename, etc. Linux doesn't need file extensions anyway, so the file might not have one, in which case you need to decide what to do.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting through folders and moving files jdepp Linux - Newbie 2 07-22-2013 02:27 PM
Moving files between Folders in Remote directory Saratha Linux - Newbie 5 03-29-2013 03:37 PM
[SOLVED] Moving files from folders and subfolders to a specific folder mrj2 Linux - Newbie 12 08-22-2010 01:40 PM
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 05:35 PM
moving files and folders thru command line JROCK1980 Linux - General 2 02-16-2004 07:55 PM


All times are GMT -5. The time now is 03:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration