Help with copying files using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with copying files using shell script
# 1  
Old 01-12-2013
HP Help with copying files using shell script

I want to write a shell script to copy a list of files from one directory to another. And while copying it should change the first character of the filename to uppercase and others to lowercase.Below is what i have tried so far.

Code:
for file in "$@"
do
  if [ -f $file ]
  then
    ufile=`echo $file | sed -e 's/^./\U&/'`
    cp -v $file $ufile
done

With this code i am able to copy files only within the current directory and the whole filename becomes uppercase.
So how do i copy the files to other directory and how do i make the other alphabets in lowercase.

Can anybody help me out to complete the shell script???

Last edited by Scott; 01-12-2013 at 11:19 AM.. Reason: Code tags
# 2  
Old 01-12-2013
I see your code is good except you are missing a fi
Code:
for file in "$@"
do
  if [ -f $file ]
  then
    ufile=`echo $file | sed -e 's/^./\U&/'`
    cp -v $file $ufile
  fi
done

BTW if you want to copy files to other directories you can append absolute path of destination directory as suffix to variable $ufile.

If you do not have GNU sed then try using awk instead:
Code:
echo $file | awk '{sub(/./, toupper(substr($0,1,1)));}1'

For any issues set xtrace and verbose and post the results:
Code:
/bin/ksh -xv

# 3  
Old 01-12-2013
In bash, you could use "parameter case modification" - no need to resort to sed or awk
Code:
$ for f in file*; do echo $f"  "${f^}; done
file      File
file~     File~
file1     File1
file1~    File1~

# 4  
Old 01-12-2013
Quote:
Originally Posted by RudiC
In bash, you could use "parameter case modification" - no need to resort to sed or awk
Code:
$ for f in file*; do echo $f"  "${f^}; done
file      File
file~     File~
file1     File1
file1~    File1~

I think the requester's system is running on HP-UX and case modification requires BASH version >= 4
# 5  
Old 01-12-2013
Ouch - didn't notice the hp- logo in his header when looking for her/him mentioning system details.
# 6  
Old 01-13-2013
Hi guys. thanks for the reply.
Bt i m a total beginner. I do no know what you mean by "xtrace".
also abt the absolute pathname, i want the script to take input from the user i.e the file to be copied and the destination.
And abt the case,i managed to make the first letter to uppercase bt wat abt the others which have to be in lowercase
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying the files in to multiple location using shell script

Hi All, i'm trying to copy the 1.txt files (sample files) in to different path location using the below command. But it is not copying the files , when i tried for single location able to copy the file. can any one please assist here. Please find the below path :- /ckr_mkr1/licencekey... (2 Replies)
Discussion started by: venkat918
2 Replies

2. Shell Programming and Scripting

Shell script for connecting multiple servers and then copying 30 days old files

Shell script for connecting multiple servers and then copying 30 days old files from those server . HI , I have 6 multiple servers pla1,pla2,pla3,pla4,pla5,pla6 1. These six servers have common shared mount point /var/share 2. Running script from /var/share to connect these servers.I... (1 Reply)
Discussion started by: rcroyal88
1 Replies

3. Shell Programming and Scripting

Need help in finding and copying list of files using bash shell script

Dear All, I have a situation where I want to copy some files of type .txt. These files are o/p from one program. Some of the files are named as fileName .txt instead of fileName.txt after fileName by mistake I have specified "space". Now I want to move these files as follows. mv fileName*... (13 Replies)
Discussion started by: linuxUser_
13 Replies

4. Shell Programming and Scripting

Copying a string from a file using shell script

Hello everyone I am completely new to shell scripting in linux. I wan to write a script to search for a certain string from a .txt file and copy the string which apears just after tat searched string. Eg: in a file- try.txt , we have a line saying: "roses are red, so what do i do" I... (4 Replies)
Discussion started by: Kishore920
4 Replies

5. Shell Programming and Scripting

Files copying - [ Listed files alone. ] - Shell script

Hi All, I am doing this for svn patch making. I got the list of files to make the patch. I have the list in a file with path of all the files. To Do From Directory : /myproject/MainDir To Directory : /myproject/data List of files need to copy is in the file: /myproject/filesList.txt ... (4 Replies)
Discussion started by: linuxadmin
4 Replies

6. Shell Programming and Scripting

Shell script for copying files from 1 server to other

Hi, I just need a shell script that copies a list of files from a directory in a remote server to my current directory at local server the remote server may contain the following list: /root/pradeep/myfiles/default /root/pradeep/myfiles/dir1 /root/pradeep/myfiles/dir2 ...... (1 Reply)
Discussion started by: paddu
1 Replies

7. Shell Programming and Scripting

Shell Script to connect to another server and copying files

Hi Unix Gurus, I have a doubt reg file transfer. I have used the below script to connect to another server and find files having modified for the last 24 hours and have to move the file to another server. While i tried i am getting authentication failed, destination path not found issue. ... (2 Replies)
Discussion started by: incepted
2 Replies

8. Shell Programming and Scripting

Shell script not unzipping and copying correctly

Hi, I am trying to unzip a file( $pfile, it contains a couple of files and 4 folders with subfolders and files) and have its contents go into a directory instead of into a folder in that directory (ZZZZ), I have the following script: #Unzip the build unzip -o "$HOME/ZZZZ/$pfile" -d... (2 Replies)
Discussion started by: SlumberMachine
2 Replies

9. UNIX for Dummies Questions & Answers

Shell Syntax Error when copying files

Hello, I apologize if this was addressed in a previous post. I have done some searching but may have missed it. I am trying to read a list from a file, for example: 3bik 3bix 3biu 3bin 1nwn and using this list, copy files with these names (ex: 3bik.dssp.Z) to a seperate folder,... (1 Reply)
Discussion started by: InfoSeeker
1 Replies

10. Shell Programming and Scripting

copying files and Renaming them + shell script

Hi, I have a problem. I have some text files in a folder. The names can be like: emp.txt emp1.txt emp3.txt 32emp4.txt What i need is i have to copy all the files which have "emp" string in their filename to a different folder and those file names... (7 Replies)
Discussion started by: pathanjalireddy
7 Replies
Login or Register to Ask a Question