脚本应列出当前目录文件除以后缀(并结束没有后缀的列表)。
示例(也是“.extension:”)
.c: first.c main.c var.c
.h: const.h first.h
.odt: relazione.odt
makefile README COPYING
我正在尝试使用ls,sort,uniq,但我不能。 有人可以帮帮我吗?
我在python中写了一个解决方案:
#!/usr/bin/env python3
import os
dictionary={}
for dirpath,_,files in os.walk("./"):
for f in files:
path = os.path.abspath(os.path.join(dirpath, f))
_ , ext = os.path.splitext(path)
if ext not in dictionary:
dictionary[ext] = []
dictionary[ext].append(f)
for k in dictionary:
if k != "":
print(k, end=": ")
for i in dictionary.get(k):
print(i, end= " ")
print("")
for i in dictionary.get(""):
print(i, end= " ")
print("")
答案 0 :(得分:3)
可以使用Perl。
use warnings;
use strict;
die "Too many args. Please supply one directory\n" if @ARGV > 1;
die "Too few args. Please supply one directory\n" if @ARGV < 1;
opendir (my $dir, "$ARGV[0]") || die "$ARGV[0]: $!\n" ;
my %extfiles;
my @Others;
while (my $file = readdir $dir){
next if (-d "./$file");
if($file =~ /^..*(\.[^\.]*)$/){
$extfiles{$1}=$extfiles{$1}?"$extfiles{$1} $file":"$file"
}
else{
push @Others,$file
}
}
closedir $dir;
#print "Files with extensions\n\n";
for my $extension (keys %extfiles){
print "$extension: $extfiles{$extension}\n";
}
#print "\nFiles without extensions\n\n";
print join (" ",@Others),"\n";
保存在文件中,例如
ListExt.pl
运行方式:
perl ListExt.pl $Dir
答案 1 :(得分:2)
您可以使用此find + awk
脚本:
find . -maxdepth 1 -type f -print0 |
awk -v RS='\0' -F. '{$0 = substr($0, 3)} NF>1{ext[$NF] = ext[$NF] OFS $0; next}
{noext = noext $0 OFS} END{for (e in ext) print "." e ":" ext[e]; print noext}'
答案 2 :(得分:1)
awk '
BEGIN {
for (i=1; i<ARGC; i++) {
fname = ARGV[i]
n = split(fname,parts,/\./)
ext = ( n>1 ? parts[n] : "none" )
ext2files[ext] = ext2files[ext] OFS fname
}
for (ext in ext2files) {
print ext ":" ext2files[ext]
}
exit
}' *
答案 3 :(得分:0)
假设:
$ ls -lndp *
-rw-r--r-- 1 501 0 0 May 1 15:01 COPYING
-rw-r--r-- 1 501 0 0 May 1 15:01 README
-rw-r--r-- 1 501 0 0 May 2 06:36 a.b.c
drwxr-xr-x 2 501 0 68 May 2 06:15 adir.dir/
-rw-r--r-- 1 501 0 0 May 1 15:00 const.h
-rw-r--r-- 1 501 0 0 May 2 08:00 file name with a space.doc
-rw-r--r-- 1 501 0 0 May 1 15:00 first.c
-rw-r--r-- 1 501 0 0 May 1 15:00 first.h
-rw-r--r-- 1 501 0 0 May 1 15:00 main.c
-rw-r--r-- 1 501 0 0 May 1 15:01 makefile
-rw-r--r-- 1 501 0 0 May 1 15:01 relazione.odt
-rw-r--r-- 1 501 0 0 May 1 15:00 var.c
假设您不想包含目录,可以过滤掉目录,只将文件名提供给awk
:
awk '{n=split($0, parts, /\./)
ext = ( n>1 ? parts[n] : "none" )
ext2files[ext] = ext2files[ext] OFS $0
}
END{
for (ext in ext2files)
print ext ":" ext2files[ext]
}' <(for fn in *; do [ ! -d "$fn" ] && echo "$fn"; done)
h: const.h first.h
none: COPYING README makefile
odt: relazione.odt
doc: file name with a space.doc
c: a.b.c first.c main.c var.c
如果您希望输出已排序,并且您有gawk
,则可以编写比较函数:
gawk ' function cmp_idx(i1, v1, i2, v2) {
if (i1=="" || i2=="")
return (i1=="") ? 1 : -1
return (i1 < i2) ? -1 : (i1 != i2)
}
{
n=split($0, parts, /\./)
ext = ( n>1 ? parts[n] : "" )
ext2files[ext] = (ext2files[ext] ? ext2files[ext] OFS $0 : $0)
}
END{
PROCINFO["sorted_in"] = "cmp_idx"
for (ext in ext2files)
print (ext ? ext ": " ext2files[ext] : ext2files[ext])
}' <(for fn in *; do [ ! -d "$fn" ] && echo "$fn"; done)
c: a.b.c first.c main.c var.c
doc: file name with a space.doc
h: const.h first.h
odt: relazione.odt
COPYING README makefile
答案 4 :(得分:-1)
#!/bin/bash
ls -d *.* 2>/dev/null | cut -d. -f2- | sort -u | while read i ; do
echo -n ."$i":' '
for j in *."$i" ; do
echo -n "$j"' '
done
echo
done
ls | grep -v '\.' | tr '\n' ' '