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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in finding and copying list of files using bash shell script
# 1  
Old 09-14-2014
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
Code:
fileName .txt

instead of
Code:
fileName.txt

after fileName by mistake I have specified "space". Now I want to move these files as follows.
Code:
mv fileName* fileName.txt

But situation is not that straight forward. I have many files with file names
Code:
fileName1
fileName2
fileName3
:
:

also many other files with other names as well

I cant re-run my program. So, I taught shell script will be best option to save time.

Any help is very thankful Smilie

Thanks & Regards,
linuxUser_
# 2  
Old 09-14-2014
One way with awk:
Code:
ls fileName* | awk '/ /{f=$0; sub(" ",x);system("mv \""f "\" "$0)}'

Try this first to see if it moves the correct files:
Code:
ls fileName* | awk '/ /{f=$0; sub(" ",x);print "mv \""f "\" "$0}'

# 3  
Old 09-14-2014
Quote:
Originally Posted by Franklin52
One way with awk:
Code:
ls fileName* | awk '/ /{f=$0; sub(" ",x);system("mv \""f "\" "$0)}'

Try this first to see if it moves the correct files:
Code:
ls fileName* | awk '/ /{f=$0; sub(" ",x);print "mv \""f "\" "$0}'

Dear Franklin,

Thanks for the reply. I have one very quick question.
I want to move different files like
Code:
abc.txt, def.txt, ghi.txt etc.

.
Your code may be helpful in moving files like
Code:
fileName1.txt, fileName2.txt etc

So irrespective of file name I want to move all the files by just removing space only as I mentioned in my 1st post.


I am unable to move files using that code Smilie

Thanks & Regards,
linuxUser_
# 4  
Old 09-14-2014
replace
Code:
filename*

by
Code:
*

and
Code:
sub

by
Code:
gsub

if you want replace more all space by nothing
# 5  
Old 09-14-2014
Quote:
Originally Posted by protocomm
replace
Code:
filename*

by
Code:
*

and
Code:
sub

by
Code:
gsub

if you want replace more all space by nothing
Still there is something wrong. I am unable to move files with these changes :-|

---------- Post updated at 08:02 PM ---------- Previous update was at 07:52 PM ----------

Dear All,

May be it is not space.
When I see these file names in terminal it is something showing like this
Code:
fileName?.txt

when I see these files in directory, there is a line between fileName and .txt.
It is showing like
fileName
.txt

I suspect may be its not space. Don't know what it is exactly even Smilie
# 6  
Old 09-14-2014
Quote:
Originally Posted by linuxUser_
Still there is something wrong. I am unable to move files with these changes :-|

---------- Post updated at 08:02 PM ---------- Previous update was at 07:52 PM ----------

Dear All,

May be it is not space.
When I see these file names in terminal it is something showing like this
Code:
fileName?.txt

when I see these files in directory, there is a line between fileName and .txt.
It is showing like
fileName
.txt

I suspect may be its not space. Don't know what it is exactly even Smilie
i don't understand, it's work for me

Code:
ls pathOfYourFolder/* | commandAWK

# 7  
Old 09-14-2014
Result for
Code:
ls pathToFiles

Code:
/home/linuxUser/case/files/testRename/pbar?.stl
/home/linuxUser/case/files/testRename/tk?.stl
/home/linuxUser/case/files/testRename/mbyv?.stl
/home/linuxUser/case/files/testRename/mvFiles
/home/linuxUser/case/files/testRename/vm3?.stl
/home/linuxUser/case/files/testRename/mk?.stl

I didn't understand why this question mark.
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

Copying large files in a bash script stops execution

Hello, I'm new to this forum and like to first of all say hello to everyone. I've got a really annoying problem at the moment. I'm trying to rsync some files (about 200MB with one file of 120MB) from a Raspberry PI with raspbian to a debian server via rsync. This procedure is stored in a... (3 Replies)
Discussion started by: wex_storm
3 Replies

3. Shell Programming and Scripting

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. for file in "$@" do if then ufile=`echo $file | sed... (5 Replies)
Discussion started by: vishal.desai
5 Replies

4. 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

5. Shell Programming and Scripting

Finding & Copying files

Hello :) can someone please help me with this task: I am in the shell, in folder "Main" below the folder Main are sub folders: "sourceA", "Source B", and "target" in sourceA and source B are files, and in folder "Main" is a textfile, with filenames, one filename per line. I need a... (4 Replies)
Discussion started by: Y-T
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

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

9. UNIX for Dummies Questions & Answers

finding and copying files !

Hi , I have a question relating to finding and copying files. i need to find the .pdf files from the specified directory which has subdirectories too. I only need .pdf files and not the directories and need to copy those files into my current directory. copy files from :... (5 Replies)
Discussion started by: bregoty
5 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