echo ls to a file and then read file and selectively delete


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting echo ls to a file and then read file and selectively delete
# 1  
Old 03-22-2011
echo ls to a file and then read file and selectively delete

I'm trying to write a script that will do an ls of a location, echo it into a file, and then read that file and selectively delete files/folders, so it would go something like this:

cd $CLEAN_LOCN
ls >>$TMP_FILE
while read LINE
do
if LINE = $DONTDELETE
skip
elseif LINE = $DONTDELETE2
skip
elseif LINE = $DONTDELETE3
skip
elseif
rm -rf $LINE
done <$TMP_FILE

My problem is that any variation of ls that I try echoes as one line into the $TMP_FILE and reads as one line. So I'm looking for a method to split the ls with linefeeds so it displays that way and reads that way in the $TMP_FILE or a method to write it with , separators and use that to split the lines in the While read statement.
I've tried:
ls -1
ls -m
And:
while IFS=, read LINE
but they all read as 1 line. Thus far, I'm just trying to echo the LINE in the while loop to get the correct syntax.

The location has a set # of folders that are known but I am trying to archive everything and delete some of those folders along with any other junk someone might throw in that location.

---------- Post updated at 01:35 PM ---------- Previous update was at 01:26 PM ----------

solved it differently avoiding temp file:

for LINE in `ls`; do
echo $LINE
done
# 2  
Old 03-22-2011
Close, pipelining kills latency:
Code:
ls | while read LINE
do
echo "$LINE"
done
 
But you can pipe the whole task:
 
ls | grep -Ev "^($DONTDELETE|$DONTDELETE2|$DONTDELETE3)$" | xargs -r rm -rf


Last edited by DGPickett; 03-24-2011 at 10:25 AM.. Reason: double quote variables against embedded white space/meta char!
# 3  
Old 03-22-2011
Quote:
Originally Posted by MaureenT
solved it differently avoiding temp file:

for LINE in `ls`; do

Not only is ls unnecessary, it will break your script if any filenames contain whitespace or other pathological characters. Use:
Code:
for line in *; do

Quote:
echo $LINE

That will remove leading or trailing spaces and reduce multiple spaces to a single space. Use:
Code:
echo "$LINE"

Or, preferably:
Code:
printf "%s\n" "$LINE"

If all you want to do is print the filenames, you don't need a loop:
Code:
printf "%s\n" *

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove spaces from a file selectively?

Hi i have a file in which i am doing some processing. The code is as follows: #!/bin/ksh grep DATA File1.txt >> File2.txt sed 's/DATA//' File2.txt | tr -d ‘ ‘ >> File4.xls As you can see my output is going in a xl file.The output consist of four columns/feilds out of which the first... (20 Replies)
Discussion started by: Sharma331
20 Replies

2. Shell Programming and Scripting

Shell script to read specified value from file and echo to the same location to other file.

Hello. I want to to backup some "default:" values from a file do some other job and after restore that "default:" values back. The problem is that the source and destination file has a lot of default: strings in it but with different values... So.. Here is an example: A part of my source... (6 Replies)
Discussion started by: ausdim
6 Replies

3. UNIX for Dummies Questions & Answers

How to read a file without opening the file and delete last line?

I have file called "text". The contents are as below : aaa bbb ccc ddd eee ffff ddd hhhh iiii I want to read this file without opening and and delete the last line. How can it be done? (4 Replies)
Discussion started by: the_hunter
4 Replies

4. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

5. Shell Programming and Scripting

Selectively Find/Replace in a file?

I have a file that is HTML encoded. Each line has something like this on each line.. <href=http://link.com/username.aspx>username </a> more info.. <a href=http://link.com/info1.aspx>info1</a> more code... <a href=http://link.com/info2.aspx>info2</a> I have one goal really.. to clean up the... (2 Replies)
Discussion started by: dragin33
2 Replies

6. UNIX for Dummies Questions & Answers

echo appends to a read-only file

echo command can append to a read-only file too. If the file is read-only, with w flag removed, then even echo should not be able to append to the file. But it is possible. Can anybody explain the below behaviour? /root > echo 'sample text' > file /root > cat file sample text /root >... (2 Replies)
Discussion started by: anand_bh
2 Replies

7. Shell Programming and Scripting

how to read a file to an echo statement

I was searching for an option where i can echo some strings together with the contents of a file. Eg. I was to echo the below string to a file "alter application PMS_ add variable CurrYear Y2009;" >> ${ESS_MAXL_DIR}/PMS_SUB.txt The value 2009 is coming from a file called date.txt without the... (3 Replies)
Discussion started by: Celvin VK
3 Replies

8. UNIX for Advanced & Expert Users

Selectively Reformating a file using AWK

Dear users, I am new to AWK and have been battling with this one for close to a week now. Some of you did offer some help last week but I think I may not have explained myself very well. So I am trying again. I have a dataset that has the following format where the datasets repeat every... (5 Replies)
Discussion started by: sda_rr
5 Replies

9. UNIX for Dummies Questions & Answers

File read/delete synchronization in unix

Hi, I have 2 processes that will be accessing the same file. First process first reads from the file, closes it and then opens it again to write to it (this is done sequentially). The second process may delete the file on some condition. These processes are independent of each other now. What... (2 Replies)
Discussion started by: saumya.c
2 Replies

10. Shell Programming and Scripting

Selectively splitting a file with C-shell?

I have a rather long csh script that works, but it's terribly ungraceful and takes a while from various loops. I only know enough code to get myself into trouble, so I'm looking for some guidance. I have a large file that is separated at intervals by the same line, like this: ... (2 Replies)
Discussion started by: fusi0n
2 Replies
Login or Register to Ask a Question