Help for shell scripts for moving all files from one directory to another

Hi ,
Can you please check this code .I am getting the follwing error while executing the script.
Please help me out.

#rm /tmp/$$
#!/bin/ksh
dir_one="/usr/bin/sou"
dir_two="usr/bin/sou/temp"
for files in $dir_one/*.txt               
 do                                                                           
 if [ -a $files ]; then                                                                        
    mv $files $dir_two         
 else                                                                         
   echo "no files to move"             
 fi
done

I a m getting the below error done unexcepted

  1. The first line should always be the she-bang line #! /bin/ksh . I see that in your script, it's #rm /tmp/$$

  2. And $$ refers to process ID of the current shell. What're you trying to do here: #rm /tmp/$$ ?

  3. if [ -a $files ] --> What're you trying to do here? -a is binary AND operator.

@balajesuri: -a is also the equivalent of -e in older Korn Shells.

1 Like

Thanks Scrutinizer, that makes sense. I don't have much experience working on ksh. But still [ -a $files ] is an overkill in the script.

@soumyamishra: Try this one-liner:

find /usr/bin/sou/ -maxdepth 1 -name "*.txt" -exec mv {} /usr/bin/sou/temp/ \;

Hi..Thanks all for your help but i need to move both txt files and csv files all together.can you help me .

why not just

mv /usr/bin/sou/*.txt /usr/bin/sou/temp/

This is much lighter.

Thanks for your quick reply.How can i move all the file extension like .txt ,.csv,.pdf and .gz files all together using

find /usr/bin/sou/ -maxdepth 1 -name "*.txt" -exec mv {} /usr/bin/sou/temp/ \;

This will move all files (only files) to temp dir.

find /usr/bin/sou/ -maxdepth 1 -type f -exec  mv {} /usr/bin/sou/temp/ \;

Hi I am getting error while executing

find /usr/bin/sou/ -maxdepth 1 -exec mv {} /usr/bin/sou/temp/ \; 
error :find:bad option -maxdepth.

---------- Post updated at 06:22 AM ---------- Previous update was at 06:20 AM ----------

find /usr/bin/sou/ -maxdepth 1 -type f -exec  mv {} /usr/bin/sou/temp/ \;

i am getting error find bad option -maxdepth

for i in "txt csv pdf gz"
do
mv /usr/bin/sou/*.$i /usr/bin/sou/temp/
done

you can use this loop for any logic.

Thanks vishal.But while executing your loop i got error unexcepeted error 'do

The below simple command will fulfill your needs.?

mv /usr/bin/sou/*.* /usr/bin/sou/temp/

Not possible, it is working for me on my linux system.

$ for i in "txt gz fie"
> do
> echo $i
> done
txt gz fie

can you paste your script or whole command which you are using?

---------- Post updated at 05:20 PM ---------- Previous update was at 05:17 PM ----------

@sanoop, in post #7, soumyamishra is specifically asking about some specific files.

Remove the double quotes and try.
for i in jpg txt csv pdf gz

do
  mv /usr/bin/sou/*.$i /usr/bin/sou/temp/
done

---------- Post updated at 06:55 AM ---------- Previous update was at 06:55 AM ----------

Remove double quote from vishal's script.

Hi,
I am getting same error while executing the same script while executing the script..
i am doing the following step.
save as mv.sh
in mv.sh paste the code for i in jpg txt csv pdf gz
do
mv /usr/bin/sou/*.$i /usr/bin/sou/temp/
done
after that while exeucting mv.sh i am getting the above said error.Please help me out..

I am not sure whats wrong there.!! This is working fine on both solaris and redhat linux.

both in unix it is not working.

Try:

dir_one="/usr/bin/sou"
dir_two="/usr/bin/sou/temp"
for file in *.jpg *.txt *.csv *.pdf *.gz
do
  mv "$dir_one/$file" "$dir_two"
done

Hi.Same error with unexceped syntax error do at line 5.have pasted the same code to mv.sh n den execute it
please help me

Oops I left out a dollar sign, corrected it in the post. please try again..
First put an echo in front of the mv statement to see if it does the right thing.