LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-01-2007, 02:39 AM   #1
talwar_
LQ Newbie
 
Registered: Mar 2007
Posts: 3

Rep: Reputation: 0
Question Need bash script to list files, drop extension and dump to file


Hi all,

I'm trying to write an elegant bash script to list files in a specified directory with a particular extension, drop the extension and output to a file.

The directory name should be hard coded within the script, because the script will not reside in that directory.

So lets say my folder contains the following files:

a.todump
b.todump
c.todump
other.ext
other2.ext


The script should dump only files with .todump extension, but must drop the extension. The contents of the dumped file should be:

a
b
c


ive been trying for a little while but been unsuccessful as im not experienced with shell scripts and havent been using linux long enough to know all the useful shell commands. It should be pretty simple though. Thanks in advance for your assistance!
 
Old 06-01-2007, 02:51 AM   #2
flupke
Member
 
Registered: Jun 2005
Location: BE
Posts: 30

Rep: Reputation: 15
This should do the trick :
Code:
#!/bin/sh

DIR="/your/directory/here"
SUFFIX="todump"

for i in "$DIR"/*.$SUFFIX
do
    echo ${i%%.$SUFFIX} |sed 's#^.*/##'
done
HTH
Flupke
 
Old 06-01-2007, 03:00 AM   #3
talwar_
LQ Newbie
 
Registered: Mar 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Hey flupke,

thanks for the prompt reply! will test it out when i have a chance
 
Old 06-01-2007, 03:01 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
for file in *todump; do  mv $file ${file%.todump}; done
 
Old 06-02-2007, 05:48 AM   #5
talwar_
LQ Newbie
 
Registered: Mar 2007
Posts: 3

Original Poster
Rep: Reputation: 0
ok.. flupke's post works great. ghostdog, i think you misunderstood me as your code is going to rename my files.. thats not i want. only wanted them renamed in the dumped file.
 
Old 11-04-2010, 10:33 PM   #6
paisal
LQ Newbie
 
Registered: Nov 2010
Posts: 1

Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by flupke View Post
This should do the trick :
Code:
#!/bin/sh

DIR="/your/directory/here"
SUFFIX="todump"

for i in "$DIR"/*.$SUFFIX
do
    echo ${i%%.$SUFFIX} |sed 's#^.*/##'
done
HTH
Flupke
Thank Flupke is work for me
 
Old 05-25-2011, 12:53 PM   #7
andekoppen
LQ Newbie
 
Registered: May 2011
Posts: 1

Rep: Reputation: Disabled
Along the same lines ...

Hi

I realize that this is properly a "dead" thread but I thought I'd give it a go.

I'm trying to create a script, that's quite similar to the one presented by ghostdog74, but I can't get it to work.

The script is:
#!/bin/bash

NAVN="prescript - "

for i in "$NAVN"*
do
mv $i ${i#NAVN}
done

The difference being that I want to remove the prescript of some files rather than the extension, but when I run it, i get the error:
"mv: the target 'A' is not a catalog "
(The error is in Danish so it might not be the same wording in the English error, but this is the literal translation).

What am I doing wrong?
 
Old 05-25-2011, 02:11 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Please start your own thread. It might be helpful to add set -xv to your script to see what it is doing -- or put an echo before the mv.
 
Old 06-01-2011, 09:13 AM   #9
martaskolda
LQ Newbie
 
Registered: Jun 2011
Posts: 1

Rep: Reputation: Disabled
Did you say ELEGANT? You don't need loops, just do
Code:
ls *.todump | sed 's/.todump//g'
 
Old 06-02-2011, 12:44 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
There's a couple of nice Linux cmds for separating filenames from dir names:
http://linux.die.net/man/1/basename http://linux.die.net/man/1/dirname
The first will also separate out file extension strings
 
Old 06-03-2011, 09:18 AM   #11
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by andekoppen View Post
The script is:
#!/bin/bash

NAVN="prescript - "

for i in "$NAVN"*
do
mv $i ${i#NAVN}
done
Are there real blanks in the filenames like you mention in "prescript - "? Then the mv will also need quotes around each argument, so that they are still seen as one argument despite the multiple blanks which would separate different arguments otherwise.

And you need a second $:
Code:
mv "$i" "${i#$NAVN}"
 
  


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
script for deleting certain file extension wfischer Linux - Software 3 03-03-2007 05:36 PM
Bash script: symbolic linking all files with same extension in a directory Katachi Programming 1 12-25-2006 09:42 AM
bash script to sort files by extension otheralex Programming 7 08-19-2005 02:40 AM
bash script to set extension association vi0lat0r Programming 6 05-24-2004 07:04 PM
bash scripting - largest # file extension brian0918 Programming 3 06-16-2003 12:06 PM

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

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