LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2007, 01:22 PM   #1
Simon_6162
Member
 
Registered: Jul 2004
Location: UK
Distribution: Fedora, Gentoo, Redhat Enterprise
Posts: 141

Rep: Reputation: 16
Question bash script, for all files in folder do ............


Hi I have a large number of files in a folder four example names are below.

This is a really long file name for a test 001.csv
This is a really long file name for a test 002.csv
This is a really long file name for a test 003.csv
This is a really long file name for a test 004.csv

I wanted to copy all the files in the current folder via ftp and rename them all to just the 001.csv 002.csv etc however if I do for files in `ls` the files names are split on the spaces in the names so $ file reads
this
is
a
really etc.

The solution I found was to do a string replace on the ls first using sed, which works, and then switch it back in the for loop but I'm just curious if there is a better solution to this.

This is the code I have:

Code:
#!/bin/bash

#copy files to the remote pc
for file in `ls *.csv | sed 's/ /~/g' `
do
	file1=`echo $file | sed 's/~/ /g'`
	file2=`echo $file1 | cut -c 44-`
	echo "copying file $file2"
	curl -T "$file1" -u uname:passwd ftp://192.168.5.230/"$file2"
done
so is it possible to not use sed ?
 
Old 07-18-2007, 01:37 PM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
An easier way is to override the IFS variable - it's what tells bash how to separate values, and it default to containing the space character, and -then- newlines.

See here for more info

Just be sure to back it up, first --

Code:
tempvar=$IFS
IFS="
"

...stuff...

IFS=$tempvar
exit
 
Old 07-18-2007, 02:40 PM   #3
Simon_6162
Member
 
Registered: Jul 2004
Location: UK
Distribution: Fedora, Gentoo, Redhat Enterprise
Posts: 141

Original Poster
Rep: Reputation: 16
Thanks for your help that was what I was after. I obviously didn't look hard enough.

Simon
 
  


Reply



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
how to tell bash to move files to another folder? hq4ever Linux - Newbie 10 12-30-2010 03:15 AM
BASH Shell script : copying a file to multiple folder zamri Programming 14 04-29-2008 10:27 AM
Script that checks for new files in a folder achtung_linux Programming 7 02-03-2007 09:18 AM
need a script to delete all files from a folder and subfolders cccc Programming 1 03-04-2005 10:54 AM
How to copy in a script files to a samba folder? Julianus Linux - Networking 1 10-09-2004 07:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:00 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
Open Source Consulting | Domain Registration