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.
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: -
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: -
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: -
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: -
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-
By far the safest approach is to: -
read in the list of folders from a config file.
change into the directory
check $PWD to confirm you are in the directory
issue the command rm -f *.dec from there
That way you have no chance of deleting anything outside of your target directory.
Good luck!
Last edited by steadyonabix; 11-01-2009 at 07:44 AM..
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)