LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   creating directories and moving into them (https://www.linuxquestions.org/questions/linux-newbie-8/creating-directories-and-moving-into-them-4175426761/)

atjurhs 09-11-2012 05:11 PM

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

TobiSGD 09-11-2012 05:21 PM

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


cbtshare 09-11-2012 07:01 PM

Quote:

Originally Posted by TobiSGD (Post 4777987)
[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

TobiSGD 09-11-2012 07:18 PM

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

atjurhs 09-12-2012 10:48 AM

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

chrism01 09-12-2012 09:16 PM

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

suicidaleggroll 09-12-2012 09:21 PM

Quote:

Originally Posted by chrism01 (Post 4779008)
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}
?

chrism01 09-12-2012 09:24 PM

I started by testing raw on the cmd line with echo and forgot to remove it later... :)

suicidaleggroll 09-12-2012 09:34 PM

lol, just checking. I was curious if there was a special reason for it.


All times are GMT -5. The time now is 08:00 PM.