Bash Script to delete txt file based on the following

Hi all,

Im trying to make a bash script that searches through a directory and deletes a .txt file based on .wav files of the same name.

Search Dir = /voicemail/default

e.g.

Current Dir = /voicemail/default/2300
------------
one.txt
one.wav
one.WAV
two.txt
------------

In this example I want the script to delete the two.txt because it doesnt have the assoicated .wav files
Im not too sure how this is done as I dont have much bash scripting experience.

Please help.

Thanks,
Joe



LVL 1
joe90kaneAsked:
Who is Participating?
 
pjedmondConnect With a Mentor Commented:
If it can be a .WAV *or* a .wav then you could:

find /voicemail/default/ | grep -e "\.txt" | sed -e s/\.txt$// | awk {'print "ls "$1".wav || ls "$1".WAV || rm -Rf "$1".txt"'} | /bin/bash

(   (()
(`-' _\
 ''  ''
0
 
slyongCommented:
Hope this will do the trick, don't try it on the live directory (the forth line is what you look for).  Make a temp directory to test it:

$ mkdir test_dir && cd test_dir
$ touch one.txt one.wav one.WAV two.txt
$ ls
$ for files in *.txt; do [ -f `echo $files | cut -f1 -d.`.wav ] || rm $files; done
$ ls
0
 
ahoffmannCommented:
(find $Dir -name \*.wav;ls -1 $Dir/*.txt)|sed -e 's/\.wav$//' -e 's/\.txt$//'|sort|uniq -u|sed -e 's/.*/rm &.txt/'|sh

for testing omit the final  |sh
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
joe90kaneAuthor Commented:
Hi ahoffmann,

In your script ( $Dir -name <--Do i put the name on the directory here?

Thanks,
Joe
0
 
joe90kaneAuthor Commented:
OK so I have a file called

voicemail.sh

Contents = $find $Dir -default \*.wav;ls -1 $Dir/*.txt)|sed -e 's/\.wav$//' -e 's/\.txt$//'|sort|uniq -u|sed -e 's/.*/rm &.txt/'|sh

I put in the "/voicemail/" folder

Does this script look inside /voicemail/default/**ALL Directories**

and deletes the odd txt files????

Im missing something here, Im a novice so please point out even the most basic of information.

Thanks,
Joe
0
 
byttaCommented:
there should not be a $ in front of find.

$Dir is a variable that must be set to the directory you want to delete from
 -default is meaningless: ahoffmann said "find $Dir -name" not "$find $Dir -default"
Just do as he says, OK?

Try this voicemail.sh  - do not include the -------------- lines
--------------
#! /bin/sh
Dir=$1    #directory set to argument 1
(find $Dir -name \*.wav;ls -1 $Dir/*.txt)|sed -e 's/\.wav$//' -e 's/\.txt$//'|sort|uniq -u|sed -e 's/.*/rm &.txt/'|sh
--------------
called by writing:
voicemail.sh /voicemail/default/

for testing omit the final  |sh
0
 
joe90kaneAuthor Commented:
Thanks,

Im getting the following error when i run it:

----------------------------------------------------------

[root@asterisk1 voicemail]# voicemail.sh /default/
-bash: voicemail.sh: command not found
[root@asterisk1 voicemail]#

-----------------------------------------------------------
0
 
joe90kaneAuthor Commented:
sorry guys,

I've ran the script:

heres the results:

Current result = http://www.cwps.ie/info.jpg

OK so it has removed the text files associated with the .wav files.

I need to delete only the text files that have no wav files of the same name:

Wanted Result = http://www.cwps.ie/result.jpg

Thanks for the help

0
 
pjedmondCommented:
If it's just no .wav files, then

find /voicemail/default/ | grep -e "\.txt" | sed -e s/\.txt$// | awk {'print "ls "$1".wav || rm -Rf "$1".txt"'} | /bin/bash

Will find all files in your /voicemail/default/

It then only lists those that end in .txt, and then uses sed to remove this ending. Note the \. to make sure that the sed will not match "xdcvxctxt" or any other wierd combinations. Also note the $ which means that the .txt must be at the end of the line, and problems will not occur with a filename such as /voicemail/default/my.txt.dir/filename

the awk statement prints the command:

ls filename.wav || rm Rf -Rf filename.txt

i.e if the filename.wav does not exist, then delete the txt file. This conforms to your result.jpg. If however, ther are some .WAV files as well as an appropriate match, then you could:

find /voicemail/default/ | grep -e "\.txt" | sed -e s/\.txt$// | awk {'print "ls "$1".wav && ls "$1".WAV || rm -Rf "$1".txt"'} | /bin/bash

OK there's plenty of output from this. If you don't want it, then:

find /voicemail/default/ | grep -e "\.txt" | sed -e s/\.txt$// | awk {'print "ls "$1".wav || rm -Rf "$1".txt"'} | /bin/bash > /dev/null

(   (()
(`-' _\
 ''  ''







0
 
joe90kaneAuthor Commented:
Thanks for the info pjedmond,

That works perfect.
0
 
pjedmondCommented:
A pleasure:)

(   (()
(`-' _\
 ''  ''

0
 
joe90kaneAuthor Commented:
This works for the main folder in the directory,

But it doesnt apply the changes to the other folders in the dir.

Mail DIR
    ---> INBOX = works fine
    ---> OLD = Doenst work
    ---> WORK = doesnt work

Let me know if this is handy to do.

Thanks,
Joe
0
 
pjedmondCommented:
As root, the above should work for all directories, *unless* extended ext functionality is being used.

Please provide:

ls -al inboxdir
lsattr inboxdir

Is this the same Q?

(   (()
(`-' _\
 ''  ''
0
 
joe90kaneAuthor Commented:
[root@parabox INBOX]# ls -al
total 1604
drwx-w----  2 asterisk asterisk   4096 Jul 14 17:22 .
drwx-w----  5 asterisk asterisk   4096 Jun 19 12:06 ..
-rw-r--r--  1 asterisk asterisk    248 Jul 13 17:05 msg0000.txt
-rwx------  1 asterisk asterisk   3564 Jul 13 17:04 msg0000.wav
-rwx------  1 asterisk asterisk    450 Jul 13 17:04 msg0000.WAV
-rw-r--r--  1 asterisk asterisk    249 Jul 13 17:51 msg0001.txt
-rwx------  1 asterisk asterisk  69164 Jul 13 17:51 msg0001.wav
-rwx------  1 asterisk asterisk   7080 Jul 13 17:51 msg0001.WAV
-rw-r--r--  1 asterisk asterisk    247 Jul 14 12:13 msg0002.txt
-rwx------  1 asterisk asterisk 213484 Jul 14 12:13 msg0002.wav
-rwx------  1 asterisk asterisk  21706 Jul 14 12:13 msg0002.WAV
-rw-r--r--  1 asterisk asterisk    248 Jul 14 13:27 msg0003.txt
-rwx------  1 asterisk asterisk 335084 Jul 14 13:27 msg0003.wav
-rwx------  1 asterisk asterisk  34056 Jul 14 13:27 msg0003.WAV
-rw-r--r--  1 asterisk asterisk    253 Jul 14 17:05 msg0004.txt
-rwx------  1 asterisk asterisk 485804 Jul 14 17:05 msg0004.wav
-rwx------  1 asterisk asterisk  49396 Jul 14 17:05 msg0004.WAV
-rw-r--r--  1 asterisk asterisk    248 Jul 14 17:22 msg0005.txt
-rwx------  1 asterisk asterisk 308844 Jul 14 17:22 msg0005.wav
-rwx------  1 asterisk asterisk  31390 Jul 14 17:22 msg0005.WAV


[root@parabox INBOX]# lsattr
------------- ./msg0003.WAV
------------- ./msg0000.wav
------------- ./msg0005.txt
------------- ./msg0000.txt
------------- ./msg0001.txt
------------- ./msg0003.txt
------------- ./msg0005.WAV
------------- ./msg0003.wav
------------- ./msg0002.wav
------------- ./msg0002.txt
------------- ./msg0000.WAV
------------- ./msg0004.wav
------------- ./msg0004.txt
------------- ./msg0002.WAV
------------- ./msg0004.WAV
------------- ./msg0005.wav
------------- ./msg0001.wav
------------- ./msg0001.WAV
[root@parabox INBOX]#
0
 
pjedmondCommented:
Looking at the permissios on that Dir (INBOX), it should work fine....which from your comments, it does.

Are permissions the same for OLS and WORK the same? No links or anything else odd?

(   (()
(`-' _\
 ''  ''
0
 
ahoffmannCommented:
still don't see what was wrong with my suggestion (except that it is case sensitive, which was not metioned to be a problem in the question). Can someone please enlighten me.
0
 
pjedmondCommented:
>still don't see what was wrong with my suggestion (except that it is case sensitive, which was not metioned to be a problem in the question).

Like many of these Qs, you have to 'guess' what the questioner wants....not what they're asking for! I'd been polishing my crystal ball, and was actually trying to show the versatility of the approach rather than answer the Q with the accepted answer - Obviously my crystal ball is working well at the moment;)
0
 
pjedmondCommented:
...someone mentioned that they sell Crystall Balls on ebay - you could try there? ;)
0
 
ahoffmannCommented:
pjedmond, I need to get such a crystall ball too. Thanks for the tip (going to hack e... now ...:-)
BTW, you proove again that IT isn't engineering, sigh.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.