LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 06:11 PM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 168

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 06:22 PM.
 
Old 09-11-2012, 06:21 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
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 06:46 AM. Reason: forgot shebang
 
Old 09-11-2012, 08:01 PM   #3
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

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 08:02 PM.
 
Old 09-11-2012, 08:18 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
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, 11:48 AM   #5
atjurhs
Member
 
Registered: Aug 2012
Posts: 168

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 12:31 PM.
 
Old 09-12-2012, 10:16 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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, 10:21 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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, 10:24 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

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

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


All times are GMT -5. The time now is 10:42 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