Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving fiels in a script 1

Status
Not open for further replies.

westredd

MIS
Jul 22, 2004
260
GB
Hi, im new to linux and scripting in linux. I need help!

Im trying to a very simple task in a script to move 1 file, but cant get it to work. When i use this at the command propmt it works fine, but in a script file I get an error. see below.

My script has the following

#!/bin/sh
mv File2 /etc/Files/

I get the following error:

when moving multiple files last argument must be a directory

I have no spaces in the file. What am I doing wrong.

thanks

West


 
in scripts you should use full path names so the program will work from any current directory. There are many ways to write scripts and everyone does it different.

# before making program, make sure directory /etc/Files is # writable

linux4 :root: [1645]-> cd /etc
linux4 :root: [1646]-> mkdir Files
linux4 :root: [1647]-> cd Files
linux1 :root: [1648]-> pwd
/etc/Files
linux4 :root: [1649]-> cd ..
linux4 :root: [1650]-> ls -l | grep Files
drwxr-xr-x 2 root root 4096 Apr 4 15:48 Files
linux1 :root: [1651]-> chmod 777 Files
linux1 :root: [1652]-> ls -l | grep Files
drwxrwxrwx 2 root root 4096 Apr 4 15:48 Files

I would write it like this:

Code:
# This program will copy arg1 filename to /etc/Files
# directory.
if test "$#" -ne 1
   then
        echo -e "\\nUsage:   copier_to_etcFiles arg1"
        echo -e "Example: copier_to_etcFiles thisfile\\n"
   exit
   fi
if test "$#" -eq 1
   then
        /bin/cp $1 /etc/Files
        echo -e "\\nSuccessfully copied ${PWD}/$1 to /etc/Files"
        echo -e "\\nls -ltr /etc/Files | tail -1"
        ls -ltr /etc/Files | tail -1
        echo -e " "
   exit
   fi

linux4 [1702]-> copier_to_etcFiles

Usage: copier_to_etcFiles arg1
Example: copier_to_etcFiles thisfile

linux4 [1703]-> copier_to_etcFiles modems

Successfully copied /homea/bsh/bin/modems to /etc/Files

ls -ltr /etc/Files | tail -1
-rwxr-xr-x 1 me me 1688 Apr 4 16:08 modems

linux4 [1704]->





A great teacher, does not provide answers, but methods to teach others "How and where to find the answers"

bsh

35 years Bell, AT&T, Lucent, Avaya
Tier 3 for 25 years and counting
 
Thanks a lot of the respnse.
Unfortunatly I dont understand how to use the code in my example.
 
Is a directory?

/etc/Files

Is this a file or a directory?

File2

do

"ls -l /etc/Files"

"ls -l File2"

"ls -l yourProgramName"

if your File2 is in your current directory when you
execute your script, it should work.

paste it back here





A great teacher, does not provide answers, but methods to teach others "How and where to find the answers"

bsh

35 years Bell, AT&T, Lucent, Avaya
Tier 3 for 25 years and counting
 
My script has the following

#!/bin/sh
mv File2 /etc/Files/

If /etc/Files is a directory
and
if File2 is a directory, this will move the directory File2 and its files and subdirectories to /etc/Files

If Files is a file
and File2 is a file
Files will be deleted and replaced with File2

If you can "mv File2 /etc/Files" on the command line,
with correct permissions, it should also work in a shell script.

by permissions I mean, what login you are using and what login owns the directory you are moving files to, and what login owns the file you are trying to move.

/etc files generally belong to "root" and only root can do this without changing permissions and ownership of the source file(s) or directory(s) and also the target
directory(s)

In you example:
File2 is source
/etc/Files is the target





A great teacher, does not provide answers, but methods to teach others "How and where to find the answers"

bsh

35 years Bell, AT&T, Lucent, Avaya
Tier 3 for 25 years and counting
 
thanks again for your repsonse.

to answer some of the questions you asked.

/etc/Files , Files is a directory

files2 is a file.

Im using the root account and the root account created all these objects.

Im running the script from the dir that contains the files2 file.

The mv line in the script works fine from a command. just not in the script.

Thanks
 
cd to the directory with files2

ls -l files2

ls -l /etc | grep Files

cat your_script

paste all this here and maybe we can figure out what is wrong.

A great teacher, does not provide answers, but methods to teach others "How and where to find the answers"

bsh

35 years Bell, AT&T, Lucent, Avaya
Tier 3 for 25 years and counting
 
The original post specified /etc/Files/

Have you tried /etc/Files (ie without the trailing /)?

I want to be good, is that not enough?
 
copy and paste the link rather than click on it, thanks.
 
the problem is the direction of your slash

/ is for linux, solaris unix

\ is for DOS XP and vista


A great teacher, does not provide answers, but methods to teach others "How and where to find the answers"

bsh

35 years Bell, AT&T, Lucent, Avaya
Tier 3 for 25 years and counting
 
god how embarressing.
Every time I typed it here i did it correct and also on the command line. But when I wrote the script I did it wrong and never noticed.

Well, thanks for spotting it, your help was really excellent.
 
A picture is worth a K words



A great teacher, does not provide answers, but methods to teach others "How and where to find the answers"

bsh

35 years Bell, AT&T, Lucent, Avaya
Tier 3 for 25 years and counting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top