特殊:Badtitle/NS100:RecursiveMaintenanceScripts
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/RecursiveMaintenanceScripts }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/af | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|Afrikaans| [[::RecursiveMaintenanceScripts/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/ar | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|العربية| [[::RecursiveMaintenanceScripts/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/az | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|azərbaycanca| [[::RecursiveMaintenanceScripts/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/bcc | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|جهلسری بلوچی| [[::RecursiveMaintenanceScripts/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/bg | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|български| [[::RecursiveMaintenanceScripts/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/br | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|brezhoneg| [[::RecursiveMaintenanceScripts/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/ca | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|català| [[::RecursiveMaintenanceScripts/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/cs | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|čeština| [[::RecursiveMaintenanceScripts/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/de | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|Deutsch| [[::RecursiveMaintenanceScripts/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/el | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|Ελληνικά| [[::RecursiveMaintenanceScripts/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/es | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|español| [[::RecursiveMaintenanceScripts/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/fa | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|فارسی| [[::RecursiveMaintenanceScripts/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/fi | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|suomi| [[::RecursiveMaintenanceScripts/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/fr | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|français| [[::RecursiveMaintenanceScripts/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/gu | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|ગુજરાતી| [[::RecursiveMaintenanceScripts/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/he | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|עברית| [[::RecursiveMaintenanceScripts/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/hu | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|magyar| [[::RecursiveMaintenanceScripts/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/id | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|Bahasa Indonesia| [[::RecursiveMaintenanceScripts/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/it | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|italiano| [[::RecursiveMaintenanceScripts/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/ja | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|日本語| [[::RecursiveMaintenanceScripts/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/ko | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|한국어| [[::RecursiveMaintenanceScripts/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/ksh | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|Ripoarisch| [[::RecursiveMaintenanceScripts/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/mr | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|मराठी| [[::RecursiveMaintenanceScripts/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/ms | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|Bahasa Melayu| [[::RecursiveMaintenanceScripts/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/nl | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|Nederlands| [[::RecursiveMaintenanceScripts/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/no | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|norsk| [[::RecursiveMaintenanceScripts/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/oc | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|occitan| [[::RecursiveMaintenanceScripts/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/pl | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|polski| [[::RecursiveMaintenanceScripts/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/pt | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|português| [[::RecursiveMaintenanceScripts/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/ro | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|română| [[::RecursiveMaintenanceScripts/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/ru | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|русский| [[::RecursiveMaintenanceScripts/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/si | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|සිංහල| [[::RecursiveMaintenanceScripts/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/sq | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|shqip| [[::RecursiveMaintenanceScripts/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/sr | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|српски / srpski| [[::RecursiveMaintenanceScripts/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/sv | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|svenska| [[::RecursiveMaintenanceScripts/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/th | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|ไทย| [[::RecursiveMaintenanceScripts/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/tr | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|Türkçe| [[::RecursiveMaintenanceScripts/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/vi | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|Tiếng Việt| [[::RecursiveMaintenanceScripts/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/yue | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|粵語| [[::RecursiveMaintenanceScripts/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/zh | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|中文| [[::RecursiveMaintenanceScripts/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/zh-hans | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|中文(简体)| [[::RecursiveMaintenanceScripts/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:RecursiveMaintenanceScripts | UbuntuHelp:RecursiveMaintenanceScripts | {{#if: | :}}RecursiveMaintenanceScripts}}/zh-hant | • {{#if: UbuntuHelp:RecursiveMaintenanceScripts|中文(繁體)| [[::RecursiveMaintenanceScripts/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:RecursiveMaintenanceScripts|:RecursiveMaintenanceScripts|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :RecursiveMaintenanceScripts/zh | | {{#ifexist: RecursiveMaintenanceScripts/zh | | {{#ifeq: {{#titleparts:RecursiveMaintenanceScripts|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:RecursiveMaintenanceScripts|1|-1|}} | zh | | }}
<<Include(Tag/NeedsExpansion)>>
- title Recursive Maintenance Scripts
Introduction
Shell scripts can be one of the most useful tools for maintaining a Linux system. Here we'll give some simple examples of a recursive script that can be used to perform maintenance functions on an entire directory structure.
Basic structure
The basic structure of the recursive script uses a function call which in turn calls itself whenever it finds a directory. The script is run with a parameter of one or more directory names. For example:
username@systemname:~$ myscript.sh directory_name_to_process another_directory_to_process
#!/bin/bash # the function to perform function recursivefunction() { # for every file in the directory - do something with it for file in "$1"/*; do # do something done # once you've processed all the files go back and check for directories for file in "$1"/*; do # if you find a directory... if [ -d "$file" ]; then # perhaps do something # then call the function again with this new directory recursivefunction "$file" fi done } for param in "$@"; do recursivefunction "$param" done
Examples
Correcting file permissions
Have you ever copied a directory structure from a CDROM or USB thumb drive and the file and directory permissions were a mess? This quick script can recurse through the directory structure and set some sane permissions.
#!/bin/bash function setperms() { for file in "$1"/*; do chmod 664 "$file" done for file in "$1"/*; do if [ -d "$file" ]; then chmod 775 "$file" setperms "$file" fi done } for param in "$@"; do setperms "$param" done
Convert all video files
Want to convert all your various video files to the open standard Ogg Theora format?
#!/bin/bash function converttoogg() { for file in "$1"/*.mpg; do chmod 660 "$file" ffmpeg2theora -v 10 -a 10 "$file" done for file in "$1"/*.avi; do chmod 660 "$file" ffmpeg2theora -v 10 -a 10 "$file" done for file in "$1"/*.vob; do chmod 660 "$file" ffmpeg2theora -v 10 -a 10 "$file" done for file in "$1"/*.wmv; do chmod 660 "$file" ffmpeg2theora -v 10 -a 10 "$file" done for file in "$1"/*.mov; do chmod 660 "$file" ffmpeg2theora -v 10 -a 10 "$file" done for file in "$1"/*; do if [ -d "$file" ]; then chmod 770 "$file" converttoogg "$file" fi done } for param in "$@"; do converttoogg "$param" done