LinuxQuestions.org
Help answer threads with 0 replies.
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-11-2012, 05:11 PM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 305

Rep: Reputation: Disabled
creating directories and moving into them


Hi guys,

I have bunches and bunches of "special binary tar like" files that I need to "unpack" they are all located in one parent directory.

Because the data files inside each could have the same names, I want to create a directory with their specific name and then move them into their own directories and then unpack them.

I/m tinking something like

Code:
 
for
file in 'ls *.packed' ;
do mkdir  (however I pass the filename to the mkdir)
mv filename into directory
done
hold on guys, I think I might know more than just this. I just have to run, I'm late to meet some of the girls for diner

Tabitha

Last edited by atjurhs; 09-11-2012 at 05:22 PM.
 
Old 09-11-2012, 05:21 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Code:
#!/bin/bash
# never parse ls output in scripts
for filename in *.packed
do
  mkdir $filename.dir
  # mkdir $filename wouldn't work, since a file with this name already exists
  mv $filename $filename.dir
done

Last edited by TobiSGD; 09-13-2012 at 05:46 AM. Reason: forgot shebang
 
Old 09-11-2012, 07:01 PM   #3
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
Quote:
Originally Posted by TobiSGD View Post
[code]
# never parse ls output in scripts
hmm, are you saying that the code below is not to be done?

Quote:
for file in $(ls -l /folder)
or you mean he should have done

Quote:
'ls *.packed >/dev/null'
maybe I am missing understanding the use of the word parse,please explain.

Thanks

Last edited by cbtshare; 09-11-2012 at 07:02 PM.
 
Old 09-11-2012, 07:18 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Look here for a good explanation why you should not use the output of ls (parsing is a different word for processing, more or less) in Bash scripts: http://mywiki.wooledge.org/ParsingLs
 
Old 09-12-2012, 10:48 AM   #5
atjurhs
Member
 
Registered: Aug 2012
Posts: 305

Original Poster
Rep: Reputation: Disabled
Thanks Tobi!!!

When I run your script I get directories named

ABC.packed.dir
DEF.packed.dir
GHI.packed.dir

etc...

I would like them to have more of the usual naming convention

ABC
DEF
GHI

etc...

so I was thinking that I could first run your script and then add on to it:

Code:
for filename in *.packed
do
  mkdir $filename.dir
  mv $filename $filename.dir
done

for dirname in *.packed.dir
do
    rename $dirname.packed.dir $dirname *dirname.packed.dir
done
thinking I could use a second for/do loop that starts up after all the directories are created and the files are moved inside them, but as you know (and I don't) that doesn't work

I searcehed on the web and theres lots of posts about how to rename files, but not directories

I think I can simply add a second for/do loop after another and get it to run because I did
Code:
for dirname in *.packed.dir
do
    echo 1
done
and a series of 1 was printed to the screen.

I'm self taught and so slow learning, so I greatly appreciate your help and your teaching!

Tabitha

Last edited by atjurhs; 09-12-2012 at 11:31 AM.
 
Old 09-12-2012, 09:16 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,342

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
Try
Code:
for dir in *.packed.dir
do
    new_dir=$(echo ${dir%.packed.dir})
    mv $dir $new_dir
done
http://tldp.org/LDP/abs/html/string-manipulation.html
 
Old 09-12-2012, 09:21 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by chrism01 View Post
Try
Code:
for dir in *.packed.dir
do
    new_dir=$(echo ${dir%.packed.dir})
    mv $dir $new_dir
done
http://tldp.org/LDP/abs/html/string-manipulation.html
Why the echo instead of just
Code:
new_dir=${dir%.packed.dir}
?
 
Old 09-12-2012, 09:24 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,342

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
I started by testing raw on the cmd line with echo and forgot to remove it later...
 
Old 09-12-2012, 09:34 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
lol, just checking. I was curious if there was a special reason for it.
 
  


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
moving files to different directories congos Linux - Newbie 1 12-11-2011 01:20 AM
[SOLVED] Moving files and directories except few moyorakkhi Linux - Newbie 2 10-25-2011 06:49 AM
Moving directories to new directories keysorsoze Linux - Newbie 2 03-31-2006 07:14 PM
moving directories with FTP illiniguy3043 Linux - Software 2 07-08-2004 12:37 AM
Moving directories to their own partitions bxb32001 Debian 7 12-08-2003 06:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:36 PM.

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
Open Source Consulting | Domain Registration