The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find and replace string in a directory files koti_rama Shell Programming and Scripting 2 05-30-2008 03:48 AM
Find and replace a string in multiple files pharos467 UNIX for Dummies Questions & Answers 2 11-05-2007 11:47 PM
replace character in a string pattern and save the change in same file mihir0011 Shell Programming and Scripting 2 09-26-2007 05:31 PM
replace first character of string sed prkfriryce Shell Programming and Scripting 6 08-03-2007 02:07 PM
how to find and replace string mridula High Level Programming 3 08-17-2006 04:44 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-10-2008
callimaco0082 callimaco0082 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 8
Find and replace character in a string

Hi all,

My problem is the following:

I've a script that must list all files in a directory and write this information in a text file.

I've tried to get the list through ls command and then write it using msgecho

msgecho "`ls $PATH_APS_JOB_ORA`"

This works good but the created string is very long, having the line with all files in the list separated by a blank space.

Is it possible to store the ls results in a string variable, replace the space with \n and then write it into the text file?
In this way, the text file will be more and more clear...

I've read about sed and grep commands: is it possible to apply them to a string? How?

Thanks to all will help.
  #2 (permalink)  
Old 04-10-2008
saikiransvs saikiransvs is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 2
Use the below command to write the list of files to file.

find /Required_Directory/ -maxdepth 1 -type f -exec basename {} \; > file

  #3 (permalink)  
Old 04-10-2008
callimaco0082 callimaco0082 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 8
maxdepth doesn't work

Hi,

I've tried the solution you propose but it says that maxdepth is not a valid option for find command...

Furthermore, what basename means?

Thanks!
  #4 (permalink)  
Old 04-10-2008
saikiransvs saikiransvs is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 2
Hi,

If the maxdepth option does not work for find on your OS, use the below command

find /Required_Directory/ -type f -exec basename {} \; > file

But this will give you the files recurrsively present inside directories inside the actual directory.

If you need files only in the required directory,use the below command.

ls -l /Required_Directory/ | grep "^-" | tr -s " " | cut -d" " -f8 > file

check if the 8th field is filename, if not change the field number.

For your clarification, "basename" command will give you strips the directory path and gives only the filename.
  #5 (permalink)  
Old 04-10-2008
callimaco0082 callimaco0082 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 8
Question answered

Thank you so much!

Now I'm using the following sintax

Code:
ls -l $PATH_APS_JOB_ORA | grep "^-" | tr -s " " > $PATH_APS_LOG_ORA/file_list.log
  #6 (permalink)  
Old 04-10-2008
callimaco0082 callimaco0082 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 8
A new question!!!

Hi,

a new problem was born.

the code you suggested works very good but now I'm trying to list in the text file more than one file list.
If I repeat the operation two times, calling the ls command on two different folders, the text file is overwritten and the first data are lost...
How to write in append mode on the text file?
In alternative, is it possible to specify two or more different folders in ls command?

Thanks in advance!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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




All times are GMT -4. The time now is 02:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0