LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   renaming files script. (https://www.linuxquestions.org/questions/programming-9/renaming-files-script-240668/)

xushi 10-09-2004 02:42 PM

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

utopicdog 10-09-2004 03:11 PM

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

homey 10-09-2004 10:31 PM

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


xushi 10-10-2004 04:15 AM

Thanks!

That actually does the job well, ecept that it doesn't like when i use ' [ or ] ' brackets in the name of a file.

homey 10-10-2004 08:06 AM

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.


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