Operating System - HP-UX
1833047 Members
2340 Online
110049 Solutions
New Discussion

Re: Parising the list of the files to be deleted

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Parising the list of the files to be deleted

Dear Sirs,

I want to clean up certain files in some log directory. I went to the log directory for listings of the files to be deleted.
I selected the files to be deleted. I redirected the output of $ll -lrt > tempfile. I edited this tempfile for the files to be deleted. Now this tempfile the contains the list of the files to be deleted.

I want to parse this tempfile to delete the files. Can anyone suggest the complete syntax of the command to be used rm command ?

Thanks,
Shiv
14 REPLIES 14
Sanjay_6
Honored Contributor

Re: Parising the list of the files to be deleted

Hi,

Say the file "file_name" has entries

file1
file2
.....


you can try,

# cat file_name |while read FILE
> do
> rm $FILE
done

Hope this helps.

regds
Shivkumar
Super Advisor

Re: Parising the list of the files to be deleted

Can I use -i option also to confirm before deleting the files ?
Sanjay_6
Honored Contributor

Re: Parising the list of the files to be deleted

Hi Shiv,

You can dress it up any way you like it. This was pretty raw. you can use the -i option with the rm command.

Hope this helps.

regds
James R. Ferguson
Acclaimed Contributor
Solution

Re: Parising the list of the files to be deleted

Hi Shiv:

If you simply add 'rm -i' to the read loop as for example:

#!/usr/bin/sh
while read LINE
do
rm -i ${LINE}
done < /tmp/files_to_remove
exit 0

...then you are mixing input from STDIN and you will *not* accomplish what you want.

Instead you need to separate (with different file descriptors) the input from the interactive 'rm' and the input from the file that you are reading:

#!/usr/bin/sh
exec 3< /tmp/files_to_remove
while read -u3 LINE
do
rm -i ${LINE}
done
exit 0

The second script will work. File descriptor #3 is used to read lines from the file '/tmp/files_to_remove'. Each LINE (file name) so read is offered for an interactive remove.

Regards!

...JRF...
Shivkumar
Super Advisor

Re: Parising the list of the files to be deleted

Respected James Sir,

If "files_to_remove" contains the list of the files to be removed then what is the purpose of word "LINE" in the script ?

Kindly explain.

Thanks,
Shiv
James R. Ferguson
Acclaimed Contributor

Re: Parising the list of the files to be deleted

Hi Shiv:

You asked "If 'files_to_remove' contains the list of the files to be removed then what is the purpose of word 'LINE' in the script?"

Yes, I used the name 'files_to_remove' to represent the name of the file that contains a list of files to remove. For example:

# cat files_to_remove
/tmp/f1
/tmp/f2
/tmp/f3
/home/jrf/oldtuff
/home/shiv/oldstuff

The word 'LINE' was merely the name that I chose for a variable that holds the data from a 'read' operation. In this case, the 'read' fills the 'LINE' variable with whatever it reads up until a newline character.

Since I only expected one field on each "line" of the file, I only used one variable.

Have a look at the man pages for 'read(1)' and compare how these two scripts work by running them and typing various sentences. Use a CTRL_D to kill each script:

while read X
do
echo ${X}
done < /dev/tty

...versus:

while read X Y Z
do
echo ${X}
done < dev/tty

For example type sentences like:

who am i
what
does this help you understand?

Regards!

...JRF...
Arunvijai_4
Honored Contributor

Re: Parising the list of the files to be deleted

Hi Shiv, LINE is a shell variable which is used in reading values passed to that script. If you want to know more about it, http://www.shelldorado.com/goodcoding/cmdargs.html

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Cem Tugrul
Esteemed Contributor

Re: Parising the list of the files to be deleted

Hi Shiv,

it would be nice that you you to get such
a kind of document "SHELLS:User guide"

Good Luck,
Our greatest duty in this life is to help others. And please, if you can't
Arturo Galbiati
Esteemed Contributor

Re: Parising the list of the files to be deleted

Hi Shiv,
with simple command in one line:
rm -i $(
1. $() executes teh command within the parenthesis
2. < reads the files_to_removes
3. rm -i remove the file asking if ok

HTH,
Art
Raj D.
Honored Contributor

Re: Parising the list of the files to be deleted

Hi Shiv ,

Here is another , you can use:

Example: the file name is : files_to_remove

# vi remove.sh

#!/usr/bin/ksh
######## Removing files from the list file: files_to_remove ########################

for i in `cat files_to_remove`
do
rm -i $i
echo "FILE: $i will be removed now"
done
echo "File Remove completed"
##########################################



To execute this :
# chmod +x remove.sh
# ./remove.sh

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Muthukumar_5
Honored Contributor

Re: Parising the list of the files to be deleted

Why you are redirecting to some file and then doing action. Try as,

# cd
# ls -lrt | awk '/^-/ { print $9; }' | xargs rm -f

-Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Parising the list of the files to be deleted

More informations,

ll -lrt > tempfile is a main problem.

It will give directory, filenames etc to the tempfile. If you try to remove with rm command then it will delete files and as well as directories when proceeding with option -rf.

You can get only files in that directory not the sub directory then,

# cd
# ls -lrt | awk '/^-/ { print $9; }' | xargs rm -f

^- will give only files. $9 is the file name.
xargs used to get all files and do the rm -f action.

-Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: Parising the list of the files to be deleted

If you want interactive deletion, you can use -i option with "rm". Better create an alias for Trash and delete your files.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Raj D.
Honored Contributor

Re: Parising the list of the files to be deleted

Hi Shiv,

You must use awk , to filter only the file names in your files_to_remove file list.
And so that it will only contains the names of the file not the permissions and date time stamp.

ex:
# ls -l |grep -v ^d | awk '{print $9}' > files_to_remove

[ It will eliminate the directory names and only files will be listed in the file ]


hth,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "