Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-09-2004, 02:42 PM   #1
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


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...

#! /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
	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

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

Last edited by xushi; 10-09-2004 at 02:44 PM.
Old 10-09-2004, 03:11 PM   #2
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!

Old 10-09-2004, 10:31 PM   #3
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.

        echo "Usage: $0 [directory]"
        exit 1;

test -d "$1" || usage


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"
Old 10-10-2004, 04:15 AM   #4
Senior Member
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45

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
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 ....


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
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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration