1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > linux shell 获取表 bash - 如何获取shell脚本中目录中的文件列表?

linux shell 获取表 bash - 如何获取shell脚本中目录中的文件列表?

时间:2022-05-21 04:40:23

相关推荐

linux shell 获取表 bash  - 如何获取shell脚本中目录中的文件列表?

bash - 如何获取shell脚本中目录中的文件列表?

我试图使用shell脚本获取目录的内容。

我的脚本是:

for entry in `ls $search_dir`; do

echo $entry

done

其中$search_dir是相对路径。 但是,$work_dir包含许多名称中包含空格的文件。 在这种情况下,此脚本不会按预期运行。

我知道我可以使用$search_dir,但这只适用于我当前的目录。

我知道我可以更改到该目录,使用$search_dir然后更改回来,但我的特殊情况阻止我这样做。

我有两个相对路径$search_dir和$work_dir,我必须同时处理它们,读取它们创建/删除它们中的文件等。

那我现在该怎么办?

PS:我用bash。

9个解决方案

215 votes

for entry in "$search_dir"/*

do

echo "$entry"

done

Ignacio Vazquez-Abrams answered -06-27T17:51:20Z

21 votes

这里的其他答案很棒并且回答你的问题,但这是" bash获取目录中文件列表的最佳google结果",(我正在寻找保存文件列表)所以我 以为我会回答这个问题:

ls $search_path > filename.txt

如果您只想要某种类型(例如任何.txt文件):

ls $search_path | grep *.txt > filename.txt

请注意,$ search_path是可选的; ls> filename.txt将执行当前目录。

tegan answered -06-27T17:51:58Z

17 votes

for entry in "$search_dir"/* "$work_dir"/*

do

if [ -f "$entry" ];then

echo "$entry"

fi

done

ghostdog74 answered -06-27T17:52:16Z

13 votes

这是一种方法,我可以更简单地理解语法:

yourfilenames=`ls ./*.txt`

for eachfile in $yourfilenames

do

echo $eachfile

done

yourfilenames是当前工作目录,但可以替换为任何路径

eachfile返回anything.txt

您可以直接在终端中输入ls命令来查看将要列出的内容。

基本上,您创建一个变量yourfilenames,其中包含list命令作为单独元素返回的所有内容,然后循环遍历它。 该循环创建一个临时变量eachfile,其中包含循环的变量的单个元素,在本例中为文件名。 这并不一定比其他答案更好,但我觉得它很直观,因为我已经熟悉了ls命令和for循环语法。

rrr answered -06-27T17:53:11Z

10 votes

find "${search_dir}" "${work_dir}" -mindepth 1 -maxdepth 1 -type f -print0 | xargs -0 -I {} echo "{}"

Noel Yap answered -06-27T17:53:30Z

0 votes

这是在目录中列出文件的另一种方式(使用不同的工具,效率不如其他一些答案)。

cd "search_dir"

for [ z in `echo *` ]; do

echo "$z"

done

echo *输出当前目录的所有文件。 test -d循环遍历每个文件名并打印到stdout。

此外,如果在目录中查找目录,请将其放在test -d循环内:

if [ test -d $z ]; then

echo "$z is a directory"

fi

test -d检查文件是否是目录。

SnoopDogg answered -06-27T17:54:17Z

0 votes

在我使用的Linux版本(x86_64 GNU / Linux)下面的工作:

for entry in "$search_dir"/*

do

echo "$entry"

done

Andrushenko Alexander answered -06-27T17:54:44Z

0 votes

接受的答案不会返回带有a的文件前缀。要做到这一点

for entry in "$search_dir"/* "$search_dir"/.[!.]* "$search_dir"/..?*

do

echo "$entry"

done

TrevTheDev answered -06-27T17:55:10Z

0 votes

$ pwd; ls -l

/home/victoria/test

total 12

-rw-r--r-- 1 victoria victoria 0 Apr 23 11:31 a

-rw-r--r-- 1 victoria victoria 0 Apr 23 11:31 b

-rw-r--r-- 1 victoria victoria 0 Apr 23 11:31 c

-rw-r--r-- 1 victoria victoria 0 Apr 23 11:32 'c d'

-rw-r--r-- 1 victoria victoria 0 Apr 23 11:31 d

drwxr-xr-x 2 victoria victoria 4096 Apr 23 11:32 dir_a

drwxr-xr-x 2 victoria victoria 4096 Apr 23 11:32 dir_b

-rw-r--r-- 1 victoria victoria 0 Apr 23 11:32 'e; f'

$ find . -type f

./c

./b

./a

./d

./c d

./e; f

$ find . -type f | sed 's/^\.\///g' | sort

a

b

c

c d

d

e; f

$ find . -type f | sed 's/^\.\///g' | sort > tmp

$ cat tmp

a

b

c

c d

d

e; f

变化

$ pwd

/home/victoria

$ find $(pwd) -maxdepth 1 -type f -not -path '*/\.*' | sort

/home/victoria/new

/home/victoria/new1

/home/victoria/new2

/home/victoria/new3

/home/victoria/new3.md

/home/victoria/new.md

/home/victoria/package.json

/home/victoria/Untitled Document 1

/home/victoria/Untitled Document 2

$ find . -maxdepth 1 -type f -not -path '*/\.*' | sed 's/^\.\///g' | sort

new

new1

new2

new3

new3.md

new.md

package.json

Untitled Document 1

Untitled Document 2

笔记:

sed 's/^\.\///g':当前文件夹

删除sed 's/^\.\///g'以递归搜索

sed 's/^\.\///g':查找文件,而不是目录(./)

sed 's/^\.\///g':不要退货./

sed 's/^\.\///g':从结果列表中删除前置的./

Victoria Stuart answered -06-27T17:56:10Z

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。