![](/img/trans.png)
[英]How to make a bash script that will use cdhit on each file in the directory separately?
[英]How to make a new file for each directory in another folder using bash?
说我有一个像这样的文件结构:
stuff/
foo/
foo1.txt
foo2.txt
bar/
bar1.txt
other/
我想要一个bash命令,在这种情况下, stuff/
查找当前目录中的所有目录,并在另一个目录中创建具有这些目录名称的文件,在这种情况下, other/
具有所需的扩展名。 .csv
。
结果将如下所示:
stuff/
foo/
foo1.txt
foo2.txt
bar/
bar1.txt
other/
foo.csv
bar.csv
这个问题在“您尝试了什么”方面有点模糊,但我会纵容您。
要列出所有目录,我们可以使用find
:
$ cd stuff
$ find . -mindepth 1 -maxdepth 1 -type d
./bar
./foo
我们可以将其传递到while
循环中,该循环使用read -r
将每个连续的目录名称提取到一个变量中,在此为dirname
:
$ find . -mindepth 1 -maxdepth 1 -type d | while IFS="" read -r dirname; do echo $dirname; done
./bar
./foo
最后,除了echo
显目录名称外,我们还可以运行touch
创建具有所需名称的文件:
$ find . -mindepth 1 -maxdepth 1 -type d | while IFS="" read -r dirname; do touch ../other/$dirname.csv; done
$ ls ../other
bar.csv foo.csv
替代方法! 因为有很多方法可以给猫皮。
我们可以使用ls -d */
列出所有目录:
$ ls -d */
bar/ foo/
然后,我们使用sed
去除/
并添加路径和文件扩展名:
$ ls -d */ | sed 's#\(.*\)/#../other/\1.csv#'
../other/bar.csv
../other/foo.csv
然后,我们使用xargs
对以下每个文件名运行touch
:
$ ls -d */ | sed 's#\(.*\)/#../other/\1.csv#' | xargs touch
$ ls ../other
bar.csv foo.csv
尝试这个:
#! /bin/bash
function read_and_cp() {
for file in `ls $1`
do
if [ -d $1/${file} ]; then
touch $2/${file}.csv
read_and_cp $1/${file} $2
fi
done
}
read_and_cp stuff other
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.