LinuxQuestions.org
Review your favorite Linux distribution.
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 10-24-2011, 08:42 AM   #1
str1fe
LQ Newbie
 
Registered: Jan 2010
Distribution: Cent OS
Posts: 7

Rep: Reputation: 0
Simple bash script, moving files by criteria


Hi,

i have two folders. One with .torrent files and one with folders(has the same name except the .torrent ending). I'm trying to make a script that moves files/folders if it does not have a matching .torrent in the second folder.

This is what i've got so far:
Code:
#!/bin/bash

sti1=/home/test1
sti2=/home/test2

cd $sti1

for i in $( ls ); do
        if [ `find $sti2 -name "$i.torrent"` -z ]; then
                if [ "$i" != mvScript ]; then
                        mv $sti1/"$i" ../newfolder
                        echo "$i Moved"
                fi
        fi
done
I'm facing a couple of problems here.
  1. If the folder name uses whitespace it splits the folder name in two.
  2. Getting error message(Line 9): unary operator expected

How can i get it to grab the whole folder name regardless of what special characters it's made of. And what is wrong with my test?
Attached Thumbnails
Click image for larger version

Name:	foldertorrent.PNG
Views:	5
Size:	20.0 KB
ID:	8237  

Last edited by str1fe; 10-24-2011 at 11:24 AM.
 
Old 10-24-2011, 09:12 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
1. Do not use ls as it is not required nor a good way to do things.

2. -z would need to be passed to the command which is in this case [ ... i am not aware of it being allowed to be elsewhere

3. Will the .torrent files be at the top level under sti2 or anywhere (hence the find)?

4. If you use globbing the spaces issue should resolve

5. What are the relationships between test1 and test2?

6. The test against mvScript would seem to be at the wrong point as you are already inside the first if, unless of course you have a torrent file called mvScript.torrent?

7. If you use the -v option for mv you will get a better feedback than your echo

8. Your echo assumes everything went ok with the move ... what test do you have to make this assumption?
 
Old 10-24-2011, 09:32 AM   #3
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,429

Rep: Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348
Quote:
If the folder name uses whitespace it splits the folder name in two.
This is a known problem with the use of ls to build a list. Just use
Code:
for i in * ; do
Quote:
Getting error message(Line 9): unary operator expected
You might do better to use a negated test
Code:
fn="$sti2"/"$i".torrent
if [ ! -f $fn ]; then
 
Old 10-24-2011, 10:51 AM   #4
str1fe
LQ Newbie
 
Registered: Jan 2010
Distribution: Cent OS
Posts: 7

Original Poster
Rep: Reputation: 0
I'm getting good results with your if-sentence allend, however, one of the folders in test1 have whitespace in it's name(giving me the error: ./mvScript: line 20: [: /home/test2/lol.hax space.torrent: binary operator expected). Anyone got a solution for this?

Code:
#!/bin/bash
  2
  3 sti1=/home/test1 #The names of thiese folders are just for testing, to make sure it works before i try it on the target folders.
  4 sti2=/home/test2
  5
  6 cd $sti1
 16 for i in "*"; do #I figured i needed the quotation-marks here to catch the whitespace in folder name?
 17         
 18         
 19         fn="$sti2"/"$i".torrent
 20         if [ ! -f $fn ]; then
 21                 #mv -v $sti1/"$i" ../old_downloads #commented while workng on the if test
 22                 echo "$fn"
 23
 24         fi
 25        
 26 done

Last edited by str1fe; 10-24-2011 at 10:59 AM.
 
Old 10-24-2011, 11:02 AM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Look at the rsync program. It does everything you want with one line. Vis: rsync -u --inplace /from/here/ /to/this

See man rsync for syntax and examples.
 
Old 10-24-2011, 11:21 AM   #6
str1fe
LQ Newbie
 
Registered: Jan 2010
Distribution: Cent OS
Posts: 7

Original Poster
Rep: Reputation: 0
I read the rsync manual and i could not understand how that one line could do all i wanted to. I'm gonna try to illustrate to make sure we're on the same page(look at attachment).
Attached Thumbnails
Click image for larger version

Name:	foldertorrent.PNG
Views:	6
Size:	20.0 KB
ID:	8236  
 
Old 10-24-2011, 11:50 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well the first thing i can see is you need to be limiting what is returned to folders, which currently you do not.
I will let PT explain about rsync as I have not used it but do understand its benefits.

Try using something like this:
Code:
#!/bin/bash

sti1=/home/test1 #The names of thiese folders are just for testing, to make sure it works before i try it on the target folders.
sti2=/home/test2

for DIR in $sti1/*
do
    if [[ -d "$DIR" && ! -f "$sti2/${DIR##*/}.torrent" ]]
    then
        echo mv -v "$DIR" ../old_downloads
    fi
done
 
Old 10-24-2011, 12:15 PM   #8
str1fe
LQ Newbie
 
Registered: Jan 2010
Distribution: Cent OS
Posts: 7

Original Poster
Rep: Reputation: 0
Many thanks, it now works! You guys are amazing

Last edited by str1fe; 10-24-2011 at 12:17 PM.
 
Old 10-24-2011, 02:35 PM   #9
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by PTrenholme View Post
Look at the rsync program. It does everything you want with one line. Vis: rsync -u --inplace /from/here/ /to/this
I don't know how rsync can meet this requirement. He wants to move subdirectories * from dir1 to dir3 if *.torrent does not exist in dir2. He's not trying to synchronise or update two directories.

@str1fe: when using globbing, you don't need the quotes around the *.
 
Old 10-24-2011, 07:08 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Not only do you not need quotes around the *, you must not quote it if you want globbing to occur. The quotes disable its special meaning, and all you'll get is a literal asterisk.

globbing is the final step in the parsing process, after all the variables are expanded and word-split, which means that spaces and special characters in the expanded filenames are not a problem.

What you do need to quote are variable expansions, which happen before word-splitting occurs. Any spaces in the filenames will cause the expanded string to be broken up into separate words (occasionally this is desired, but usually not).

Read these three links for details on how the shell processes whitespace:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
  


Reply

Tags
bash


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
Bash Script for Moving X number files from /direct1 to /direct2? Supreme1012 Programming 14 01-30-2010 06:08 PM
Question about moving a range of files with a Bash script surfrock66 Linux - Newbie 6 11-22-2009 04:37 PM
How to write a simple BASH script to "test if have folowing files, than delete."? hocheetiong Programming 10 10-01-2009 01:17 PM
Simple Bash Script to grep for keywords in MySQL dump files eodchop Programming 7 06-05-2009 02:35 AM
Simple console question. Moving files. The_Messiah Linux - General 1 08-17-2005 09:51 PM


All times are GMT -5. The time now is 03:55 PM.

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