Delete file from list of folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete file from list of folders
# 1  
Old 10-31-2009
Delete file from list of folders

Hi all,
I amd new in UNIX programming. I have a query.
I need to delete some files (like .dec files) from some folders. I have a list of folders. What will be command for it.
Please help me.

Thanks in Advance.
# 2  
Old 10-31-2009
Something like
Code:
for FOLDER in list-of-folders-seperated-by-spaces
do
    rm "$FOLDER/*.dec"  # for .dec files
done

take care if there are spaces in the names of the folders !
# 3  
Old 10-31-2009
Learn "find" command which can do the job better, according to the situation.

Code:
find . -iname *.dec -ok rm {} \;

or

Code:
find ./fold1 /path/fold2 *.dec -ok rm {} \;

The above will ask about removing particular file... and removes if you ask it to do so.

There is more than one way to do it !!
# 4  
Old 11-01-2009
Deleting files in this way is a common requirement and poorly written scripts to do so are a frequent cause of people coming to grief deleting unexpected files and directories.

A few areas people commonly go wrong: -

With this sort of method: -

Code:
find . -iname *.dec -ok rm {} \;

I have not seen a find run this way I would expect a -exec in there but I have only been scripting a year so this may be valid. But to discuss the method I am familiar with as often seen running in a script: -

Code:
find . -iname *.dec -exec rm {} \;

Using this method a new rm process is kicked off every time the find produces a file to delete, the last cleardown script I wrote clears down over 100,000 files. If we were to kick off a new process for each file found then the job would take forever.

A more efficient way from the process point of view would be: -

Code:
rm -f $(find . -iname *.dec)

This only kicks off the one rm process and the one find process.

To move on to the subject of unintentionally deleting the wrong files: -

Code like the above is very dangerous as the find can start following its way down sub directories and deleting files you had not intended.

This is also a dangerous construct commonly found within a script, although not such a problem in the for loop above: -

Code:
rm "$FOLDER/*.dec"  # for .dec files

No check is made to see if the $FOLDER is a valid directory. If the variable is empty then the command will try to delete files under the root-

Code:
rm /*.dec"

By far the safest approach is to: -
  1. read in the list of folders from a config file.
  2. change into the directory
  3. check $PWD to confirm you are in the directory
  4. issue the command rm -f *.dec from there
That way you have no chance of deleting anything outside of your target directory.

Good luck! Smilie

Last edited by steadyonabix; 11-01-2009 at 06:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to compare files in 2 folders and delete the large file

Hello, my first thread here. I've been searching and fiddling around for about a week and I cannot find a solution.:confused: I have been converting all of my home videos to HEVC and sometimes the files end up smaller and sometimes they don't. I am currently comparing all the video files... (5 Replies)
Discussion started by: Josh52180
5 Replies

2. Shell Programming and Scripting

Compress and delete folders

Hello, can someone help me to create a short script that tar.gz all folders form a specific folder and the delete the folders themself (without deleting the tar.gz created files)? Moreover I'd like to add it on crontab and run it each day. For example: in /tmp I have: /tmp/test/test1... (7 Replies)
Discussion started by: mab80
7 Replies

3. UNIX for Dummies Questions & Answers

How to delete some empty folders?

I have an amount of folders and I want to delete only the empty ones. But I have more than 200 empty folders, so I would preffer do not delete one by one... I know it is possible, but I don't know how. I've tried with the size, using 'du' command, and saving the result in a file. After that, I made... (3 Replies)
Discussion started by: saitsug
3 Replies

4. Shell Programming and Scripting

How to delete other's folders which are in our own folder?

Hi All, I need a solution for the following scenario. I am the owner for the particular folder and I have given 777 permissions for some specific reasons. So others can able to create folders and files. But when I am done with the work, I need to delete the folders which are created by... (4 Replies)
Discussion started by: manoj_thorali
4 Replies

5. Shell Programming and Scripting

Delete folders older than 30 days

Dear all, i use incremental backup my data with .zip to my hard drive. what i need is i don't want the old .zip file older than 30 days. how to write a shell script automatically remove my external hard disc zip backup folders older than 30 days? Regards, (2 Replies)
Discussion started by: joneggk
2 Replies

6. UNIX for Dummies Questions & Answers

Delete folders and files in it - UNIX

I need to delete a folder and files in it of yesterdays and simply put this in cron. Folder name - "2010-03-2010" File name - "eprod_06_23.dmp" and "eprod_06_23.exp" Actually this folder and file is been created by a script which takes a backup of DB everyday.Thats why it creates folder and file... (3 Replies)
Discussion started by: j_panky
3 Replies

7. Shell Programming and Scripting

Delete block of text in one file based on list in another file

Hi all I currently use the following in shell. #!/bin/sh while read LINE do perl -i -ne "$/ = ''; print if !m'Using archive: ${LINE}'ms;" "datafile" done < "listfile" NOTE the single quote delimiters in the expression. It's highly likely the 'LINE' may very well have characters in it... (3 Replies)
Discussion started by: Festus Hagen
3 Replies

8. UNIX for Dummies Questions & Answers

delete recursively contents of folders

hi, I've a folder structure like : /home/project/LIBNAMEA/FILE1 /home/project/LIBNAMED/FILE2 /home/project/LIBNAMEC/FILE3 /home/project/LIBNAMED/FILE4 /home/project/LIBNAMEX/FILE5 (there is no relation in the letters after the project/ ) and i need to delete the files keeping... (5 Replies)
Discussion started by: jtmartins
5 Replies

9. UNIX for Advanced & Expert Users

need to delete the sub folders

Hi Is there any comand where in which we can check the time stamp of the sub folder created in one main folder and delete all the other subfolder which have been created one week before the actual time (current time ) or even 15 days before the current time Thanks in advance (1 Reply)
Discussion started by: laxmi131
1 Replies

10. Shell Programming and Scripting

delete all folders/files and keep only the last 10 in a folder

Hi, I want to write a script that deletes all folders and keep the last 10 recent folders. I know the following: ls -ltr will sort the folders from old to recent. ls -ltr | awk '{print $9}' will list the folder names (with a blank line at the beginning) I want to get the 10th folder from... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question