简体   繁体   English

如何在单个文本文件中保存目录中所有文件的列表并添加前缀和后缀?

[英]How to save a list of all files in a directory in a single text file and add prefixes and suffixes?

I am trying to save a list of files in a directory into a single file using我正在尝试使用将目录中的文件列表保存到单个文件中

ls > output.txt

Let's say we have in the directory:假设我们在目录中:

a.txt
b.txt
c.txt

I want to modify the names of these files in the output.txt to be like:我想将output.txt中这些文件的名称修改为:

1a.txt$
1b.txt$
1c.txt$

Another easy way use AWK to change content and save to file via.tmp另一种简单的方法是使用 AWK 更改内容并保存到文件 via.tmp

This script will print content how you want.该脚本将按照您的需要打印内容。 Just add "1" and "$" to begining and ending accordingly.只需相应地在开头和结尾添加“1”和“$”。

cat output.txt  | awk '{print "1"$1"$"}'

And then you can save to original file as you want by extending command && (if success then next )然后您可以通过扩展命令 && (如果成功则下一步)根据需要保存到原始文件

cat output.txt  | awk '{print "1"$1"$"}' > output.txt.tmp && mv output.txt.tmp output.txt
#!/bin/sh -x
for f in *.txt
do
nf=$(echo "${f}" | sed 's@^@1@')
mv -v "${f}" "${nf}"
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将目录中的所有文件名保存为bash中的文本文件? - How can I save all filenames in a directory to a text file in bash? bash:使用文件名列表跨目录连接匹配文件并将所有文件保存在新目录中 - bash: use list of file names to concatenate matching files across directories and save all files in new directory 如何将文本文件拆分为具有数字后缀但没有0前缀的较小文件 - How to split text files into smaller files with numeric suffixes but no 0 prefix 如何删除所有非单一文件的Linux目录? - How to remove all not single files Linux directory? 使用文本文件中的列表重命名目录中的文件 - Renaming files in a directory with a list in Text file 如何根据最新时间列出目录中的文件并遍历该文件列表并将其保存在数组中以进行模式匹配 - How to list files in a directory based on latest time and loop through that file list and save it in an array for pattern matching 如何在Mac终端中列出给定目录的所有文件,但文件或目录除外 - How to list all files of a given directory in Mac terminal except a file or directory 使用 Bash 从 for 循环中列出所有带有前缀的文件 - List all the files with prefixes from a for loop using Bash 列出目录中的文件而bashand中没有扩展名保存到另一个文件 - list files in a directory without extention in bashand save to another file 列出最长的多个目录前缀 - List the longest multiple directory prefixes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM