简体   繁体   中英

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:

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

Another easy way use AWK to change content and save to file via.tmp

This script will print content how you want. Just add "1" and "$" to begining and ending accordingly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM