Homework_2020Bioinformatics

Bash

Posted by Yajin on October 6, 2020

YajinMo_Homework_2020/10/06

Task

写出一个 bash 脚本,可以使它自动读取一个文件夹(例如bash_homework/)的内容,将该文件夹下文件的名字输出到 filenames.txt, 子文件夹的名字输出到 dirname.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ ls
bash_homework YajinMo_20201007_Homework4.sh
$ cat YajinMo_20201007_Homework4.sh
#!/bin/bash
# retrive all file names and directer names in specified path

# input specified path
echo -n "input your specified path:"
read path

files=$(ls -al "$path" | grep "^-" | awk '{print $9}')
for mo in $files
do
 echo $mo >> ./filename.txt
done

dirs=$(ls -al "$path" | grep "^d" | awk '{print $9}')
for mo in $dirs
do
 echo $mo >> ./dirname.txt
done
$ sh YajinMo_20201007_Homework4.sh
input your specified path:./bash_homework
$ ls
bash_homework dirname.txt YajinMo_20201007_Homework4.sh filename.txt

Downloadable Content

  1. bash脚本 YajinMo_20201007_Homework4.sh

  2. 输出结果 filename.txt

  3. 输出结果 dirname.txt