确定文件或目录是否隐藏

Jos*_*tes 0 bash filenames

我正在尝试编写一个 bash 脚本,该脚本递归地打印出目录中的所有文件(包括隐藏文件)并记录文件、隐藏文件、隐藏目录和目录的数量。这是作业的一部分,我不能使用-Rorfinddu

listAllFiles() 
 {
  local dir=$1
  local file
  directoryCounter=0
  fileCounter=0
  hiddenFileCounter=0
  hiddenDirectoryCounter=0

  for file in "$dir"/*; do
  if [[ -d $file ]]; then
      listAllFiles "$file"
      directoryCounter+=1
  elif [[ -f $file ]];then
      fileCounter+=1]
      ls -l $file
  elif [[file is a hidden directory]];then
      listAllFile "$file"
      hiddenDirectoryCounter+=1
  elif [[file is a hidden file]];then
      hiddenFileCounter+=1
      ls -l $file
  fi
     done
 }
Run Code Online (Sandbox Code Playgroud)

有没有办法检测文件/目录是否被隐藏

Jos*_* R. 5

隐藏文件和目录的名称以 开头.,因此您可以在 Bash 中使用以下解决方案:

# Skip '.' and '..':
if [ "$file_name" = . ] || [ "$file_name" = .. ];then
    continue
fi
# Find hidden files:
if [[ "$file_name" =~ ^\. ]];then # if file name starts with a .
...
Run Code Online (Sandbox Code Playgroud)