LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-24-2010, 09:53 AM   #1
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Rep: Reputation: 0
Delete files with complex filenames with a script


Hi,
I am trying to write a bash script to delete a file where I know the first part of the name, but it has spaces in it. The second part of the file name is a random set of characters. I was hoping to use a wildcard for this, but I keep getting a error massage saying file or directory does not exist. This is a simplified form of my script:
Code:
MYFILE=This is my file

rm "$MYFILE*"
The file may be something like "This is my file.123abc456.suffix"

How can I delete this file using a script and wildcard?

Last edited by NickJH; 05-25-2010 at 08:52 AM.
 
Old 05-24-2010, 09:59 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
find <path/of/dir> -name "$MYFILE*" | xargs -i rm -f {}
 
Old 05-24-2010, 10:00 AM   #3
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
Wouldn't the following work:

Code:
$cd /path/to/files

$sudo rm first\ part\ you\ know*
So if you had two files "first part you knowblahblah.blah" and "first part you know2blahblah.blah" the above should prompt and remove them.

Basically the back slashes escape the spaces.

Hope this helps,
 
Old 05-24-2010, 10:00 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Or search LQ for similar questions. Here is a search for find filename and space.
 
Old 05-24-2010, 10:47 AM   #5
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PMP View Post
Code:
find <path/of/dir> -name "$MYFILE*" | xargs -i rm -f {}
Thanks but I am now failing with the meta-characters "[" and "]" in the filename.

The full file name I want to delete is
Code:
"Alvin and the Chipmunks - The Squeakquel[2009.DvDRiP[NoRaR]L33t - bittoxic.8eaa61dc40f690ea.torrent"
where I know everything up to and including bittoxic.
 
Old 05-24-2010, 11:08 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by NickJH View Post
Thanks but I am now failing with the meta-characters "[" and "]" in the filename.
Time to wheel on the robust solution
Code:
while IFS= read -r -d '' file
do
   echo rm "$file"
done < <(find your_starting_directory -type f -name 'your_name_starting_string*' -print0)
Remove the echo when you are confident the loop is generating the correct command.
 
Old 05-24-2010, 12:51 PM   #7
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
I'm struggling to get it to work.

This is where I'm at:
Code:
#!/bin/bash

TRANSMISSIONHOMEPATH=/home/transmission/
DOWNLOADPATH=/home/transmission/Downloads/
TORRENTPATH=$TRANSMISSIONHOMEPATH.config/transmission-daemon/torrents/

if [ -e $DOWNLOADPATH* ]; then
	for i in $DOWNLOADPATH*; do
		TORRENTNAME=${i/$DOWNLOADPATH/}
		chown :howitts "$i" -R
		chmod 774 "$i" -R
		while IFS= read -r -d '' file
		do
			echo rm "$file"
		done < <(find $TORRENTPATH -type f -name '$TORRENTNAME*' -print0)


	done

fi
There is another bug in the if statement when the path has multiple files in it, but I'll sort that later. I've tried with and without the trailing "/" at the end of setting TORRENTPATH but it makes no difference. The echo statement appears to do nothing which is odd.
 
Old 05-24-2010, 01:58 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The echo statement is not being run because find $TORRENTPATH -type f -name '$TORRENTNAME*' -print0 is not finding anything because of the single quotes which prevent $TORRENTNAME from being expanded. Fix by changing the single quotes to double quotes.

Last edited by catkin; 05-24-2010 at 01:58 PM. Reason: Remove quote of previous post (save LQ space)
 
Old 05-24-2010, 02:38 PM   #9
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
I've tried changing the single quotes to double quotes, but it looks like the while loop is still not doing anything.
 
Old 05-24-2010, 02:47 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by NickJH View Post
I've tried changing the single quotes to double quotes, but it looks like the while loop is still not doing anything.
A powerful debugging technique is to peel the onion from the inside out.

First off, find what $TORRENTPATH and $TORRENTNAME are for the find
Code:
#!/bin/bash

TRANSMISSIONHOMEPATH=/home/transmission/
DOWNLOADPATH=/home/transmission/Downloads/
TORRENTPATH=$TRANSMISSIONHOMEPATH.config/transmission-daemon/torrents/

if [ -e $DOWNLOADPATH* ]; then
        echo "DEBUG: TORRENTPATH: '$TORRENTPATH', TORRENTNAME: '$TORRENTNAME'"
	for i in $DOWNLOADPATH*; do
		TORRENTNAME=${i/$DOWNLOADPATH/}
		chown :howitts "$i" -R
		chmod 774 "$i" -R
		while IFS= read -r -d '' file
		do
			echo rm "$file"
		done < <(find $TORRENTPATH -type f -name '$TORRENTNAME*' -print0)
	done
fi
BTW it does look od that you are setting TORRENTNAME=${i/$DOWNLOADPATH/} inside the loop, after it has already been used in the find command. EDIT2: that's nonsense, please ignore!

Then you can run the find command at the command line and see if it produces sane output (remove the -print0 so it's easier to see).

EDIT: you can also use set -xv to trace the script (and set +xv to turn it off)

EDIT3: the debug line is in the wrong place, should be like this
Code:
#!/bin/bash

TRANSMISSIONHOMEPATH=/home/transmission/
DOWNLOADPATH=/home/transmission/Downloads/
TORRENTPATH=$TRANSMISSIONHOMEPATH.config/transmission-daemon/torrents/

if [ -e $DOWNLOADPATH* ]; then
	for i in $DOWNLOADPATH*; do
		TORRENTNAME=${i/$DOWNLOADPATH/}
		chown :howitts "$i" -R
		chmod 774 "$i" -R
                echo "DEBUG: TORRENTPATH: '$TORRENTPATH', TORRENTNAME: '$TORRENTNAME'"
		while IFS= read -r -d '' file
		do
			echo rm "$file"
		done < <(find $TORRENTPATH -type f -name '$TORRENTNAME*' -print0)
	done
fi

Last edited by catkin; 05-24-2010 at 02:51 PM.
 
Old 05-24-2010, 03:11 PM   #11
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
I have been using echo's to try to debug this and the parameters outside the while loop seem to be fine.

Moving the echo inside the for loop I get:
TORRENTPATH: '/home/transmission/.config/transmission-daemon/torrents/'
TORRENTNAME: 'Alvin and the Chipmunks - The Squeakquel[2009.DvDRiP[NoRaR]L33t - bittoxic'

TORRENTPATH should be the folder where Transmission stores its Torrent tracker.
TORRENTNAME is the name of the file downloaded by the torrent.

Downloaded Torrents go into the DOWNLOADPATH when the download has completed. The idea of the for loop is to pick each completed download in turn then strip off the path to the download to get the pure file name (TORRENTNAME) for later use. I would expect to set TORRENTNAME inside this loop as it changes for each downloaded file that I want to move.

The Torrent's tracker gets stored in TORRENTPATH and is the same as TORRENTNAME with a "." then some random digits then ".torrent" stuck on the end.

Removing the -print0 does nothing. Have we still got the meta-character problem?
 
Old 05-24-2010, 03:16 PM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
What is the output of
Code:
find '/home/transmission/.config/transmission-daemon/torrents/' -type f -name 'Alvin and the Chipmunks - The Squeakquel[2009.DvDRiP[NoRaR]L33t - bittoxic*'
Meanwhile I'll study the rest of what you have posted and reply later.
 
Old 05-24-2010, 03:22 PM   #13
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
As you posted I was trying your set -xv trick and had just tried your command on its own!

Unfortunately it produces nothing.
 
Old 05-24-2010, 03:43 PM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by NickJH View Post
As you posted I was trying your set -xv trick and had just tried your command on its own!

Unfortunately it produces nothing.
That explains why the echo in the while IFS= read loop is not executed. What is the output from
Code:
/bin/ls -l /home/transmission/.config/transmission-daemon/torrents/Alvin*
Meanwhile, maybe this will work
Code:
#!/bin/bash

TRANSMISSIONHOMEPATH=/home/transmission/
DOWNLOADPATH=/home/transmission/Downloads/
TORRENTPATH=$TRANSMISSIONHOMEPATH.config/transmission-daemon/torrents/

cd $DOWNLOADPATH || exit 1
for TORRENTNAME in *
do
    for TORRENT in "$TORRENTPATH$TORRENTNAME"*.torrent
    do
        echo rm "$TORRENT"
    done
done
 
Old 05-24-2010, 03:56 PM   #15
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
What is the output from
Code:
/bin/ls -l /home/transmission/.config/transmission-daemon/torrents/Alvin*
Code:
-rw------- 1 transmission allusers 14936 May 22 11:27 /home/transmission/.config/transmission-daemon/torrents/Alvin and the Chipmunks - The Squeakquel[2009.DvDRiP[NoRaR]L33t - bittoxic.8eaa61dc40f690ea.torrent
-rw------- 1 transmission allusers 31606 May 23 12:40 /home/transmission/.config/transmission-daemon/torrents/Alvin.and.the.Chipmunks.The.Squeakquel.2009.NORDiC.DVDR-HiGH.f836de6d3a3497cf.torrent
It is only the first of these I am trying to delete.

Quote:
Meanwhile, maybe this will work
Code:
#!/bin/bash

TRANSMISSIONHOMEPATH=/home/transmission/
DOWNLOADPATH=/home/transmission/Downloads/
TORRENTPATH=$TRANSMISSIONHOMEPATH.config/transmission-daemon/torrents/

cd $DOWNLOADPATH || exit 1
for TORRENTNAME in *
do
    for TORRENT in "$TORRENTPATH$TORRENTNAME"*.torrent
    do
        echo rm "$TORRENT"
    done
done
It is looking good.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 04:43 PM
Delete old files script simpi Linux - Newbie 11 04-25-2008 03:37 AM
How to write a script to delete files? AGazzaz Linux - General 11 12-05-2007 07:43 AM
script to insert filenames into files magnim Linux - General 4 03-22-2007 02:05 PM
Shell script: Delete filenames containing a substring? Drack Linux - General 4 02-12-2006 04:11 PM


All times are GMT -5. The time now is 10:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration