Removing Extensions In Subdirectories
Solution 1:
A Python script to do the same:
import os.path, shutil
def remove_ext(arg, dirname, fnames):
argfiles = (os.path.join(dirname, f) for f in fnames if f.endswith(arg))
for f in argfiles:
shutil.move(f, f[:-len(arg)])
os.path.walk('/some/path', remove_ext, '.tex')
Solution 2:
One way, not necessarily the fastest (but at least the quickest developed):
pax> for i in *.c */*.c */*/*.c ; do
...> j=$(echo"$i" | sed 's/\.c$//')
...> echomv"$i""$j"
...> done
It's equivalent since your maxdepth is 2. The script is just echoing the mv
command at the moment (for test purposes) and working on C files (since I had no tex
files to test with).
Or, you can use find with all its power thus:
pax> find . -maxdepth 2 -name '*.tex' | whileread line ; do
...> j=$(echo"$line" | sed 's/\.tex$//')
...> mv"$line""$j"
...> done
Solution 3:
Using "for i in" may cause "too many parameters" errrors
A better approach is to pipe find onto the next process.
Example:
find . -type f -name "*.tex" | whileread file
domv$file${file%%tex}g
done
(Note: Wont handle files with spaces)
Solution 4:
Using bash
, find
and mv
from your base directory.
for i in $(find . -type f -maxdepth 2 -name "*.tex");
domv$i $(echo"$i" | sed 's|.tex$||');
done
Variation 2 based on other answers here.
find . -type f -maxdepth 2 -name "*.tex" | whileread line;
domv"$line""${line%%.tex}";
done
PS: I did not get the part about escaping '.
' by pax
...
Solution 5:
There's an excellent Perl rename script that ships with some distributions, and otherwise you can find it on the web. (I'm not sure where it resides officially, but this is it). Check if your rename was written by Larry Wall (AUTHOR section of man rename
). It will let you do something like:
find . [-maxdepth 2] -name "*.tex" -exec rename's/\.tex//''{}' \;
Using -exec is simplest here because there's only one action to perform, and it's not too expensive to invoke rename multiple times. If you need to do multiple things, use the "while read" form:
find . [-maxdepth 2] -name "*.tex" | whileread texfile; do rename 's/\.tex//'$texfile; done
If you have something you want to invoke only once:
find . [-maxdepth 2] -name "*.tex" | xargs rename's/\.tex//'
That last one makes clear how useful rename is - if everything's already in the same place, you've got a quick regexp renamer.
Post a Comment for "Removing Extensions In Subdirectories"