deleting files in folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting deleting files in folder
# 1  
Old 02-21-2006
deleting files in folder

helloo people...

I was trying to do one script that would delete all files in one folder

there are 2-3 usefull files in folder all other is junk..

how to say delete all except those 3 files...

thanks in advance

best regards,
# 2  
Old 02-21-2006
for file_name in `ls *`
do
if $file_name in ( your list of files)
dont delete it
else
rm -f $file_name
done

Regards,
Manish Jha
# 3  
Old 02-21-2006
if you know the file pattern that is to be retained without deleting,

you can try this,

assuming file patterns to be of a*

Code:
find . ! -name 'a*' -print | xargs rm

# 4  
Old 02-21-2006
hmmm looks ok but I am not quiet sure...

let say I need to delete all except 3 files names:

script2
script43
script203

all other files in that same folder should be deleted...

thank U all...
# 5  
Old 02-21-2006
Quote:
Originally Posted by amon
hmmm looks ok but I am not quiet sure...

let say I need to delete all except 3 files names:

script2
script43
script203

all other files in that same folder should be deleted...

thank U all...
if you are sure files with pattern script* to be retained

then,

Code:
find . ! -name 'script*' -type f |  xargs rm

just include type identifier (as file) to avoid listing of (.) directory
# 6  
Old 02-21-2006
thanks matrixmadhan

that works just perfect but I need to leave 2 files that are different names

script1
tprisc23

for example I tryed to include those 2 files but it does not work how do I expend this command

find . ! -name 'script*' -type f | xargs rm

to include also tprisc

thanksss
# 7  
Old 02-21-2006
helloo everyone...

I get this kind of error...

for i in `ls *`

if [ $i -ne skriptINOUT] then
echo "$i"
#dont delete it


else
rm -f $i
fi
done


I use #!/sbin/sh ... where do I mistake??

thank U all for trying...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

3. UNIX for Dummies Questions & Answers

Deleting folder with Date format

Hi, I have number of directories being created under a main directory on daily basis with the sysdate as below $ pwd /hello/TEST $ ls 20111103 20111102 20111101 20111031 20111030 20111029 20111028 20111027 20111026 20111025 20111024 20111023 20111022 20111021 (3 Replies)
Discussion started by: spari2
3 Replies

4. Shell Programming and Scripting

Can I use a shell script for deleting chunks from a watch folder?

Hello I have a unique problem of needing to delete large files slowly off of an XSan. I was wondering if there is a script I could use to delete 100gb chunks of files and folders that get placed in to a watch folder, slowly so as not to disrupt the other users. I would like to use Automator in... (0 Replies)
Discussion started by: ajsoto
0 Replies

5. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

6. Shell Programming and Scripting

check how many files in folder or total files in folder

Hi all, I have copied all my files to one folder.and i want to check how many files (count) in the folder recently moved or total files in my folder? please send me the query asap. (3 Replies)
Discussion started by: durgaprasad
3 Replies

7. Shell Programming and Scripting

Deleting the contents of a folder older than X hours

Every day a new .zip file is uploaded to a folder and at mid-night the zip file is to be extracted into a /data/ folder, inside a date-named folder. # This should extract the contents of a zip file into the /data/ folder into a date based folder /usr/bin/unzip -a -o... (15 Replies)
Discussion started by: worchyld
15 Replies

8. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

9. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

10. Shell Programming and Scripting

Deleting the file in a folder

hi everyone I am having some n number of files in folder I want delete a file which name has todays date example my file name is 14(todaysdate) when i want transfer files i want to delete that file which name has 14.i want delete only 14 file.need help ... (2 Replies)
Discussion started by: srivsn
2 Replies
Login or Register to Ask a Question