English 中文(简体)
Strip Characters Before Period If Filename Has Prefix in Bash
原标题:

I have a directory that looks like this:

pages/
    folder1/
        folder1.filename1.txt
        folder1.filename2.txt
    folder2/
        folder2.filename4.txt
        folder2.filename5.txt
    folder3/
        filename6.txt

I want it to look like this:

pages/
    folder1/
        filename1.txt
        filename2.txt
    folder2/
        filename3.txt
        filename4.txt
    folder3/
        filename5.txt

With ls * | sed -e s/^[^.]*.// > /tmp/filenames.txt I get a file containing:

filename1.txt
filename2.txt
filename3.txt
filename4.txt
txt

How can I tell sed to ignore filenames of the form [filename].[suffix] and only look at filenames of the form [foldername].[filename].[suffix]?

The final script (as pointed out, the find command would simplify things, but this worked):

for folder in $(ls .)
do
    if test -d $folder
    then
        pushd $folder
                ls * | sed  s/.*.(.*..*)/1/  > /tmp/filenames.txt
                ls * > /tmp/current.txt

                exec 3</tmp/current.txt
                exec 4</tmp/filenames.txt

                while read file <&3; read name <&4;
                do
                    mv "$file" "$name"
                done

                rm /tmp/current.txt
                rm /tmp/filenames.txt
        popd
    else
        echo $folder "not a directory"
    fi
done

exit 0

This page is now a community wiki. You can add more elegant solutions below:

for folder in $(ls .)
do 
     something better
最佳回答

Give this a try:

sed  s/.*.(.*..*)/1/ 

You should really use find then you wouldn t need the check for "-d folder" or the temp file and execs or the while loop.

You can avoid the temporary file by using process substition:

while read line
do
    echo $line
done < <(ls)

Another item of interest: your system may already have a Perl script called rename or prename which will rename files using a regular expression.

问题回答

You don t need to use sed:

ls * > /tmp/current.txt
exec 3</tmp/current.txt
while read file <&3;
do
    replacement=${file#${folder}.}
    if [ "$replacement" != "txt" ] ; then
        mv "$file" "$replacement"
    fi
done

use the following regex:

/A(.*?\.){2,2}.+/




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签