Reputation: 378
I'm trying to make a linux script that will be updating some lists, with a timestamp on them.
The part I'm struggling is this:
for dir in $(ls)
do
echo $dir
done
I get:
noreply-unsch-simco 2014-03-17-14:20:41.txt
noreply-unsch-simcoe 2014-03-17-14:20:41.txt
noreply-unsch-sudbury-nbay 2014-03-17-14:20:41.txt
noreply-unsch-test 2014-03-17-14:20:41.txt
noreply-unsch-thunderbay 2014-03-17-14:20:41.txt
noreply-unsch-toronto 2014-03-17-14:20:41.txt
noreply-unsch-york 2014-03-17-14:20:41.txt
What I want to do is remove the older files (if they are present), because every time my script runs, it makes a new file with a new timestamp.
So basically I want to know how to search for files using only the beginning part of the name (everything before the timestamp).
I'm new to this. Help me kindly please
EDIT: Ok, so I looked at the comment but I'm getting error. Here's my full code.
#!/bin/bash
cd /var/lib/mailman/lists/
variable=$(date +"%Y-%m-%d-%H:%M:%S")
for dir in $(ls)
do
echo $dir
rm /root/Desktop/Dan/Lists/$dir-*
echo echo | cat | list_members $dir/ > /root/Desktop/Dan/Lists/$dir" "$variable.txt
done
I'm getting a directory not found error.
EDIT I just had to take away the '-'
rm /root/Desktop/Dan/Lists/$dir*
Thanks!
Upvotes: 0
Views: 176
Reputation: 37792
if you want to remove all files beginning with a certain extension you can do this:
rm noreply-unsch-*
this will remove all files matching that pattern. If you want to do something more than just removing ; you can change your loop into:
for filename in noreply-unsch-*.txt; do
#something
rm $filename
done
Upvotes: 5