I have a list of folders for example each folder name:
- pippo1 file pippo1 .txt,
- With the pippo2 file pippo2.txt,
- Pippo3 file to Pippo3.txt etc.
I want to go to the first folder, remove the number from the file name pippo1.txt, and then exit, go to another folder which named pippo2 a file named PPO2.txt, 2 file name Remove etc. I tried the following code, but it does not work! The code is:
i in * for CD $ i ls * .txt. Sed -e "s / [0-9] // g" cd .. done
What am I doing?
If the requirements are only so simple, then you can do this only with bash replacements:
Find in file -name 'Pippo * .txt'`; MV $ file $ {file / [0-9] .txt / .txt}
However, this file will fail more than single digits in the file name, e.g.
pippo999 / pippo999.txt . It can be addressed by setting the
extglob option:
to find the file in the extnglob of shopt -s. -name 'pippo * .txt'`; MV $ file $ {file / + ([0-9]). Txt / .txt} done
Bash file globing created in + (...) is available only when
extglob is set it happens.
Instead of carrying the directory tree with the loop and
cd , it uses
to get the list of all related files, then Lose the list of file names that will be of the form:
.pippoN / pippoN.txt . Rename uses Bash to get the modified file name. See also.
Note that the blank space in the file or directory names will break this approach , using the
search : there is a way in which the blank Locations will be split into many tokens around empty spaces, each token list has a different result in the iterator. So if you need to expect space characters in paths, then it becomes a bit more complex:
shopt -s extglob IFS = $ '\ n' Find 'in the file - Name 'Pippo * .txt'`; "MAV" $ $ file "" $ {file / + ([0- 9]). Here we have cited the file name for the
MV command, which will handle the empty space properly, and we have the field separator Has been changed to
IFS so that Bash can not recognize white space as a word boundary. This loop will fix the problem with
find in the iterator Now empty in the path The location will be handled correctly, so that the name of
./ pippo 9 / pippo9.txt will also be changed:
./pippo 9/pippo.txt .
Comments
Post a Comment