从输出中删除./

时间:2012-04-08 18:23:36

标签: linux bash

我有一个代码将数据输出到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"

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

由于

2 个答案:

答案 0 :(得分:3)

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

  find ${DIR} -type f -regex ".*\.${TYPES_RE}" -exec basename '{}' \;

答案 1 :(得分:0)

变化

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

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