Jak zmienić nazwy plików w podkatalogach?

Czy istnieje możliwość zmiany nazw plików w podkatalogach?

Na przykład:

Zmień nazwę *.html na *.htm w folderze, który ma katalogi i podkatalogi.

Author: jww, 2008-10-29

10 answers

Wiersz polecenia systemu Windows: (jeśli wewnątrz pliku wsadowego, Zmień %x Na %%x)

for /r %x in (*.html) do ren "%x" *.htm

Działa to również przy zmianie nazwy środka plików

for /r %x in (website*.html) do ren "%x" site*.htm
 85
Author: Anonymous,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-13 20:20:08
find . -regex ".*html$" | while read line;
 do 
    A=`basename ${line} | sed 's/html$/htm/g'`;
    B=`dirname ${line}`;
    mv ${line} "${B}/${A}";
 done
 6
Author: Aditya Mukherji,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-29 05:15:11

W Pythonie

import os

target_dir = "."

for path, dirs, files in os.walk(target_dir):
    for file in files:
        filename, ext = os.path.splitext(file)
        new_file = filename + ".htm"

        if ext == '.html':
            old_filepath = os.path.join(path, file)
            new_filepath = os.path.join(path, new_file)
            os.rename(old_filepath, new_filepath)
 6
Author: monkut,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-29 05:17:27

W Bash możesz wykonać następujące czynności:

for x in $(find . -name \*.html); do
  mv $x $(echo "$x" | sed 's/\.html$/.htm/')
done
 4
Author: Adam Rosenfield,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-29 05:28:20

Jestem pewien, że jest bardziej elegancki sposób, ale oto pierwsza rzecz, która pojawiła się w mojej głowie:

for f in $(find . -type f -name '*.html'); do 
    mv $f $(echo "$f" | sed 's/html$/htm/')
done
 2
Author: albertb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-29 05:27:18
In bash use command rename :)

 rename 's/\.htm$/.html/' *.htm

 # or

 find . -name '*.txt' -print0 | xargs -0 rename 's/.txt$/.xml/'

 #Obs1: Above I use regex \. --> literal '.'  and  $ --> end of line
 #Obs2: Use find -maxdepht 'value' for determine how recursive is
 #Obs3: Use -print0 to avoid 'names spaces asdfa' crash!
 2
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-29 19:48:27

Jeśli masz forfiles (chodzi o Windows XP i 2003 i nowsze rzeczy myślę) możesz uruchomić:

forfiles /S /M *.HTM /C "cmd /c ren @file *.HTML"
 2
Author: BBX,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-01-23 20:51:14

W Linuksie możesz użyć polecenia 'rename' do zmiany nazw plików w partiach.

 1
Author: ayaz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-29 06:11:29

AWK na Linuksie. Dla pierwszego katalogu jest to twoja odpowiedź... Ekstrapoluj rekurencyjnie wywołując awk na dir_path, być może pisząc inny awk, który zapisuje dokładnie ten awk poniżej... i tak dalej.

ls dir_path/. | awk -F"." '{print "mv file_name/"$0" dir_path/"$1".new_extension"}' |csh
 0
Author: Alex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-29 07:07:20

Na Unixie możesz użyć rnm :

rnm -rs '/\.html$/.htm/' -fo -dp -1 *

Lub

rnm -ns '/n/.htm' -ss '\.html$' -fo -dp -1 *

Wyjaśnienie:

  1. -ns: name string (nowa nazwa). {[3] } jest regułą ciągu nazw, która rozszerza się do nazwy pliku bez rozszerzenia.
  2. -ss : search string (regex). Wyszukuje pliki z dopasowaniem.
  3. -rs: zastąp łańcuch postaci /search_regex/replace_part/modifier
  4. -fo: tryb Tylko pliku
  5. -dp: głębokość katalogu (-1 oznacza nieograniczony).
 0
Author: Jahid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-07 07:35:25