當前位置:首頁 » 文件管理 » shell判斷為文件夾

shell判斷為文件夾

發布時間: 2022-09-08 22:31:50

linux shell程序,如何輸入一個文件名判斷它是文件夾還是文件

//是目錄不是文件夾
#! /bin/bash
# filename:FileType.sh

read -p "Please input the filename :" filename
fpath=$filename
if [ -d $fpath ];
then
echo "$fpath is a direstory.";
elif [ -e $fpath ];
then
echo "$fpath is a file.";
else
echo "$fpath is NOT a file or direstory.";
fi

❷ shell判斷文件夾內是否有文件

if [ `ls directory | wc` -eq 0 ]
then
echo "文件夾為空"
fi

#directory是要判斷的文件夾,'是ESC鍵下面那個鍵不是單引號,手機不好打

❸ shell腳本如何判斷目錄下的多個文件夾是否為空

directoy=./
find
${directoy}
-type
d
|
xargs

-sh
|
grep
^0
|
awk
'{print
$2}'
先在指定目錄下查找
目錄文件,
然後用

-sh
計算目錄大小,
找出大小為0的目錄,然後列印出來即可
若是不希望循環查找下去,可以只查找本級目錄:
ls
-l
|
grep
^d
|
awk
'{print
$NF}'
|
xargs

-sh
|
grep
^0
|
awk
'{print
$2}'
還有個比較完善的解答:
directoy=./
ls
-l
${directoy}
|
grep
^d
|
awk
'{printf("%s/%s\n",dir,$NF)}'
dir=${directoy}
|
xargs

-sh
|
grep
^0
|
awk
'{print
$2}'

❹ shell判斷文件夾是否存在

Shell中的test命令用於檢查某個條件是否成立,它可以進行數值、字元和文件三個方面的測試,那麼如何使用shell來判斷文件夾是否存在呢?今天小編就就帶大家來看看吧。
文件夾不存在則創建


如果身邊就有電腦,可以跟著上文一起操作,這樣可以更好的理解喔
本文章基於Lenovo品牌、Windows10系統撰寫的。

❺ shell腳本判斷是文件還是文件夾

那就寫兩個if判斷吧
if [ -f $FILE ]
if [ -d $FILE ]

❻ shell腳本判斷文件夾下是否有文件

search_dir=/tmp/test
include_subdir=1

if[$include_subdir-eq1];then
n=$(find$search_dir-typef-execsh-c'printf"%s ""$1";kill"$PPID"'sh{};|grep-v"Terminated"|wc-l)
else
n=$(find$search_dir-maxdepth1-typef-execsh-c'printf"%s ""$1";kill"$PPID"'sh{};|grep-v"Terminated"|wc-l)
fi

結果 n 為 0 表示指定目錄下面沒有文件,否則有文件。如果不需要檢查指定目錄下的子目錄,把 include_subdir 置為 0 即可。find 命令中較復雜的那部分是為了實現找到第一個文件時就停止查找,避免檢查有大量文件的目錄時影響性能。

❼ Shell腳本判斷是文件還是目錄怎麼寫

#!/bin/bash

if[-d$1]
then
echo"$1isadirectory."
exit
elif[-f$1]
then
echo-n"$1isafile,"
if[-L$1]
then
echo"anditisalsoasymboliclink."
A=`ls-L$1`
if[-e$A]
then
echo"Symboliclinkexist."
else
echo"Symboliclinknotexist."
fi
exit
else
echo"butitisnotasymboliclink."
exit
fi
fi

❽ shell:判斷文件夾是不是存在,如果不存在則

-e FILE True if file exists
更多選項,請在命令行下輸入help test

如果是bash的話 如下:
if test -e file_name
then
echo file exists
else
echo
file does not exists
fi

❾ powershell判斷是文件夾還是文件


#目錄存在
Test-Path$path-PathTypeContainer
[System.IO.Directory]::Exists($path)

#文件存在
Test-Path$path-PathTypeLeaf
[system.IO.File]::Exists($path)

❿ 如何判斷一個路徑是目錄還是文件

第十三個findfirstfile尋找文件以及獲得文件的信息
這里舉一個例子吧,列舉e盤第一目錄下的所有文件,包括文件夾,結合findnextfile
#include<windows.h>
#include<stdio.h>
int
main()
{
bool
done=true;
win32_find_data
fd;
handle
hfind
=
findfirstfile("e:\\*.*",
&fd);//第一個參數是路徑名,可以使用通配符,懂dos的人應該知道吧!fd存儲有文件的信息
while
(done)
{
printf("%s\n",fd.cfilename);
done=findnextfile(hfind,
&fd); //返回的值如果為0則沒有文件要尋了
}
return
0;
}
當然也可以直接找一個文件,不使用通配符,但這樣有什麼意義呢?,如findfirstfile("e:\\aaa.txt",&fd);其實這個可以獲取一個文件的信息,如文件是不是隱藏的,或者有沒有隻讀屬性等。
當然通過控制通配符,也可以尋找特定類型的文件,比如我只要找文本文件,那麼就是這個語句findfirstfile("e:\\*.txt",&fd);就行了,關鍵看你自己靈活運用。
前面說過fd里存儲有文件的信息,那怎麼根據fd裡面的成員判斷這個文件的屬性,文件是否隱藏,是不是文件夾。
fd里的dwfileattributes存儲有文件的信息,如判斷是否為文件夾,只要把這個變數和file_attribute_directory進行按位與運算,如果為1的話,表明為文夾件,如if(fd.dwfileattributes&file_attribute_directory==1)
printf("%s是文件夾\n",fd.cfilename);
其它判斷也是一樣,現在給出文件的屬性(常用幾個):file_attribute_hidden(隱藏)
file_attribute_readonly(只讀)file_attribute_system(系統)
第十四個findnextfile尋找文件
參照findfirstfile函數的例子!

熱點內容
xp文件夾共享密碼 發布:2024-10-11 20:20:01 瀏覽:876
夢幻西遊2跑商腳本 發布:2024-10-11 20:15:48 瀏覽:649
安卓手機如何打開dwg文件下載cad 發布:2024-10-11 20:15:39 瀏覽:90
jscss壓縮 發布:2024-10-11 20:15:01 瀏覽:422
映客一鍵清除緩存 發布:2024-10-11 20:10:32 瀏覽:278
cs16伺服器自己搭建多少錢 發布:2024-10-11 19:43:55 瀏覽:50
sql動態where 發布:2024-10-11 19:30:30 瀏覽:307
高速緩存設計博士論文 發布:2024-10-11 19:19:29 瀏覽:652
adb源碼下載 發布:2024-10-11 19:15:08 瀏覽:978
vbe編程 發布:2024-10-11 19:08:18 瀏覽:402