LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 01-25-2005, 02:07 PM   #1
mdm_linux
LQ Newbie
 
Registered: Jan 2005
Posts: 16

Rep: Reputation: 0
Question bash / ls question


hi,

i'm trying to write a small script to operate on the following file names,

mm@pp:~/mp3/Dropkick Murphys$ ls -al
total 28160
drwxr-xr-x 2 mm mm 376 Jan 25 14:23 .
drwxr-xr-x 30 mm mm 1760 Jan 25 14:23 ..
-rw-r--r-- 1 mm mm 8023997 Jan 25 14:15 Dropkick Murphy_s - Kiss Me I_m Shitfaced.mp3.OK
-rw-r--r-- 1 mm mm 5095885 Jan 25 14:21 Dropkick Murphys - Sing Loud, Sing Proud - The New American Way.mp3.OK
-rw-r--r-- 1 mm mm 5252045 Jan 25 14:16 Dropkick Murphys - The Dirty Glass.mp3.OK
-rw-r--r-- 1 mm mm 5325017 Jan 25 14:16 Dropkick Murphys - Walk Away.mp3.OK
-rw-r--r-- 1 mm mm 5099072 Jan 25 14:15 Dropkick Murphys - Workers song.mp3.OK


i want to strip the .OK off the ends.
i thought of trying this:

for i in `ls` ; do move $i `echo $i | sed -e 's/.OK//g'`; done

unfortunately, because of the spaces, this method won't work. $i 's are populated with individual words, not the complete filenames. additionally, ls -b doesn't fix this. here's an example:

mm@pp:~/mp3/Dropkick Murphys$ for i in `ls -b`; do echo $i ; done
Dropkick\
Murphy_s\
-\
Kiss\
Me\
I_m\
Shitfaced.mp3.OK
Dropkick\
Murphys\
-\
Sing\
...


is there a standard way for dealing with this?
thanks!!! m
 
Old 01-25-2005, 02:15 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
Re: bash / ls question

Quote:
Originally posted by mdm_linux
for i in `ls` ; do move $i `echo $i | sed -e 's/.OK//g'`; done
Try with quotes...
Code:
for i in "`ls *OK`" ; do mv "$i" "`echo $i | sed -e 's/.OK//g'`"; done

Cheers,
Tink
 
Old 01-25-2005, 02:32 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Code:
#!/bin/bash
for i in `ls /home/images/*.OK` ;do
   j=`echo $i |sed -e 's/.\{3\}$//g'`
   mv $i $j
done
note to self: must type faster
 
Old 01-25-2005, 03:11 PM   #4
mdm_linux
LQ Newbie
 
Registered: Jan 2005
Posts: 16

Original Poster
Rep: Reputation: 0
the double quotes are definitely necessary. i tried your regexp replacement and it didn't work (what does it stand for anyway?) i also tried s/\.OK//g with the same error.

mm@pp:~/mp3/Dropkick Murphys$ for i in "`ls`"; do j=`echo "$i" | sed -e 's/.\{3\}$//g'` ; mv $i $j ; done
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
 
Old 01-25-2005, 03:44 PM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Ok, might have to modify the approach some because you have spaces in the file names.
sed -e 's/.\{3\}$//g'` This part just says to remove the last 3 digits.
Code:
#!/bin/bash

cd /home/images
for i in *.OK ;do
j=`echo $i |sed -e 's/.\{3\}$//g'`

echo mv "/home/images/$i" "/home/images/$j"
done

Last edited by homey; 01-25-2005 at 03:50 PM.
 
Old 01-25-2005, 04:26 PM   #6
mdm_linux
LQ Newbie
 
Registered: Jan 2005
Posts: 16

Original Poster
Rep: Reputation: 0
hmmm...

this worked:
for i in *.OK ; do j=`echo $i | sed -e 's/\.OK//g'`; mv "$i" "$j" ; done

this didn't:
for i in "`ls`"; do j=`echo "$i" | sed -e 's/\.OK//g'` ; mv "$i" "$j" ; done
and this didn't
for i in `ls`; do j=`echo "$i" | sed -e 's/\.OK//g'` ; mv "$i" "$j" ; done

someday i'll understand this cruft
thanks for the tips!
 
Old 01-25-2005, 04:38 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
Quote:
Originally posted by mdm_linux
someday i'll understand this cruft
thanks for the tips!
Tsk tsk tsk ...
bash != cruft

windows == cruft
;)

Cheers,
Tink
 
Old 01-27-2005, 09:49 PM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
An alternative method:
for file in *.mp3.OK; do mv "${file}" "${file%.OK}"; done

This should be faster because you aren't dispatching another shell to execute a command.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about BASH operand Linux - Newbie 2 11-27-2005 09:49 PM
BASH question robscott Linux - Software 3 11-23-2005 09:39 AM
Bash question lnxduck Programming 3 09-30-2005 08:50 AM
bash question? shanenin Linux - Software 1 11-26-2004 12:22 PM
little bash question gplacek Linux - General 2 09-01-2004 12:10 PM


All times are GMT -5. The time now is 08:36 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration