1

我有一个将数据输出到 txt 文件的代码,但我想从每个文件名的输出中删除 ./

代码如下

#!/bin/bash

# fill with more extensions or have it as a cmd line arg
TYPES=( mov mp4 avi mp3 wma)

DIR=$1

# Create a regex of the extensions for the find command
echo "Available Media Files in Directory"

TYPES_RE="\\("${TYPES[1]}
for t in "${TYPES[@]:1:${#TYPES[*]}}"; do
    TYPES_RE="${TYPES_RE}\\|${t}"
done
TYPES_RE="${TYPES_RE}\\)"

# Set the field seperator to newline instead of space
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

# Generate output from path and size using: `stat -c "%s" filepath`
OUTPUT=""

for f in `find ${DIR} -type f -regex ".*\.${TYPES_RE}"`; do

OUTPUT=`echo ${f}`";"$OUTPUT

done

# Reset IFS
IFS=$SAVEIFS

# Reverse numeric sort the output and replace ; with \n for printing
echo $OUTPUT  | tr ';' '\n' | sed 's/.*/"&"/' | sort -nr >playlist.txt

结果是:

"./You Da One.mp3"
"./Wiz Khalifa Roll Up.mp4"
"./Vybz Kartel neva get a gal.mp3"
"./Tyga Rack City.mp4"
"./Tyga Lap Dance.mp4"
"./Travis Porter Make It Rain.mp4"
"./Travis Porter ft. Tyga Ayy Ladies.mp4"
"./Snoop Dogg feat. Wiz Khalifa Bruno Mars Young Wild & Free.mp4"
"./Shot Caller.mp3"
"./Chris Brown - Your Body.mp4"
"./Chris Brown Turn Up The Music.mp4"

需要从每个文件中删除 ./

谢谢

4

2 回答 2

3

您可以使用 basename 命令去除目录文件名,在您的脚本中,使用它的一个好方法可能是在 find 命令中,即

  find ${DIR} -type f -regex ".*\.${TYPES_RE}" -exec basename '{}' \;
于 2012-04-08T18:25:48.260 回答
0

change

echo $OUTPUT  | tr ';' '\n' | sed 's/.*/"&"/' | sort -nr >playlist.txt

to

echo $OUTPUT  | tr ';' '\n' | sed 's/^\.\///gi' | sed 's/.*/"&"/' | sort -nr >playlist.txt
于 2012-04-08T18:34:06.133 回答