LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2008, 12:17 AM   #1
Cyberman
Member
 
Registered: Aug 2005
Distribution: Debian Stable
Posts: 218

Rep: Reputation: 17
I can't move my files to folders using metacharacters (mv ./.blah ../?[0-9]/)


So, I have these folders:

../listdata/
../listdata/.scripts/
../listdata/.scripts/.subscripts/

inside of ../listdata/ are a bunch of folders going up to 100

example:
../listdata/1/
../listdata/2/
../listdata/9/
../listdata/25/
../listdata/100/

Anyway, the way I had it, I used the mv command and had all 100 folders listed (100+ lines of code). I mean, this was the only way I could do it without lots of scripting knowledge. My knowledge has increased since then, but I guess not enough. I figured using metacharacters and wildcards in the proper way would simplify things, but I haven't been able to get anything to work.

so, i tried doing this now

the .movescripts folder is inside of ../listdata/.scripts/

$ bash ./.movescripts.sh/

Code:
#!/bin/sh
mv ./.subscripts/.script1.sh ./.subscripts/.script2.sh ./.subscripts/.script3.sh ./.subscripts/.script4.sh ../listdata/[0-9]/
mv ./.subscripts/.script1.sh ./.subscripts/.script2.sh ./.subscripts/.script3.sh ./.subscripts/.script4.sh ../listdata/?[0-9]/
mv ./.subscripts/.script1.sh ./.subscripts/.script2.sh ./.subscripts/.script3.sh ./.subscripts/.script4.sh ../listdata/??[0-9]/
each is suppose to be representative of each 0-9, 10-99, and 100-999 folders. But it doesn't work. I don't know why. Matter of fact, it likes to add files to folder "98," which doesn't make sense to me. I don't know what to do, really. I tried googling this, and I can't figure it out.
 
Old 06-29-2008, 06:21 AM   #2
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Not real sure what you are trying to do? I think you are trying to move the files based on the number in the filename or maybe how many digits are in the filename. So this may help you extract the number from the script name:

Code:
$ ls
script1.sh    script135.sh  script171.sh  script29.sh  script65.sh
script101.sh  script137.sh  script173.sh  script3.sh   script67.sh
script103.sh  script139.sh  script175.sh  script31.sh  script69.sh
script105.sh  script141.sh  script177.sh  script33.sh  script7.sh
script107.sh  script143.sh  script179.sh  script35.sh  script71.sh
script109.sh  script145.sh  script181.sh  script37.sh  script73.sh
script11.sh   script147.sh  script183.sh  script39.sh  script75.sh
script111.sh  script149.sh  script185.sh  script41.sh  script77.sh
script113.sh  script15.sh   script187.sh  script43.sh  script79.sh
script115.sh  script151.sh  script189.sh  script45.sh  script81.sh
script117.sh  script153.sh  script19.sh   script47.sh  script83.sh
script119.sh  script155.sh  script191.sh  script49.sh  script85.sh
script121.sh  script157.sh  script193.sh  script5.sh   script87.sh
script123.sh  script159.sh  script195.sh  script51.sh  script89.sh
script125.sh  script161.sh  script197.sh  script53.sh  script9.sh
script127.sh  script163.sh  script199.sh  script55.sh  script91.sh
script129.sh  script165.sh  script21.sh   script57.sh  script93.sh
script13.sh   script167.sh  script23.sh   script59.sh  script95.sh
script131.sh  script169.sh  script25.sh   script61.sh  script97.sh
script133.sh  script17.sh   script27.sh   script63.sh  script99.sh

$ for file in *.sh;do temp=${file/#script/};num=${temp/%.sh/};echo "$num";done
1
101
103
105
107
109
11
111
113
115
...
...
89
9
91
93
95
97
99

Now if you wanted to know how many digits were in the number you would do this:

<EDIT> Maybe this isn't so clear
$ for file in *.sh;do temp=${file/#script/};num=${temp/%.sh/};echo "$file has ${#num} digits";done
script1.sh has 1 digits
script101.sh has 3 digits
script103.sh has 3 digits
script105.sh has 3 digits
script107.sh has 3 digits
script109.sh has 3 digits
script11.sh has 2 digits
script111.sh has 3 digits
script113.sh has 3 digits
script115.sh has 3 digits
...
...
script89.sh has 2 digits
script9.sh has 1 digits
script91.sh has 2 digits
script93.sh has 2 digits
script95.sh has 2 digits
script97.sh has 2 digits
script99.sh has 2 digits
temp=${file/#script/} #<- This strips the string "script" from the front of the string.
num=${temp/%.sh/} #<- This strips the string ".sh" from the end of the string.

Of course in the above you would replace the echo with a mv command.

Last edited by /bin/bash; 06-29-2008 at 12:53 PM.
 
Old 06-29-2008, 08:31 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
The syntax for mv is:
mv <source> <destination>

I can't relate this to the command structure you are using.

How about a simple loop?
 
Old 06-29-2008, 07:01 PM   #4
Cyberman
Member
 
Registered: Aug 2005
Distribution: Debian Stable
Posts: 218

Original Poster
Rep: Reputation: 17
imagine this:

Code:
ubuntu@ubuntu:~/Desktop/listdata$ ls
1  100  15  2  5  66  77  9
ubuntu@ubuntu:~/Desktop/listdata$ ls -a
.  ..  1  100  15  2  5  66  77  9  .scripts
ubuntu@ubuntu:~/Desktop/listdata$ cd ./.scripts/
ubuntu@ubuntu:~/Desktop/listdata/.scripts$ ls -a
.  ..  movefiles.sh  .subscripts
ubuntu@ubuntu:~/Desktop/listdata/.scripts$ cd ./.subscripts/
ubuntu@ubuntu:~/Desktop/listdata/.scripts/.subscripts$ ls -a
.  ..  .capitalize.sh  .disguise.sh  .noterize.sh  .organize.sh
ubuntu@ubuntu:~/Desktop/listdata/.scripts/.subscripts$ cd ..
ubuntu@ubuntu:~/Desktop/listdata/.scripts$ bash ./movefiles.sh
When I execute ./movefiles.sh it will move all the files from ./.subscripts/ into the folders, which there are 100 of them (in reality), that are located in ./listdata/

so, all the files in ./.subscripts would be moved into the numbered folders that exist in ./listdata

Thank you for the replies so far.

Last edited by Cyberman; 06-29-2008 at 07:06 PM.
 
Old 06-29-2008, 08:33 PM   #5
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
If you started in listdata directory and _if_ all the numbered directories are sequential then this would work:
for dir in $(seq 1 100);do cd "$dir";cp -a ../.scripts/.subscripts/.?* ./;cd ../;done
 
Old 06-29-2008, 08:34 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
It looks like you've got 100 numbered dirs in the listdata dir, and 'some' scripts (not numbered) in the (hidden) dir .scripts/.subscripts.
(incidentally notarize has an 'a' not an 'e')
It seems that you want 100 copies of the scripts, 1 set in each numbered dir.

Here's simplified version that works for me:
Code:
for file in `ls tmp`
do
    for num in `seq 24`
    do
        cp tmp/$file /home/chris/${num}/$file
    done
done
In your case, subst subdirs for my tmp, and run script from listdata dir. Subst listdata for /home/chris
 
  


Reply



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
Can't move files/folders to trash... adred Linux - General 1 06-22-2008 05:40 AM
Alternative to such good Flashfxp that can move the files/folders into the ftp ? frenchn00b Linux - Software 2 06-08-2008 10:45 PM
move files in tar.gz back to into original folders GUIPenguin Linux - General 1 05-09-2005 12:11 PM
newbie issues....kernel panic: blah blah blah Rio Nishida Slackware 16 11-29-2002 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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