使用ls命令即可區分文件夾和文件。示例:ls -la
說明:
-l 列出文件的詳細信息。
-a 列出目錄下的所有文件,包括以 . 開頭的隱含文件。
要判斷是文件還是文件夾,看第一列的內容即可,第一列的第一個字母指明了文件類型:-」表示普通文件,「d」代表目錄,「l」代表連接文件,「b」代表設備文件。
Ⅱ Linux如何 查看根目錄
/ - 根目錄:
每一來個文件和源目錄都從這里開始。
只有root用戶具有該目錄下的寫許可權。此目錄和/root目錄不同,/root目錄是root用戶的主目錄。
> cd 目錄名字
> cd .. 或 cd ../ //上級目錄切換
> cd . 或 cd ./ //切換到當前目錄
> cd ~ 或 cd //直接切換到用戶家目錄 其實只要知道Linux命令一切都好說,推薦以下查詢方法去了解更多更詳細的Linux命令介紹》
Ⅲ 怎麼區別Linux系統裡面的目錄和文件
目錄指的文件夾,文件就是單個文件。如果使用命令行對目錄進行復制操作,需要加上-r參數,比如cp
-r
fold
new_fold,目錄和文件操作在參數上可能不同,還要注意許可權的問題,你可以訪問該目錄,不一定能操作該目錄下的所有文件。
你這無法保存應該有具體出錯信息
Ⅳ linux編寫:給出目錄名,輸出整棵目錄樹,判斷是不是目錄文件,若是則要打開輸出目錄的文件
Ⅳ linux 判斷是文件還是目錄的命令
使用if...else...進行判斷,比如待判斷的文件或目錄名是xxx,則可內以這樣進行判斷:容
if [ -d xxx ];then
echo "xxx is a dir"
elif [ -f xxx ];then
echo "xxx is a file"
fi
Ⅵ linux 判斷是文件還是目錄
可以使用ls命令的-l(小寫的L)參數,如果結果的第一列(第一列是屬性和許可權信息)的第一個字元是d,就表示這個是個目錄,如果第一個字元是-(英文橫杠),就表示這是個文件(第一列一共有10個字元)。例子:
ls -l target
Ⅶ 怎麼在linux 下查看文件目錄
在linux系統下使用ls可以查看當前目錄的所有文件,ls
-l可以查看所有文件並附加的所有信息;
如果想要回linux目錄下所有的文答件,可以使用ls的-r選項:
ls
-r
這樣顯示出來的是系統中所有的文件,如果需要顯示所有目錄文件,可以用egrep將目錄文件篩選出來,
ls
-r|egrep
./
這樣就能查看linux下的所有目錄文件了
Ⅷ 如何判斷linux是文件還是目錄
如果是腳本可以通過ls -l命令來判斷,因為目錄第一個字母是d。
如果是編程可以參考以下內容。
man stat;
stat.h中有判斷是什麼文件的宏:
S _ I S R E G ( ) 普通文件
S _ I S D I R ( ) 目錄文件
S _ I S C H R ( ) 字元特殊文件
S _ I S B L K ( ) 塊特殊文件
S _ I S F I F O ( ) 管道或F I F O
S _ I S L N K ( ) 符號連接( P O S I X . 1或S V R 4無此類型)
S _ I S S O C K ( ) 套接字(P O S I X . 1或S V R 4無此類型)
具體可以參照APUE文件目錄相關章節。
下面給出一個具體的例子:
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char* argv[])
{
struct stat buf;
if(argc < 2)
{
printf("Need Parameter");
return -1;
}
if(lstat(argv[1], &buf) < 0)
{
printf("lstat error for %s\r\n", argv[1]);
return;
}
if(S_ISDIR(buf.st_mode))
printf("%s Is DIR\r\n", argv[1]);
else if(S_ISREG(buf.st_mode))
printf("%s Is FILE\r\n", argv[1]);
return 0;
}
Ⅸ Linux 判斷是否是一個目錄還是一個文件的方法
#1、使用命令
ls-lfile
####結果中第一個字元是「-」則為普通文件,是「d」則為目錄
#2、使用命令
stat--printf=%Ffile
####查看結果
#3、shell
if[-ffile];then
echo"file"
elif[-dfile];then
echo"directory"
fi
Ⅹ linux 判斷文件夾是否有某個目錄
1. shell判斷文件,目錄是否存在或者具有許可權
2. #!/bin/sh
3.
4. myPath="/var/log/httpd/"
5. myFile="/var /log/httpd/access.log"
6.
7. # 這里的-x 參數判斷$myPath是否存在並且是否具有可執行許可權
8. if [ ! -x "$myPath"]; then
9. mkdir "$myPath"
10. fi
11.
12. # 這里的-d 參數判斷$myPath是否存在
13. if [ ! -d "$myPath"]; then
14. mkdir "$myPath"
15. fi
16.
17. # 這里的-f參數判斷$myFile是否存在
18. if [ ! -f "$myFile" ]; then
19. touch "$myFile"
20. fi
21.
22. # 其他參數還有-n,-n是判斷一個變數是否是否有值
23. if [ ! -n "$myVar" ]; then
24. echo "$myVar is empty"
25. exit 0
26. fi
27.
28. # 兩個變數判斷是否相等
29. if [ "$var1" = "$var2" ]; then
30. echo '$var1 eq $var2'
31. else
32. echo '$var1 not eq $var2'
33. fi
-f 和-e的區別
Conditional Logic on Files
-a file exists.
-b file exists and is a block special file.
-c file exists and is a character special file.
-d file exists and is a directory.
-e file exists (just the same as -a).
-f file exists and is a regular file.
-g file exists and has its setgid(2) bit set.
-G file exists and has the same group ID as this process.
-k file exists and has its sticky bit set.
-L file exists and is a symbolic link.
-n string length is not zero.
-o Named option is set on.
-O file exists and is owned by the user ID of this process.
-p file exists and is a first in, first out (FIFO) special file or
named pipe.
-r file exists and is readable by the current process.
-s file exists and has a size greater than zero.
-S file exists and is a socket.
-t file descriptor number fildes is open and associated with a
terminal device.
-u file exists and has its setuid(2) bit set.
-w file exists and is writable by the current process.
-x file exists and is executable by the current process.
-z string length is zero.
是用 -s 還是用 -f 這個區別是很大的!