nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Need help on moving files and something else

This is a discussion on Need help on moving files and something else within the Shell scripting forums, part of the Development/Scripting category; I need some files to be copied to a different folder. But I have to setup a script which downloads ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 08-18-2006, 11:30 PM
Junior Member
User
 
Join Date: Jun 2006
Posts: 21
Rep Power: 0
karabaja
Default Need help on moving files and something else

I need some files to be copied to a different folder. But I have to setup a script which downloads a file called file.zip but the problem is that this file is extracted every time to a folder with different name. I think this file is updated weekly and and then folder in which files are extracted is for example
file01082006 then next time I download the file and extract it folder would be named file08082005.

Now is there a way for me to copy all files in that folder to another location. It contains like 50 txt files. Can I somehow use a wildcard like file* or something. Or can I specify names of text files to be copied and can they be found even if I don't specify exact folder name?
I need content of this folder, these text files moved to another location since I want to include them in a web page. So I don't need the folder iteslf copied.
Maybe if there is a way to copy all files from specified folder and subfolders to another folder. So I could extract this zip to for example /root/folder/file01082006/*.txt and if possible copy all files from "folder" to a different location.

And another thing I could use a help with is, is there some command or more of them I could use to insert two words at the very beginning and the end of those files. Like to insert "start" and "end" in each file.

Thank you in advance, I hope my question is understandable. I am a beginner and English is not my native language.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-19-2006, 05:43 AM
Junior Member
User
 
Join Date: Jun 2006
Posts: 21
Rep Power: 0
karabaja
Default

I noticed they all have same word at the top of the file so I could use sed to replace it with what I need. But I still need a way to insert the other one at the end. I need text wrapped in <pre> tags. So I could replace first word in document with <pre> but I still need a way to insert </pre> at the end. And the way to copy them without specifing exact path.
Reply With Quote
  #3 (permalink)  
Old 08-19-2006, 06:13 AM
Junior Member
User
 
Join Date: Jun 2006
Posts: 21
Rep Power: 0
karabaja
Default

Ok, I am narrowing it down

I found out that I could use unzip -j to so that folder structure is not recreated.

Now I need a way to insert </pre> at the end of each file.
Reply With Quote
  #4 (permalink)  
Old 08-19-2006, 08:10 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Quote:
Now I need a way to insert </pre> at the end of each file.
You can use following command:
Code:
echo '</pre>' >> myfile.txt
Be careful use >> and not > (single)
Reply With Quote
  #5 (permalink)  
Old 08-19-2006, 08:13 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Quote:
And another thing I could use a help with is, is there some command or more of them I could use to insert two words at the very beginning and the end of those files. Like to insert "start" and "end" in each file.
Let us assume that your file name is filename.txt, then
Code:
echo 'word' >/tmp/head.txt
echo 'word' >/tmp/tail.txt
cp head.txt filename.txt tail.txt > /tmp/final.txt
cp /tmp/final.txt filename.txt
rm  -f /tmp/head.txt /tmp/tail.txt /tmp/final.txt
Hope this helps
Reply With Quote
  #6 (permalink)  
Old 08-19-2006, 08:55 PM
Junior Member
User
 
Join Date: Jun 2006
Posts: 21
Rep Power: 0
karabaja
Default

Thanks monk.

This line:
Code:
cp head.txt filename.txt tail.txt > /tmp/final.txt
gave me an error that destination is not a directory. But I can manage using combination of sed to replace first word in all documents with <pre>
and use echo to add </pre> at the end.

It would help if I could use wildcards to cover all files at once, but I don't think that is possible. I guess I can use wildcard for sed input but I can't for output. And I tried using wild card with echo but it gave me:

Code:
ambiguous redirect
But this is still very helpfull, thank you. I'll specify all file names in a script.
Reply With Quote
  #7 (permalink)  
Old 08-20-2006, 12:07 AM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

karabaja, i am sorry it should be cat and not the cp i.e. line should be:
Code:
cat head.txt filename.txt tail.txt > /tmp/final.txt
You can process all files using for loop for example *.txt in in /data directory. So something as follows should work out:

Code:
files="ls /data/*.txt"
echo '<pre>' > /tmp/head
echo '</pre>' > /tmp/tail

for i in $files
do
  out="/tmp/out.$$"
  cat /tmp/head $i /tmp/tail > $out
  cp $out $i
  rm -f $out
done

rm -f /tmp/head /tmp/tail
To be frank I am not sure how to use sed, above solution should work out. Or you may have to wait till vivek or someone else post the solution using sed.

Try out and lemme know
Reply With Quote
  #8 (permalink)  
Old 08-20-2006, 08:02 PM
Junior Member
User
 
Join Date: Jun 2006
Posts: 21
Rep Power: 0
karabaja
Default

Thx but I've already written a script with your first echo suggestion. Although I did it the hard way but as long as it is done and working now.
I had to write like 150 lines for echo command to insert in </pre> in each file and then 150 lines with sed command to replace first word with <pre> and to output result to destination folder when I need them
But it did just what I need, that's what matters, thx.

P.S. That last code works fine. I just wish I've waited some more for before doing it the hard way. But never mind, I needed that first word removed anyway. I'd still have to use sed or something else to remove it.
Reply With Quote
  #9 (permalink)  
Old 08-21-2006, 03:48 AM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
My distro: Suse, RHEL, Vista
Posts: 154
Rep Power: 4
sweta will become famous soon enough
Default

You can use grep to remove exact word, for example remove word <pre>:
Code:
egrep -wv '<pre>' input.txt > output.txt
Only remove <pre> from first line using sed:
Code:
sed -e '1s/<pre>//' input.txt > output.txt
Remove all <pre> from files:
Code:
sed -e 's/<pre>//g' input.txt > output.txt
__________________
Friends - v-nessa - missyAdmin - LinuxChix
Reply With Quote
  #10 (permalink)  
Old 08-23-2006, 05:53 AM
Junior Member
User
 
Join Date: Jun 2006
Posts: 21
Rep Power: 0
karabaja
Default

Thx guys for all the help.
Now I am just curious even though I don't need this at the moment, but would like to learn.

How would reverse process look like.
Fore example to grep content of the file.txt but only output what is between "start" and "end" in that file, not the whole content.
And what would happen if "start" and "end" would appear more then once in the document.

Sorry to keep bugging you guys but when I try to find out more about some command on the net all I get is basic usage.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell script for automatic conversion of files in tar files kasimani Shell scripting 2 02-08-2007 04:45 PM
moving files based on size kavi Shell scripting 2 11-11-2005 06:17 PM


All times are GMT +5.5. The time now is 06:57 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36