Useful Commands/File Processing: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
(Created page with "{| class="wikitable" ! 情境 || 指令 |- | 目錄與檔案權限分別設為 755 和 644 | <source lang="bash"> find . -type d -exec chmod 755 {} +\ find . -type f -exec chm...")
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
= File System =
{| class="wikitable"
{| class="wikitable"
! 情境 || 指令
! 🎯 Purpose || ⌨️ Command
|-
| list with color and suffix for dir and link
| <syntaxhighlight lang="bash">ls -FG</syntaxhighlight>
|-
| list add hidden files only '''single quote is necessary'''
| <syntaxhighlight lang="bash">find . -name '.*'</syntaxhighlight>
|-
| delete dist directory recursively except README.md and itself.
| <syntaxhighlight lang="bash">
find ../../assets/swagger/support ! -path *swagger/support ! -name 'README.md' -delete
</syntaxhighlight>
|-
| delete files that filename leads by - or --
| <syntaxhighlight lang="bash">rm -- -X --exclude-from</syntaxhighlight>
|-
| Set file mtime <br/>2013-09-01 00:00:00
| <syntaxhighlight lang="bash">touch -t 201309010000 FILENAME</syntaxhighlight>
|}
 
{| class="wikitable"
! 🎯 Purpose || ⌨️ Command
|-
|-
| 目錄與檔案權限分別設為 755 和 644
| 目錄與檔案權限分別設為 755 和 644
| <source lang="bash">
| <syntaxhighlight lang="bash">
find . -type d -exec chmod 755 {} +\
find . -type d -exec chmod 755 {} +\
find . -type f -exec chmod 644 {} +
find . -type f -exec chmod 644 {} +
</source>
</syntaxhighlight>
|-
| 大量改檔名 *.js 改名 *.html || <source lang="bash">rename 's/js$/html/' *.js</source>
|-
|-
| 大量改檔名 *.pdf (原理為 sed) || <source lang="bash">rename 's/搜尋樣式/取代字串/' *.pdf</source>
| rename
| <syntaxhighlight lang="bash">
# Test first
$ rename -n 's/short_selled-([0-9]{8}).json/selled-$1.json/' short_selled-*
rename(short_selled-20190606.json, selled-20190606.json)
rename(short_selled-20190610.json, selled-20190610.json)
rename(short_selled-20190611.json, selled-20190611.json)
...
# Do it
$ rename 's/short_selled-([0-9]{8}).json/selled-$1.json/' short_selled-*
</syntaxhighlight>
|-
|-
| 搜尋文字檔,複製到另一個目錄 || <source lang="bash">find . -name *.txt -exec cp {} 目的目錄 \;</source>
| 搜尋文字檔,複製到另一個目錄 || <syntaxhighlight lang="bash">find . -name *.txt -exec cp {} 目的目錄 \;</syntaxhighlight>
|-
|-
| 從所在目錄遞迴掃檔案找 'string' || <source lang="bash">grep -rn --color -C 2 'string' .</source>
| 從所在目錄遞迴掃檔案找 'string' || <syntaxhighlight lang="bash">grep -rn --color -C 2 'string' .</syntaxhighlight>
|-
|-
| 移除 7 天前的 log || <source lang="bash">find . -name '*.log' -mtime 7 -exec rm -f {} \;</source>
| 移除 7 天前的 log || <syntaxhighlight lang="bash">find . -name '*.log' -mtime 7 -exec rm -f {} \;</syntaxhighlight>
|-
|-
| 移除超過 7 天的 log || <source lang="bash">find . -name '*.log' -mtime +7 -exec rm -f {} \;</source>
| 移除超過 7 天的 log || <syntaxhighlight lang="bash">find . -name '*.log' -mtime +7 -exec rm -f {} \;</syntaxhighlight>
|-
|-
| 移除 7 天前到現在的 log || <source lang="bash">find . -name '*.log' -mtime -7 -exec rm -f {} \;</source>
| 移除 7 天前到現在的 log || <syntaxhighlight lang="bash">find . -name '*.log' -mtime -7 -exec rm -f {} \;</syntaxhighlight>
|-
|-
| 用 regex 找檔案 || <source lang="bash">find . -regex '.*[0-9]\{4\}.txt'</source>
| 用 regex 找檔案 || <syntaxhighlight lang="bash">find . -regex '.*[0-9]\{4\}.txt'</syntaxhighlight>
|-
|-
| 取檔名中數字的部分 || <source lang="bash">ls file-0001.txt | sed 's/file-\([0-9]\{4\}\).txt/\1/'</source>
| 取檔名中數字的部分 || <syntaxhighlight lang="bash">ls file-0001.txt | sed 's/file-\([0-9]\{4\}\).txt/\1/'</syntaxhighlight>
|}
|}

Latest revision as of 01:59, 17 September 2026

File System

🎯 Purpose ⌨️ Command
list with color and suffix for dir and link
ls -FG
list add hidden files only single quote is necessary
find . -name '.*'
delete dist directory recursively except README.md and itself.
find ../../assets/swagger/support ! -path *swagger/support ! -name 'README.md' -delete
delete files that filename leads by - or --
rm -- -X --exclude-from
Set file mtime
2013-09-01 00:00:00
touch -t 201309010000 FILENAME
🎯 Purpose ⌨️ Command
目錄與檔案權限分別設為 755 和 644
find . -type d -exec chmod 755 {} +\
find . -type f -exec chmod 644 {} +
rename
# Test first
$ rename -n 's/short_selled-([0-9]{8}).json/selled-$1.json/' short_selled-*
rename(short_selled-20190606.json, selled-20190606.json)
rename(short_selled-20190610.json, selled-20190610.json)
rename(short_selled-20190611.json, selled-20190611.json)
...
# Do it
$ rename 's/short_selled-([0-9]{8}).json/selled-$1.json/' short_selled-*
搜尋文字檔,複製到另一個目錄
find . -name *.txt -exec cp {} 目的目錄 \;
從所在目錄遞迴掃檔案找 'string'
grep -rn --color -C 2 'string' .
移除 7 天前的 log
find . -name '*.log' -mtime 7 -exec rm -f {} \;
移除超過 7 天的 log
find . -name '*.log' -mtime +7 -exec rm -f {} \;
移除 7 天前到現在的 log
find . -name '*.log' -mtime -7 -exec rm -f {} \;
用 regex 找檔案
find . -regex '.*[0-9]\{4\}.txt'
取檔名中數字的部分
ls file-0001.txt | sed 's/file-\([0-9]\{4\}\).txt/\1/'