如何列出所有现有扩展名的文件名

时间:2015-11-06 14:09:05

标签: git bash

如果有人能帮助我,我将非常感激。 我需要一个表单输出:

home/Documents/some-cool-doc 
.docx 
.html 
.js

2 个答案:

答案 0 :(得分:2)

试试这个:

ls -Xp | grep -Eo "\.[^/]+$" | uniq

答案 1 :(得分:0)

首先请务必使用associative array

伪代码

For each file in directory
  store the filename,
  append the file extension to the value

For each filename indexed
  print the filename and associated extensions

#!/usr/bin/env bash

declare -A filenames

for file in *; do
    filename="${file%.*}"
    ext="${file##*.}"

    printf "$filename . $ext\n"
    filenames["$filename"]="${filenames["$filename"]}"$ext",";
done;

for filename in "${!filenames[@]}"; do
    printf "$filename \n\t ${filenames["$filename"]}\n";
done

输出

content-requirements-example
         zip,
user-journey.pptx
         zip,
Ergonomic-criteria-for-the-evaluation-of-human-computer-interfaces-J.M.-Christian-Bastien,-Dominique-L.-Scapin
         pdf,
UX_Curve_templates_eng_new
         doc,pdf,

缺点

可能有一些角落案例,随时可以适应。