Re: filename renaming

Woogie <[email protected]>
Newsgroups gmane.user-groups.linux.ottawa.general
Message-ID <CAJaTAYm58DS_rXeR7mKtx9uYZ_sR93y3V6_LZyp480ji7edoGA@mail.gmail.com>
Using this bit of bash and sed worked for me. It might choke on funky
characters, though

I used "Pictures" as my example directory:

for x in `ls Pictures/*` ; do
DIR_NAME=$(echo $x | sed -e 's|\([^/]*\)/.*|\1|')
FILE_NAME=$(echo $x | sed -e 's|[^/]*/\(.*\)|\1')
echo "${DIR_NAME}_${FILE_NAME}"
done

The first line iterates over all the file names in "Pictures" in the
form "Pictures/filename.jpg"
The second line uses sed to assign the variable DIR_NAME the name of
the directory
The third line uses sed to assign the variable FILE_NAME the name of the file
The fourth line just echoes the form of the filename you wanted. You'd
replace this line with a cp or mv, at your leisure. Remember, $x holds
the path to the file
The fifth line terminates the loop

This is quick and dirty; odds are there are programs out there you
could chain together to get the same results without resorting to
using sed.

Woogie

On Tue, Sep 20, 2011 at 4:38 PM, Normand Fisher <[email protected]> wrote:
> I'd like to have a script to rename groups of files by including the
> directory name in the revised filename.
>
> Ex:  in directory "A", I have files 1, 2, 3, 4, and 5.
> I would like to rename the files to A_1, A_2, A_3, A_4 and A_5.
>
> To do this, I have so far used the "mmv" command but I still have to
> type in the directory name for each batch.  Is there a way to capture
> the directory name and place it in a variable?
> I have looked at "pwd" but that gives me the whole path.
>
> I don't mind using command line but I am not a programmer.  Any
> suggestion is welcome.
>
> Normand
> --
> OCLUG general discussion list
> [email protected]
> http://oclug.on.ca/mailman/listinfo/oclug
>



-- 
Evil will always triumph, because good is dumb
-- 
OCLUG general discussion list
[email protected]
http://oclug.on.ca/mailman/listinfo/oclug
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.