Sheel script to Delete a set of files from a given directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sheel script to Delete a set of files from a given directory
# 1  
Old 04-13-2009
Sheel script to Delete a set of files from a given directory

I have a file <filestodelete> containing names of files to to be deleted from a directory <filesstore>.

I want a script file which accptes the <filestodelete> and also the location of the directory(<filestore>) and deletes all files matching.

Thanks in Advance..
# 2  
Old 04-13-2009
Hi,
you tried something? Or should us write the script for you?
I assure you it is very simple, and think if you try you can manage it.
There's no help if we write the script for you, because you won't learn anything.
Try to write the script and then ask the forum for help if it doesn't work .

Bye
# 3  
Old 04-13-2009
Hi Souron

I'm new to unix and i have been tring few basic steps for the below problem.

I just wanted to check if the there are any file in the directory

i wrote the code as below..
if [ -f * ]
then
echo Files exist
fi

but throw up an error as
-bash: [-f: command not found
# 4  
Old 04-13-2009
Hi,
bash tells you "command not found" because you typed [-f ... without spaces,
anyway the option "-f" check a single file so even if you write the if statement the right way, the code you posted won't work.

Anyway if you want to delete files you can try this:

Code:
for FILES in $(cat <filestodelete>); do 
rm /path_to_your_directory/$FILES
done

if you want to send the directory as a parameter of the script, you can try what follow

./script_to_delete_files.sh <directory>
in your code you'll write:

[[ -z "$1" ]] && echo "you must enter a dir" && exit 0
[[ ! -d "$1" ]] && echo "Directory $1 doesn't exist" && exit 0

for FILES in $(cat <filestodelete>); do
rm /path_before_your_directory/$1/$FILES
done

Hope it helps

Bye

Last edited by Yogesh Sawant; 04-13-2009 at 07:32 AM.. Reason: added code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. AIX

How to set owner and permission for files/directory in directory in this case?

Hi. My example: I have a filesystem /log. Everyday, log files are copied to /log. I'd like to set owner and permission for files and directories in /log like that chown -R log_adm /log/* chmod -R 544 /log/*It's OK, but just at that time. When a new log file or new directory is created in /log,... (8 Replies)
Discussion started by: bobochacha29
8 Replies

2. 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

3. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

4. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

5. Shell Programming and Scripting

Need to delete large set of files (i.e) close to 100K from a directory based on the input file

Hi all, I need a script to delete a large set of files from a directory under / based on an input file and want to redirect errors into separate file. I have already prepared a list of files in the input file. Kndly help me. Thanks, Prash (36 Replies)
Discussion started by: prash358
36 Replies

6. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

7. Shell Programming and Scripting

chaging a environment varible using sheel script

hi How can one change an environment varaible inside a shell script say my bash shell has a env variable export FOO="Day" no i want to write a script inside which the FOO variable is modified to say export FOO=NIGHT after this script finishes te bash shell should show me NIGHT when i... (1 Reply)
Discussion started by: wojtyla
1 Replies

8. HP-UX

Unix sheel script to run report

Hi All, I have a report which I am running from unix shell script using CONCSUB utility. Just for testing I put the order number in shell script as ord_low and ord_high. But Actually what is going to happen is that the order numbers will be in a file say a.txt and this CONCSUB should... (0 Replies)
Discussion started by: isingh786
0 Replies
Login or Register to Ask a Question