Solved

Bash Script to delete txt file based on the following

Posted on 2006-07-14
19
895 Views
Last Modified: 2010-04-20
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



0
Comment
Question by:joe90kane
  • 7
  • 7
  • 3
  • +2
19 Comments
 
LVL 24

Expert Comment

by:slyong
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
(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
 
LVL 1

Author Comment

by:joe90kane
Comment Utility
Hi ahoffmann,

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

Thanks,
Joe
0
 
LVL 1

Author Comment

by:joe90kane
Comment Utility
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
 
LVL 4

Expert Comment

by:bytta
Comment Utility
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
 
LVL 1

Author Comment

by:joe90kane
Comment Utility
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
 
LVL 1

Author Comment

by:joe90kane
Comment Utility
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
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
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
 
LVL 22

Accepted Solution

by:
pjedmond earned 500 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:joe90kane
Comment Utility
Thanks for the info pjedmond,

That works perfect.
0
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
A pleasure:)

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

0
 
LVL 1

Author Comment

by:joe90kane
Comment Utility
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
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
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
 
LVL 1

Author Comment

by:joe90kane
Comment Utility
[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
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
>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
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
...someone mentioned that they sell Crystall Balls on ebay - you could try there? ;)
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now