『壹』 如何在linux中使用shell腳本遍歷指定目錄的文件,將創建時間大於指定時間的文件,復制到指定目錄下。
大於指定時間?最簡單的就是直接find裡面指定吧。例如,查找當前目錄及其子目錄所有mtime大於1天的文件:
find
/path
-type
f
-mtime
+1
即可,/path
可以換成其他路徑,-mtime
+1
表示時間大於1天。-1的話表示小於一天也就是1天之內的。
『貳』 Linux如何使用shell查看目錄及其子目錄下的所有文件
執行tree命令即可按樹形結構輸出指定目錄下的所有子目錄和所有文件
『叄』 linux shell命令在哪個文件夾
假設你的文件夾都在/path/to/dir下,並且該目錄下的文件夾都是你要處理的這種日期格式的,簡單腳本如下:
#!/bin/sh
for
fd
in
`find
/path/to/dir
-maxdepth
1
-mindepth
1
-type
d`
do
dn=${fd##*/}
if
[
$dn
!=
$2
]
&&
[
$dn
-le
`date
+%y%m%d
-d
-$1`
]
then
rm
-rf
$fd
fi
done
調用方式:腳本名
參數1
參數2
參數1
-
#day或#month或#year;#為數字
參數2
-
要保留的文件夾名
『肆』 linux Shell怎麼查找指定文件,並進入該文件所在目錄,目錄有空格
1.使用絕對路徑執行的shell文件(如/home/xxx/binfile)
直接使用dirname $0即可
2.對於使用相對路徑執行的shell文件(如 ./xxx/binfile)
pwd與dirname結合使用;pwd獲得的是執行當前shell文件時,用戶所在的位置;dirname可以獲得相對於那個位置的偏移:
例如某shell文件所在的位置是/home/user_name/work2/SNS3_server_im/Developing/trunk/im_capp/src/notify_serv/shell文件名
1 #!/bin/sh
2 pwd
3 echo `dirname $0`
執行後輸出
/home/user_name/work2/SNS3_server_im/Developing/trunk/im_capp/src
./notify_serv
『伍』 linux shell中的遍歷目錄並刪除目錄下與目錄名相同的文件
先設定實驗環境:
#
造
5
個
目錄,每個目錄下,造
3
個
文件和兩個子目錄如下:
cd
$home/tmp
for
i
in
d1
d2
d3
d4
d5
do
mkdir
-p
$i
touch
$i/1.txt
$i/2.txt
$i/3.txt
mkdir
-p
$i/tmp1
$i/tmp2
done
#
檢驗測試環境:
$
ls
-lr
d1
total
0
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
2.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
3.txt
drwxr-sr-x
2
wenlee
comm
256
dec
22
10:35
tmp1/
drwxr-sr-x
2
wenlee
comm
256
dec
22
10:35
tmp2/
#
利用下列腳本來實現你要做的:
cd
$home/tmp
for
i
in
*/1.txt
do
echo
"found
$i,
save
$i
and
remove
everything
else
under
$(dirname
$i)/"
save_this_file=$(basename
$i)
curr_dir=$(dirname
$i)
#
把這個1.txt暫時存到/tmp裡面去,為了避免已經有同樣的檔案名稱在/tmp,加上$$
(i.e.
pid)
mv
$i
/tmp/${save_this_file}.$$
rm
-rf
$curr_dir
mkdir
-p
$curr_dir
mv
/tmp/${save_this_file}.$$
$curr_dir
done
#
屏幕執行輸出如下:
found
d1/1.txt,
save
d1/1.txt
and
remove
everything
else
under
d1/
found
d2/1.txt,
save
d2/1.txt
and
remove
everything
else
under
d2/
found
d3/1.txt,
save
d3/1.txt
and
remove
everything
else
under
d3/
found
d4/1.txt,
save
d4/1.txt
and
remove
everything
else
under
d4/
found
d5/1.txt,
save
d5/1.txt
and
remove
everything
else
under
d5/
#
復驗實驗環境:
$
ls
-l
d?/*
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d1/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d2/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d3/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d4/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d5/1.txt
ok?
thanks!
『陸』 linux中Shell歷史命令記錄文件的路徑是什麼
路徑/etc/bashrc。在 Linux 下面可以使用 history 命令查看用戶的所有歷史操作,同時 shell 命令操作記錄默認保存在用戶目錄的 .bash_history 文件中。通過這個文件可以查詢 shell 命令的執行歷史。
代碼如下:
HISTFILESIZE=4000
HISTSIZE=4000
HISTTIMEFORMAT='%F %T'
export HISTTIMEFORMAT
注意:
HISTFILESIZE 表示在 .bash_history 文件中保存命令的記錄總數,默認值是 1000;
HISTSIZE 定義了 history 命令輸出的記錄總數;
HISTTIMEFORMAT 定義了時間顯示格式,該格式與 date 命令後的 「+"%F %T"」 是一樣的;
HISTTIMEFORMAT 作為 history 的時間變數將值傳遞給 history 命令。
(6)linuxshell目錄文件擴展閱讀:
顯示歷史命令
history 顯示全部歷史
history 數字 顯示之前執行過的若干命令,例:history 2 顯示執行過的上兩條命令
使用上下箭頭鍵也可以查看上一條根下一條命令,
3.運行歷史命令
!! 運行上一條命令
!88 運行第88條命令
!88 /test 運行第88條命令並在命令後面加上/test
!?CF? 運行上一個包含CF字元串的命令
!ls 運行上一個ls命令
!ls:s/CF/l 運行上一個ls命令,其中把CF替換l
fc 編輯並運行上一個歷史命令
fc 66 編輯並運行第66個歷史命令
fc -e /usr/bin/vim 66 使用vim編輯第66個命令並運行
『柒』 linux shell 運行後沒有那個文件或目錄,需要怎麼改
看不出來有問題,你是不是輸入位置變數錯了
要不你是試試每個mkdir 後面加 -p
『捌』 linux shell 打開執行目錄
可以在shell中定義變數,然後使用變數,實現高效的跳轉到對應目錄,免去了輸入較長的地址。
比如我裝的LAMP環境,每次跑起環境都挺麻煩的。具體命令如下:
cd /opt/lamppsudo ./manager.run Linux命令需求可查詢「Linux命令大全」。
『玖』 如何用linux命令進入一個目錄 並且執行該目錄下的一個文件
用linux命令進入一個目錄 並且執行該目錄下的一個文件方法如下:
1、打開文件vi(如/etc裡面有一個文件hosts,要打開這個文件執行vi hosts);
2、進入編輯模式i,a(打開hosts後不能進行編輯,輸入i之後出現---insert就進入編輯模式可以進行編輯了);
3、退出編輯模式,按下Esc;
4、進入命令模式,按下;
5、退出但不保存q;
6、退出保存wq或x;
7、保存w。
『拾』 如何用shell獲取linux目錄下的文件名
獲取所有常規文件的文件名並列印出來的腳本listfile.sh如下
#!/bin/bash
dir="/*"
dir=$1$dir
for f in $dir
do
if [ -f $f ]
then
echo $f
fi
done
使用方法:
$ listfile.sh PATH
原理:
PATH參數是路徑,將路徑後加上「/*」,代表該目錄下的所有文件和目錄名,利用for循環比較每個文件是否是常規文件( -f比較運算符),若if表達式為真則列印
舉例:
ls -l
total 36
-rwxrwxr-x 1 lipeng lipeng 48 Nov 29 20:08 aaa.sh
drwxrwxr-x 2 lipeng lipeng 4096 May 4 2015 byteorder
drwxrwxr-x 8 lipeng lipeng 4096 May 3 2015 hello
-rwxrwxr-x 1 lipeng lipeng 122 Nov 29 20:16 listfile.sh
-rw-rw-r-- 1 lipeng lipeng 177 Aug 1 03:10 main.cpp
drwxrwxr-x 2 lipeng lipeng 4096 Sep 13 16:42 matrix
drwxrwxr-x 5 lipeng lipeng 4096 Apr 28 2015 modbus
drwxrwxr-x 2 lipeng lipeng 4096 Sep 13 10:10 shtest
drwxrwxr-x 2 lipeng lipeng 4096 Sep 16 18:21 test
$ ./listfile.sh .
./aaa.sh
./listfile.sh
./main.cpp