LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-16-2004, 06:29 AM   #1
rblampain
Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 7
Posts: 833

Rep: Reputation: 35
bash script - variables & arrays question


I need to extract, rename and move a few files from a cascade of directories and subdirectories. Some names of files are known, others have to be found with the script.

It seems at some stage of the script, I need to save a directory name in a variable or a number of directory names in an array, then 'cut' a part of the name(s) and 'paste' it in following commands in order to rename and move the files to be saved to the top directory before removing everything else.

Found good tutorials on the net but no solution to this particular problem. Any help on what to include in script or hint about suitable 'howto' most welcome.

System is Red Hat 9
Here is the layout:
(all files relative to /home/user/job01/)
01 ~ no1/ -->keep
02 z_files/
03 important.html -->save as "no1/main.html"
04 a_dat/
05 important.gif -->save as 'no1/main.gif"
06 001_files/
07 sub_files/
08 some_da/
09 important.html -->save as "no1/001.html"
10 some_dat/
11 main.png -->save as "no1/001.png"
12 002_files
13 etc
14 100_files
15 ~no2/
16 etc
17 ~no300/

directory named 'no1' (on line 01) number about 300, all these names are known and are placed in an array

files to be saved on lines 03, 05, 09 and 11 are unique and easy to identify and process

directory named 001_files (on line 06) are unknown names but have a common ending (_files), the number of directories is also variable, the variable part of the name (001 in this case) will be used to rename the files to be saved

it is possible to issue the command "cd *_files from the ~no1/ directory which will enter any of these files, then save the first digits of the name ('001' in the example above) which can then be 'pasted' to the files to be saved from this directory, then remove this directory, then re-issue the same command that will enter the next random "*_files" directory and so on untill there is no more "_files" directory.

How to use and parse the portion of the name is what I don't know.
 
Old 09-16-2004, 09:54 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
You might find this useful:
name_start=`echo 001_files|cut -d'_' -f1`
$name_start now has '001'

as you can see, this will give you the leading digits/string. Note that's backquotes '`' around the whole cmd in the first line, not single quotes.
You then say something like:
cd 001_files
cp thisfile1 /newdir/${name_start}_thisfile1

HTH
 
Old 09-22-2004, 01:28 AM   #3
rblampain
Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 7
Posts: 833

Original Poster
Rep: Reputation: 35
thanks to chrism01

Shortly after posting my question I had the script running using the wildcard option. It works well but it leaves a feeling
of uncertainty. I tried your elegant suggestion but I found my understanding of bash is far too little to adapt it safely. I also found I will need to use bash more than anticipated initially and therefore I will get a good book to help solve the problems as they come. Thank you again.
 
Old 09-22-2004, 08:08 AM   #4
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,653

Rep: Reputation: Disabled
For some 500+ pages of bash scripting look here: http://www.tldp.org/guides.html#abs
 
Old 09-25-2004, 09:57 AM   #5
rblampain
Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 7
Posts: 833

Original Poster
Rep: Reputation: 35
Thanks to JZL240I-U also.
I've downloaded the stuff and it looks like I'll find a lot of answers in that.
I should pay a bit more attention to the LDP.
Cheers
 
  


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
bash script help (arrays and strings from files) nkoplm Programming 14 12-02-2005 09:50 AM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
BASH script Ė reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM
Using Bash Script for Exporting and Returning Environmental Variables Jicksta Programming 3 12-04-2004 04:14 PM
bash script variables twantrd Programming 7 11-17-2004 02:38 AM


All times are GMT -5. The time now is 10:58 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration