答案1
這是您可以使用的一個小 shell 函數。只需將這些行添加到您的~/.bashrc
:
lsc(){
## globs that don't match should expand to a null string
shopt -s nullglob
## If no arguments were given use the current dir
if [[ $# -eq 0 ]]; then
targets="."
## Otherwise, use whatever was given
else
targets=($@)
fi
## iterate over the arguments given
for target in "${targets[@]}"; do
## get the contents of the target
contents=("$target"/*)
## iterate over the contents
for thing in "${contents[@]}"; do
## If this one is a directory
if [[ -d "$thing" ]]; then
## collect the directory's contents
count=("$thing"/*)
## Print the dir's name (with a '/' at the end)
## and the number of items found in it
printf "%s/ (%s)\n" "$thing" "${#count[@]}"
else
## If this isn't a dir, just print the name
printf "%s\n" "$thing"
fi
done
done
}
然後打開一個新終端並運行:
lsc /path/to/dir
例如,給定以下目錄(\012
名稱中是換行符號):
$ tree
.
├── a bad one
│ └── file 1
├── a bad worse\012one
│ └── file 1 \012two
├── dir1
│ └── file
├── dir2
│ ├── file1
│ └── file2
├── dir3
│ ├── file1
│ ├── file2
│ └── file3
├── dir4
│ └── dir
├── empty_dir
8 directories, 7 files
你會得到:
$ lsc
./a bad one/ (1)
./a bad worse
one/ (1)
./dir1/ (1)
./dir2/ (2)
./dir3/ (3)
./dir4/ (1)
./empty_dir/ (0)
./mp3/ (1)
這種方法的主要優點是:
您可以在多個目錄上運行它:
lsc /path/to/dir /path/to/dir2 ... /path/to/dirN
或在當前的情況下:
lsc
它可以處理任意檔案和目錄名稱,甚至是那些包含空格或換行符的檔案和目錄名稱,如您在上面看到的。