Linux Shell 脚本:将目录的内容列出到文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5408402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:23:05  来源:igfitidea点击:

Linux Shell Script: list contents of a directory into a file

linux

提问by Jim

i have to write a shell script that gets a directory name as a parameter and then lists the names and sizes of the files in the directory into a text file. I have little knowledge of linux so can you please help me?

我必须编写一个 shell 脚本,它获取目录名称作为参数,然后将目录中文件的名称和大小列出到文本文件中。我对linux知之甚少,所以你能帮我吗?

All i managed to write is this:

我设法写的是这样的:

for entry in "$search_dir"/* 
do
  if [ -f "$entry" ];then
    echo "$entry"
  fi
done

The output file should look like this:

输出文件应如下所示:

filename1 filesize1
filename2 filesize2

I have problems about getting the directory name as a parameter

我在获取目录名称作为参数时遇到问题

回答by sharpner

like this:

像这样:

#! /bin/bash
if [ $# -gt 0 ] ; then
   ls  > textfile.txt
else
    echo "Please provide Foldername";
fi

additionally you could check if $1 is a folder but this should suffice

另外你可以检查 $1 是否是一个文件夹,但这应该就足够了

回答by Nick Weaver

You could easily put the output of a script into a file using >

您可以使用>轻松地将脚本的输出放入文件中

For example

例如

ls /tmp > ./contentsOfDir.txt

will dump the ls command into the contentsOfDir.txt in your current directory.

将 ls 命令转储到当前目录中的 contentsOfDir.txt 中。

The script could look like this for a bash shell:

对于 bash shell,脚本可能如下所示:

#!/bin/bash
ls -l  > contentsOfDir.txt

and is called

并被称为

./myScript dirNameToBeDumpedInFile

Have a look at this bash scripting tutorial, it covers the basics.

看看这个 bash 脚本教程,它涵盖了基础知识。

回答by knittl

#!/bin/sh

if [ ! -d "" ]; then
  echo "usage: 
4.0K    ./bundle.h
24K ./walker.o
4.4M    ./git-show
4.0K    ./sha1-lookup.h
100K    ./refs.o
<directory>"; exit 1; fi cd ; find -maxdepth 1 -type f -print0 | xargs -0 -n1 du -h; cd -;

this will output something like:

这将输出如下内容:

sudo apt-get install tree

回答by Ramy Nasr

If you want to print a pretty tree of the folder content and it's subfolders, you can use "tree".

如果要打印文件夹内容及其子文件夹的漂亮树,可以使用“树”。

If it's not install on your system, You need to do so first:

如果它没有安装在你的系统上,你需要先这样做:

tree -h -A path/to/dir > output.txt

Then the syntax is pretty simple. If you want to save the output to a file:

然后语法非常简单。如果要将输出保存到文件:

> tree --help
  -a            All files are listed.
  -d            List directories only.
  -l            Follow symbolic links like directories.
  -f            Print the full path prefix for each file.
  -L level      Descend only level directories deep.
  -o filename   Output to file instead of stdout.
  -s            Print the size in bytes of each file.
  -h            Print the size in a more human readable way.
  --dirsfirst   List directories before files (-U disables).
  • -Ais used to make sure the output is using ASCII characters.
  • -hwill print each file size in a human readable format.
  • -A用于确保输出使用 ASCII 字符。
  • -h将以人类可读的格式打印每个文件大小。

You have more options to limit the output that you can get by using the "--help" option:

您有更多选项来限制使用“--help”选项可以获得的输出:

##代码##