[SOLVED] Delete files with complex filenames with a script
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
#!/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.
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 12:58 PM.
Reason: Remove quote of previous post (save LQ space)
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
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?
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.
#!/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
-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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.