[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Bash Script to delete txt file based on the following

Posted on 2006-07-14
19
Medium Priority
?
908 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 3
  • +2
19 Comments
 
LVL 24

Expert Comment

by:slyong
ID: 17106889
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
ID: 17106993
(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
ID: 17107122
Hi ahoffmann,

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

Thanks,
Joe
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:joe90kane
ID: 17107872
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
ID: 17108326
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
ID: 17108704
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
ID: 17109270
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
ID: 17109418
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 2000 total points
ID: 17109448
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
 
LVL 1

Author Comment

by:joe90kane
ID: 17109503
Thanks for the info pjedmond,

That works perfect.
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 17109531
A pleasure:)

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

0
 
LVL 1

Author Comment

by:joe90kane
ID: 17110563
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
ID: 17110827
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
ID: 17113513
[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
ID: 17113733
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
ID: 17120840
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
ID: 17120937
>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
ID: 17120943
...someone mentioned that they sell Crystall Balls on ebay - you could try there? ;)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17121261
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

649 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