lulix刪除檔案 使用grep恢復被刪檔案內容

2021-10-18 20:16:47 字數 2333 閱讀 3486

相信每乙個人對於作業系統的重定向不會陌生了。就是》, >>,

4function mytest()

} < mytest.in > mytest.out 2> mytest.err

現在,只要是test被呼叫,那麼,這個函式就會從mytest.in讀入資料,並把輸出重定向到mytest.out檔案中,然後標準錯誤則輸出到mytest.err檔案中。是不是很簡單?

因為函式級的重定向僅當在被函式呼叫的時候才會起作用,而且其也是指令碼的一部分,所以,你自然也可以使用變數來借檔名。下面是乙個示例:

11#!/bin/bash

function mytest()

echo hello world coolshell.cn

} >$out

out=mytest1.out

mytest

out=mytest2.out

mytest

這樣一來,標準輸出的重定向就可以隨$out變數的改變而改變了。在上面的例子中,第乙個調是重定向到mytest1.out,第二個則是到mytest2.out。

9$bash mytest.sh;more mytest?.out

mytest1.out

hello world coolshell.cn

mytest2.out

hello world coolshell.cn

正如前面所說的一樣,這裡並沒有什麼新的東西。上面的這個示例,轉成傳統的寫法是:

8#!/bin/bash

function mytest()

echo hello world coolshell.cn

mytest >mytest1.out

mytest >mytest2.out

到此為此,好像這個feature並沒有什麼特別的實用之處。有乙個可能比較實用的用法可能是把把你所有**的的標準錯誤重定向到乙個檔案中去。如下面所示:

24#!/bin/bash

log=err.log

function error()

echo "$*" >&2

function mytest1()

error mytest1 hello1 world1 coolshell.cn

function mytest2()

error mytest2 hello2 world2 coolshell.cn

function main()

mytest1

mytest2

} 2>$log

main

執行上面的指令碼,你可以得到下面的結果:

3$bash mytest.sh ;cat err.log

mytest1 hello1 world1 coolshell.cn

mytest2 hello2 world2 coolshell.cn

當然,你也可以不用定義乙個函式,只要是 語句塊,就可以使用函式級的重定向,就如下面的示例一樣:

21#!/bin/bash

log=err.log

function error()

echo "$*" >&2

function mytest1()

error mytest1 hello1 world1 coolshell.cn

function mytest2()

error mytest2 hello2 world2 coolshell.cn

mytest1

mytest2

} 2>$log

你也可以重定向 (…) 語句塊,但那會導致語句被執行於乙個sub-shell中,這可能會導致一些你不期望的行為或問題,因為sub-shell是在另乙個程序中。

如果你問,我們是否可以覆蓋函式級的重定向。答案是否定的。如果你試圖這樣做,那麼,函式呼叫點的重定向會首先執行,然後函式定義上的重定向會將其覆蓋。下面是乙個示例:

7#!/bin/bash

function mytest()

echo hello world coolshell.cn

} >out1.txt

mytest >out2.txt

執行結果是,out2.txt會被建立,但裡面什麼也沒有。

下面是乙個重定向標準輸入的例子:

13#!/bin/bash

function mytest()

while read line

doecho $line

done

hello

coolshell.cn

eofmytest

下面是其執行結果: 1

3$bash mytest.sh

hello

coolshell.cn

grep 找回 刪除檔案

這裡給大家介紹乙個小技巧用來恢復一些被rm了的檔案中的資料。我們知道,rm命令其實並不是真正的從物理上刪除檔案內容,只過不把檔案的 inode 了,其實檔案內容還在硬碟上。所以,如果你不小刪除了什麼比較重要的程式配置檔案的時候,我們完全可以用grep命令在恢復,下面是乙個恢 復示例 檢視源 列印幫助...

使用grep恢復被刪檔案內容

這裡給大家介紹乙個小技巧用來恢復一些被rm了的檔案中的資料。我們知道,rm命令其實並不是真正的從物理上刪除檔案內容,只過不把檔案的 inode 了,其實檔案內容還在硬碟上。所以,如果你不小刪除了什麼比較重要的程式配置檔案的時候,我們完全可以用grep命令在恢復,下面是乙個恢 復示例 grep a b...

使用grep恢復被刪檔案內容

這裡給大家介紹乙個小技巧用來恢復一些被rm了的檔案中的資料。我們知道,rm命令其實並不是真正的從物理上刪除檔案內容,只過不把檔案的inode 了,其實檔案內容還在硬碟上。所以,如果你不小刪除了什麼比較重要的程式配置檔案的時候,我們完全可以用grep命令在恢復,下面是乙個恢復示例 1grep a b ...