#/bin/bash
ls/home/user/test>txt
echo"該目錄中有文件"`head-1txt`
rm-rftxt
② 如何在linux中使用shell腳本遍歷指定目錄的文件,將創建時間大於指定時間的文件,復制到指定目錄下。
#!/bin/sh
DIR=$1
TIME=$2
OTDIR=$3
TMP=`find
$1
-mtime
+$2
-print`
echo
$TMP
cp
$TMP
$OTDIR
執行時
要有3個參數指定文件的目錄
大於指定的時間
復制到的目錄
沒寫驗內證部分
參數順序不要容寫錯
③ 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命令在哪個文件夾
假設你的文件夾都在/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、pwd確認自己的路徑。
2、將shell1.sh放到/bin
⑥ 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中的遍歷目錄並刪除目錄下與目錄名相同的文件
先設定實驗環境:
#
造
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!
⑧ 如何用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
⑨ linux shell 運行後沒有那個文件或目錄,需要怎麼改
看不出來有問題,你是不是輸入位置變數錯了
要不你是試試每個mkdir 後面加 -p
⑩ Shell獲取某目錄下所有文件夾的名稱
方法有三,如下:
#!/bin/bash
#方法一
dir=$(ls-lD:/temp/|awk'/^d/{print睜罩粗$NF}')
foriin$dir
do
echo$i
done
#######
#方法二
fordirin$(lsD:/tmep/)
do
[-d$dir]&&echo$dir
done
##方法三
ls-lD:/temp/|awk'/^d/{print$NF}'
##其實同方法一,直接就可以顯示不用for循環
大致思路:
<?php
function traverseDir($dir){
if($dir_handle = @opendir($dir)){
while($filename = readdir($dir_handle)){
if($filename != "." && $filename != ".."){
$subFile = $dir.DIRECTORY_SEPARATOR.$filename; 要將源目錄及子文件相連
if(is_dir($subFile)){ 若子文件是個目錄
echo $filename.'<br>' 輸出該目錄名稱
traverseDir($subFile); 遞歸找出下級目錄名稱
}
}
}
closedir($dir_handle);
}
}
$dirNames = traverseDir("d:/dos"); 測試某目錄
?>
呵呵,原來是團友啊我寫了一個!Set ws=WScript.CreateObject("wscript.shell")
w=ws.CurrentDirectory
Set fso=WScript.CreateObject("scripting.filesystemobject")
Set fs=fso.GetFolder(w)
Set f=fs.SubFolders
For Each uu In f
t=t & uu.Path & vbcrlf
Next
MsgBox t
用FSO對象模型不是很好么,這種代碼看的好痛苦哇!
我在之前做過一個FTP的客戶端工具。
drw 文件夾
-rw 文件(有擴展名或無擴展名)
我是根據服務端返回的報文進行分析獲取的列表。
給你一些代碼片段:
/ <summary>
/ 獲取指定目錄下的文件和文件夾。
/ </summary>
/ <param name=path>要獲取的目錄</param>
/ <param name=WRMethods>要發送到FTP伺服器的密令。</param>
/ <returns></returns>
public string[] GetFileList(string path, string WRMethods)從ftp伺服器上獲得文件列表
{
WebResponse response;
string[] downloadFiles;
int conut = 4;
StringBuilder result = new StringBuilder();
Connect(path);
if (FTPVariable.IsUseProxy_ftp)
{
reqFTP.Proxy = FtpProxy.GetFtpSelectProxy(FTPVariable.FtpCommand_transferProxyName);
}
reqFTP.ReadWriteTimeout = 12000;
如果不應銷毀到伺服器的連接,則為 true;否則為 false。默認值為 true。
reqFTP.Method = WRMethods;
try
{
response = (FtpWebResponse)reqFTP.GetResponse();
goto Ftp_lbl_03;
}
catch (WebException webex)
{
GetReply(webex.Message);
if (ReplyCode == 530) 未登錄。
{
goto Ftp_lbl_04;
}
else if (ReplyCode == 550)
{
goto Ftp_lbl_04;
}
else
{
FtpManage.SetLog("獲取列表超時,等候1秒後重試!");
goto Ftp_lbl_01;
}
}
Ftp_lbl_01:
try
{
FtpManage.SetLog("正在連接伺服器 " + FtpRemoteHost);
response = GetRequest(path, WRMethods);
}
catch (WebException)
{
FtpManage.SetLog("獲取列表超時,等候1秒後重試!");
downloadFiles = null;
System.Threading.Thread.Sleep(1000);
if (conut == 0)
{
goto Ftp_lbl_02;
}
conut--;
goto Ftp_lbl_01;
}
catch (Exception ex)
{
MSG.Show(ex.Message, Global.GetRS["msgTilteError"], MessageBoxButton.OK, MsgIco.Error);
FtpManage.SetLog("命令執行失敗,原因:" + ex.Message);
downloadFiles = null;
return downloadFiles;
}
Ftp_lbl_03:
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);中文文件名
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("
");
line = reader.ReadLine();
}
if (result.Length == 0)
{
return null;
}
to remove the trailing '
'
result.Remove(result.ToString().LastIndexOf('
'), 1);
reader.Close();
response.Close();
FtpManage.SetLog("命令已成功執行");
return result.ToString().Split('
');
Ftp_lbl_04:
FtpManage.SetLog(ReplyInfo);
return null;
Ftp_lbl_02:
FtpManage.SetLog("550 獲取列表失敗,無法連接遠程伺服器!");
FtpManage.ftpmanage.IsRefurbish = true;
return null;
}
/ <summary>
/ 獲取指定目錄下的文件和文件夾。
/ </summary>
/ <param name=path>要獲取的目錄</param>
/ <returns></returns>
public string[] GetFileList(string path)從ftp伺服器上獲得文件列表
{
return GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/" + path, WebRequestMethods.Ftp.ListDirectory);
}
/ <summary>
/ 獲取指定目錄下的文件和文件夾。
/ </summary>
/ <returns></returns>
public string[] GetFileList()從ftp伺服器上獲得文件列表
{
return GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/", WebRequestMethods.Ftp.ListDirectory);
}
/ <summary>
/ 獲取目錄和文件名,返回目錄表。
/ </summary>
/ <param name=path>要獲取的目錄</param>
/ <returns></returns>
public string[] GetCatalog_FileList(string path)
{
string[] fountainhead = GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails);
string[] Catalog = null;
if (fountainhead == null)
{
return null;
}
Catalog = new string[fountainhead.Length];
for (int i = 3; i < fountainhead.Length; i++)
{
Catalog[i - 3] += fountainhead[i].Substring(55, fountainhead[i].Length - 55) + "&";FileName
Catalog[i - 3] += fountainhead[i].Substring(30, 12) + "&";FileSize
Catalog[i - 3] += fountainhead[i].Substring(42, 13) + "&";AmendDate
Catalog[i - 3] += fountainhead[i].Substring(0, 3) + "&";
}
return Catalog;
}
fso.GetFolder(path)
你剛才不是問過了么?
查找某目錄下所有 文件 及 子文件夾
試一試不用 FileSystemObject 對象,只用基本控制項的代碼。
'例子需控制項:Command1,List1,List2,File1,Dir1,都採用默認屬性。
'例如,查找 C: ,帶 '** 的語可修改
Dim ctFind As Boolean
Private Sub Form_Load()
Me.Caption = "查找所有文件及文件夾"
Command1.Caption = "查找"
List2.Visible = False: File1.Visible = False: Dir1.Visible = False
Label1.Caption = "就緒"
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub Form_Resize()
Dim W As Long
On Error Resume Next
W = 720
List1.Move 0, 0, Me.ScaleWidth - W - 120, Me.ScaleHeight - 300
Command1.Move Me.ScaleWidth - W - 60, 300, W
Label1.Move 90, Me.ScaleHeight - 255, Screen.Width, 255
End Sub
Private Sub Command1_Click()
ctFind = Not ctFind
If ctFind Then
Command1.Caption = "取消"
Call FindDirFile("C:") '**查找 C: 下的所有文件和目錄,或 C:Windows 等
Command1.Caption = "查找"
Else
Command1.Caption = "查找"
End If
End Sub
Private Sub FindDirFile(ByVal nPath As String)
Dim I As Long, nDir As String, Ci As Long
ctFind = True
List1.Clear: List2.Clear
If Right(nPath, 1) <> "" Then nPath = nPath & ""
List1.AddItem "查找 " & nPath: List2.AddItem nPath
File1.Pattern = "*"
File1.System = True: File1.Hidden = True: File1.ReadOnly = True
On Error GoTo Cuo
Dir1.Path = nPath
On Error GoTo 0
Do
If List2.ListCount = 0 Then Exit Do
nPath = List2.List(0)
List2.RemoveItem 0
Dir1.Path = nPath
For I = 0 To Dir1.ListCount - 1
GoSub ShowGe
nDir = Dir1.List(I)
If Right(nDir, 1) <> "" Then nDir = nDir & ""
List1.AddItem "■" & nDir
List2.AddItem nDir
Next
File1.Path = nPath
For I = 0 To File1.ListCount - 1
GoSub ShowGe
List1.AddItem " " & nPath & File1.List(I)
Next
Loop
Label1.Caption = "查找完畢,共找到 " & List1.ListCount & " 個條目"
ctFind = False
Exit Sub
Cuo:
List1.AddItem "起始目錄不存在:" & nPath
ctFind = False
Exit Sub
ShowGe:
Ci = Ci + 1
If Ci < 99 Then Return
Ci = 0
Label1.Caption = "已找到 " & List1.ListCount & " 個:" & nPath
DoEvents
If ctFind Then Return
End Sub
執行如下三條命令即可:
(1)、$script myresultfile
(2)、$ls -al *.txt
(3)、$exit
此時,該目錄下的所有 txt 文件名稱就會以長格式保存在 myresultfile 文件中了。
然後你再使用 SHELL 編程的功能把那些無用的列去掉即可。