ls命令。
Is是List的缩写,表示显示文件目录列表。(只显示非隐藏文件的文件名)
参数:回
-a:--all的缩写,显答示所有文件,包括隐藏文件(以 . 开头的文件)
-L:列出长数据串,显示文件的大小,时间等数据信息。
ls -la:表示显示文件包括的所有信息。
注意事项
linux查看日志文件内容命令有
1、cat:由第一行开始显示文件内容。
2、tac:从最后一行开始显示,可以看出tac是cat的倒着写。
3、nl:显示的时候,顺道输出行号。
4、more:一页一页的显示文件内容
5、less与more类似,但是比more更好的是,他可以往前翻页。
6、head:只看头几行。
7、tail:只看尾巴几行。
可以使用man [命令]来查看各个命令的使用文档,如 :man cp。
㈡ linux如何查找某个文件名
题主你好,
linux中,最常用的查找文件命令是find.
最常用的查找文件方式有两种:
a.精确查找文件,即提供被查找文件的完整名称.
举例: 我在/root/ok/tmp文件夹下有两个文件, 名为: x.t和y.txt
可以看到x.t和x.txt这两个文件都找到了, 并且给的是绝对路径.
写在最后: 当然,上面只是find命令的一个小小的用法, 题主看看利用上面的方法是否可以满足要求, 如果不满足, 题主可以追问, 详细的说一下题主的场景, 我再给出解决方案.
希望可以帮到题主,欢迎追问.
㈢ 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 编程的功能把那些无用的列去掉即可。
㈣ linux怎么查看目录下的文件
ls 命令是Linux下最常用的指令之一。ls命令为英文单词 list 的缩写,正如英文单词 list 的意思,其功能是列出指定目录下的内容及其相关属性信息。
默认状态下,ls命令会列出当前目录的内容。而带上参数后,我们可以用ls做更多的事情。作为最基础同时又是使用频率很高的命令,我们很有必要搞清楚ls命令的用法,那么接下来一起看看吧!更多linux命令大全 来《Linux就该这么学》。
语法格式: ls [选项] [文件]
常用参数:
-a 显示所有文件及目录 (包括以“.”开头的隐藏文件)
-l 使用长格式列出文件及目录信息
-r 将文件以相反次序显示(默认依英文字母次序)
-t 根据最后的修改时间排序
-A 同 -a ,但不列出 “.” (当前目录) 及 “..” (父目录)
-S 根据文件大小排序
-R 递归列出所有子目录
参考实例
列出所有文件(包括隐藏文件):
[root@linuxcool ~]# ls -a
列出文件的详细信息:
[root@linuxcool ~]# ls -l
列出根目录(/)下的所有目录:
[root@linuxcool ~]# ls /
列出当前工作目录下所有名称是 “s” 开头的文件 :
[root@linuxcool ~]# ls -ltr s*
列出 /bin 目录下的所有目录及文件的详细信息 :
[root@linuxcool ~]# ls -lR /bin
㈤ linux编程怎么获得当前文件夹的文件夹名
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下,查找某一目录下文件的内容包含指定字符的文件名
给定字符串查找文来件名自,例如,查找当前目录下所有文件中包含字符串"Linux"的文件,可以使用如下命令:egrep Linux *
举例:testfile、testfile1中都还有Linux,查找结果如下所示:
$ egrep Linux * #查找当前目录下包含字符串“Linux”的文件
testfile:hello Linux!
testfile:Linux is a free Unix-type operating system.
testfile:Linux.
testfile1:helLinux!
testfile1:This a Linux testfile!
(6)linux中获取目录下文件名扩展阅读
Linux egrep命令执行效果与"grep-E"相似,使用的语法及参数可参照grep指令,与grep的不同点在于解读字符串的方法。
egrep是用extended regular expression语法来解读的,而grep则用basic regular
expression 语法解读,extended regular expression比basic regular
expression的表达更规范。
㈦ linux下按文件名和文件内容查找文件
在当前目录下,查找所有内容包含‘abc'的文件
find . -type f | xargs grep -l 'abc'
或
find . -type f -exec grep -l 'abc' {} ;
linux查找文件命令find
根据部分文件名查找方法:
这个方法和在WINDOWS中查找已知的文件名方法是一样的。不过在Linux中根据部分文件名查找文件的方法要比在WINDOWS中的同类查找方法要强大得多。
例如我们知道某个文件包含有srm这3个字母,那么要找到系统中所有包含有这3个字母的文件是可以实现的,输入:
find /etc -name '*srm*'
这个命令表明了Linux系统将在/etc整个目录中查找所有的包含有srm这3个字母的文件,比如 absrmyz, tibc.srm等等符合条件的文件都能显示出来。如果你还知道这个文件是由srm 这3个字母打头的,那么我们还可以省略最前面的星号,命令如下:
find/etc -name 'srm*'
这是只有像srmyz 这样的文件才被查找出来,象absrmyz或者 absrm这样的文件都不符合要求,不被显示,这样查找文件的效率和可靠性就大大增强了。