Delete files using csh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete files using csh script
# 1  
Old 11-16-2010
Delete files using csh script

Want to write a csh scripts where I pass the extension of certain files, and the script will detete all such files.

For example, the following will delete all files with extension .xt

Code:
./clean.csh xt

# 2  
Old 11-16-2010
Quote:
find <directory_path> -name "$1" -exec rm -f {} \;
# 3  
Old 11-16-2010
I have tried the below to remove .xt files and did not work.

Code:
find -name "xt" -exec rm -f {} \;


Also I want only .xt files to be removed. For example, .txt files would not be touched.
# 4  
Old 11-16-2010
Code:
find <directory_path> -name "$1" -exec rm -f {} \;

Path is missing in your command. Give absolute path under which you want to delete files, OR you can give dot(.) for current directory.
e.g.
Code:
find /abc/def -name "$1" -exec rm -f {} \;

OR
Code:
find . -name "$1" -exec rm -f {} \;

# 5  
Old 11-16-2010
Oh yes, my mistake.

What the difference between these two?


Code:
find . -name "*.xt" -exec rm -f {} \;
find . -name "*.xt" -exec rm '{}' ';'

Would the following code remove .txt files as well?

Code:
find . -name "xt" -exec rm -f {} \;

# 6  
Old 11-16-2010
-f option does force delete [otherwise it asks for delete confirmation (y/n) if file premission is not proper, say deleting a file owned by others]

Following will delete only file with exact xt name, So wildcard needed.
Code:
 
find . -name "xt" -exec rm -f {} \;

# 7  
Old 11-16-2010
I understand now

---------- Post updated at 09:46 AM ---------- Previous update was at 07:31 AM ----------

I have this csh script and I want to check what the user input and only allow him to delete .xt and .ps files only. How can I do that please?

Code:
  alias MATH 'set \!:1 = `echo "\!:3-$" | bc -l`'

  set narg = $#argv
  while ($iarg < $narg)

    MATH iarg = $iarg + 1
    set arg = $argv[$iarg]
    set opt = `echo $arg | awk 'BEGIN {FS="="} {print $1}'`
    set par = `echo $arg | awk 'BEGIN {FS="="} {print $2}'`

    switch ($opt)
    case "-rmfiles":
        set rmext = `echo $par | awk 'BEGIN {FS="."} {print $1}'`
        set opt_rmfiles = 1
        breaksw
    default:
        set filename = `echo $arg | awk 'BEGIN {FS="."} {print $1}'`
        set filextension = `echo $arg | awk 'BEGIN {FS="."} {print $2}'`
        set fullnames_list = "$fullnames_list $arg"
        set fnames_list = "$fnames_list $filename"
        set fext_list = "$fext_list $filextension"
        MATH nf = $nf + 1
    endsw

  end   # while

  if ($opt_rmfiles == 1) then
    find . -name "*.$rmext" -exec rm -f {} \;
  endif

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Files delete script

Friends, I had written a small script to delete files from deletefiles.txt file. However, I want to add one more piece to script, so as to check if the file(file abc) which was already deleted earlier exists in 'deletefiles.txt' file, script should comment out that "file abc doesnt exist". Can... (5 Replies)
Discussion started by: fop4658
5 Replies

3. Shell Programming and Scripting

Need script to delete old files

Hi, I need a script to delete files older than 2 years or a year. I have around hundreds of old files which needs to be deleted. Could you please help. (2 Replies)
Discussion started by: sv0081493
2 Replies

4. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

5. UNIX for Dummies Questions & Answers

script to delete old files

Hi, I want to delete files that are older than 60 days.... i need to execute the script in 7 differnt folders.... i can run the script in crontab to regularly check.... I am struck @ finding out how the file is 60 days old or not... Can u please help me on this? Thanks, NithZ (6 Replies)
Discussion started by: Nithz
6 Replies

6. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

7. Shell Programming and Scripting

csh script to search for files and display result

Hello all. I'm a long time browser, first time poster...Be gentle :-) I don't use csh much, but have been asked to make a small script for our HP-UX systems to search for a report file(s) or summary file and display the result (I was using vuepad, but this probably is just complicating... (4 Replies)
Discussion started by: neillsm
4 Replies

8. Shell Programming and Scripting

script to delete files

I have 1000 directories named: 0 - 999 which should contain 1000 files named 0 - 999. But some of these directories contain file whose names are greater than 999 and I need to delete those. I wrote the script below but that doesnt work. Any ideas? #!/bin/bash DIRS=999 for (( j = 0 ; j <... (3 Replies)
Discussion started by: looza
3 Replies

9. Shell Programming and Scripting

csh failing to call an 2 embedded csh script

I have an extraordinary problem with a csh script.....(feel free to berate the use of this but I'm modifying an existing bunch of them) Anyway, I have a master csh script which in turn calls a second csh script. This second csh script is below. Within this second script are two compiled C++... (1 Reply)
Discussion started by: pollsizer
1 Replies

10. Shell Programming and Scripting

script to delete files

hi guys, i need a script to delete files that have core in their name ...it might be part of the file name or as a .core extension ...any file that has core as its extension.... i am only able to delete files which just have thier name as core using this : find $1 -type f -name "core"... (12 Replies)
Discussion started by: vats
12 Replies
Login or Register to Ask a Question