LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-09-2004, 02:42 PM   #1
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Rep: Reputation: 45
renaming files script.


Lo all,

How can i create a simple bash script to cut out a small part (eg. a word) of a name of a file, the same word on several files?
In more detail, i have several files (of same extension), which start with

name_foo.txt
name_blah.txt
name_fart.txt

or sometimes
something [lol].zip
jack [lol].zip

I'm assuming the program has to ask me for the common word, search a folder, display the files with the common word, and ask me to delete it...

I've managed to write up 2 functions to get the location and search for file names with that word respectively, but can't quite figure out how to delete that word from the file names...

Code:
#! /bin/bash

# This is a trap, to catch signals
# particularely ctrl^c
trap 'echo "Force Quit."; exit 1' 2

# Ask for the directory
function askForDir
{
	# Ask user for the location of the file
	echo -n "Location of Directory (leave empty for current): "
	read myLocation

	# If no input then take default source location
	if [ "$myLocation" = "" ]; then
		pwd > location
		myLocation="$location"
	fi
	
	echo "Location: $myLocation"
}

# Search for the word you want.
function searchForWord
{
	echo -n "Enter Word: "
	read myWord
	echo "Files with '$myWord' are: "
	ls $myLocation | grep $myWord
	read temp
}

askForDir
searchForWord
Note: default source location section doesn't work atm...
EDIT: heh.. apparently it *does* work =)
Thanks

Last edited by xushi; 10-09-2004 at 02:44 PM.
 
Old 10-09-2004, 03:11 PM   #2
utopicdog
Member
 
Registered: Aug 2003
Location: france
Distribution: suse/fedora
Posts: 105

Rep: Reputation: 15
i know what you mean, it should be simple but it isn't.

find -name "*zz*"

will give you all files with zz anywhere in the filename.
after that you pipe the result into xargs

find -name "*zz*" | xargs -i abc rm abc

will remove all files with zz in the filename.
to change the filename, use gawk instead of rm, or write a small script/C program.

bon courage!

utopicdog
 
Old 10-09-2004, 10:31 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
How about something like this....
just make the script executable and run it like this: ./test /home/dir or whatever directory you have in mind.

In this example, it sort of assumes the word to be removed is at the beginning of the file name. You can change the sed part to put the word at the end like this....
j=`echo -n "$i" | sed -e 's/\(.*\)'$word'/\1/g'`;

Also, the move statement has echo in front so you can test it with no harm done.

Code:
#!/bin/sh
#
usage()
{
        echo "Usage: $0 [directory]"
        exit 1;
}

test -d "$1" || usage

dir="$1"

echo -n "Enter Word: "
read word
ls $dir | grep -e $word | \
while read i; do
j=`echo -n "$i" | sed -e 's/'$word'\(.*\)/\1/g'`;
echo mv "$dir/$i" "$dir/$j"
done
 
Old 10-10-2004, 04:15 AM   #4
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Thanks!

That actually does the job well, ecept that it doesn't like when i use ' [ or ] ' brackets in the name of a file.
 
Old 10-10-2004, 08:06 AM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here's a hack for specical characters in the search word. In the example you gave, we know there is one special character before and one after the word ...
something [lol].zip
jack [lol].zip

so we can use a one charater place holder, the period ( . ) before the word and one after the word ....
.lol.
 
  


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
shell renaming script paskari Programming 3 09-24-2005 02:15 PM
Simple Renaming Script Boffy Programming 5 08-12-2005 09:07 AM
directory renaming shell script? Apostasy Linux - General 15 02-22-2005 06:50 AM
Need help with shell script - renaming multiple files NiallC Linux - Newbie 25 07-04-2004 10:45 AM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM

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

All times are GMT -5. The time now is 09:07 AM.

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