LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 03-22-2012, 10:39 PM   #1
marcusrex
Member
 
Registered: Mar 2012
Posts: 36

Rep: Reputation: Disabled
A question about file manipulation from the command-line


Hello!

My name is Mark and this is my second post to Linux Questions. I am not an utter newbie to the Unix command-line, however I've never really done anything more sophisticated than moving and copying files, and moving and copying folders. Recently at work, I wanted to perform batch conversion of a long list of files from one file extension type to another.

I searched the web and found this link (http://lab.artlung.com/unix-batch-file-rename/). I had to go about halfway down the page to find information that finally helped me; much of what was posted there didn't work on my system. Specifically, here is the text from the link that ultimately helped me.

>In general, if you want to add a suffix to your files, do this (.txt in this example):
>
>ls
>file1 file2 file3
>
>for i in *; do mv $i $i.txt; done
>
>ls
>file1.txt file2.txt file3.txt
>
>If you want to take it back off (.txt in this example again)
>
>for i in *.txt; do mv $i ${i%.*}; done
>
>ls
>file1 file2 file3

I would like to learn more about this but have no idea how to search for more information. Here is my question. What is this text (for i in *; do mv $i $i.txt; done) and this text (for i in *.txt; do mv $i ${i%.*}; done) called? In other words, when I type that text into the command-line, what is the name of the activity I am performing? Is it called shell scripting? Programming? Mucking about? The sequence of commands in those two texts reduced a job that would have taken hours manually into a nearly instantaneous process and I would really like to learn more about that.

Thank you for your help!

Mark

P.S. It would be great if you could recommend books or other reference materials I should seek out. In other words I would like to RTFM but I don't know what FM to R.
 
Old 03-22-2012, 10:48 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally Posted by marcusrex
What is this text (for i in *; do mv $i $i.txt; done) and this text (for i in *.txt; do mv $i ${i%.*}; done) called?
It's "called" entering a command. I'm not trying to sound like an arse, but that's really what it is. Some might call it shell scripting, but to call it shell scripting would also require that same person to call a simple "ls" shell scripting as well.

Here's the thing, a shell script is just a sequence of commands. The stuff you quoted are commands. They are "loops" but loops are commands just as well as anything else.

Anyway, I don't want to get distracted on a philosophical and ultimately impractical discussion.

While those are commands, they are first steps toward shell scripting. The usual link for shell scripting is:
The Advanced Bash Scripting Guide

Don't let the "advanced" discourage you. Just dive into it.
 
Old 03-22-2012, 11:24 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
See also
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

I'd say 'shell scripting' if you put those cmds into a shell file ie script (file) called eg myscript.sh, something like
Code:
#!/bin/bash

for i in *; do mv $i $i.txt; done

#  OR, for more complex work, put each cmd on a separate
# line so you can add extra lines in between (no line nums for shell )
for i in *
do 
   mv $i $i.txt
done
Everything between 'do' & 'done' is repeated as often as dictated by the 'for ...' line.
Note also that end-of-line is sufficient to be end-of-cmd; you only need ';' to separate
multiple cmds on the same line to make the parser's life easier
 
Old 03-23-2012, 10:39 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


Congratulations on starting out with scripting!

Of course the specific action you've decided to start with has been covered many times, but we all have to start somewhere.


To start with, here are a few useful bash scripting references:
http://mywiki.wooledge.org/BashGuide
http://www.linuxcommand.org/index.php
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
http://ss64.com/bash/

The first link in particular should cover all the basic concepts you need to know to understand your script.

The specific structure you asked about is simply called a "for loop" by the way, and one of the most commonly-used bits of shell syntax.


However, there is one very important issue with the above commands that needs to be addressed right now:

QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

You'll experience failures with the above loops as soon as you hit a file name with spaces in it. So double-quote the "$variables" to keep the parts of the names together.



Have fun scripting!

Last edited by David the H.; 03-23-2012 at 10:47 AM. Reason: minor adjustments
 
Old 03-23-2012, 11:07 AM   #5
marcusrex
Member
 
Registered: Mar 2012
Posts: 36

Original Poster
Rep: Reputation: Disabled
Hello,

I have learned that proper LQ ettiquette is to post a follow up message if your post was adequately addressed. My post was more than adequately addressed.
Thank you Dark Helmet, chrism01, and David the H., you've provided me with exactly the information I sought.

Sincerely,

Mark
 
  


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
File manipulation question shoemoodoshaloo Linux - Newbie 3 06-09-2009 04:57 PM
LXer: Flimp - Graphical frontend for Command Line Image Manipulation tools LXer Syndicated Linux News 0 10-15-2008 07:00 AM
command line image manipulation (imagemagick?) exodist Linux - Software 3 03-13-2006 01:35 PM
Hardware Manipulation From Command Line LouisTheDamned Linux - Newbie 2 03-27-2004 03:42 PM
Yep, another PHP question (File manipulation) jacksmash Programming 6 11-22-2003 01:07 PM

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

All times are GMT -5. The time now is 06:28 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