LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-31-2017, 03:15 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,052

Rep: Reputation: 78
understand IFS change for looping files with spaces


The other day I was trying to loop some files that contained spaces. I was trying to delete the same string that appeared at the beginning of several files using substring removal.
Suppose the filename is "First - Second - file.txt" and I'd like to remove "First - Second - " so that only "file.txt" remains.
So I tried:
Code:
for i in First*; do mv $i ${i##* }; done.
But it didn't work and then someone told me that I needed to change the IFS. Which I did, following these instructions: https://www.cyberciti.biz/tips/handl...s-in-bash.html

And it worked just fine.
The site suggests using IFS=$(echo -en "\n\b")

Could anyone explain to me exactly what is going on? How does bash process the loop and why does one need a backspace after a newline? I really don't understand the mechanism, even if it would seem rather basic.
 
Old 01-31-2017, 05:41 AM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,336

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
You need to quote the variables,
Code:
for i in First*; do mv "$i" "${i##* }"; done
otherwise the command expands to
Code:
mv First - Second - file.txt file.txt
instead of
Code:
mv "First - Second - file.txt" "file.txt"
as you want.
 
1 members found this post helpful.
Old 01-31-2017, 07:36 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,764

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
Above is solution, but if I may, until you are 100%, ALWAYS quote variables in bash.
 
1 members found this post helpful.
Old 01-31-2017, 10:18 AM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,052

Original Poster
Rep: Reputation: 78
I suppose that's much easier. Thanks
 
Old 01-31-2017, 10:56 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,764

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
Please mark as SOLVED once you have a solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Looping a script on 700+ files? chris_crunch Linux - Newbie 3 05-16-2016 11:14 PM
[SOLVED] Problems looping on .csv files eudald Linux - Newbie 6 06-25-2014 06:30 AM
looping through files to search for given substrings kabester LinuxQuestions.org Member Success Stories 1 08-02-2012 04:20 PM
Bash: when an empty IFS does not work like a default IFS (info) catkin Programming 13 04-19-2012 10:40 AM
looping trough files BackwardsDown Programming 4 02-09-2007 11:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration