Bash 为ls创建别名,使其不显示该模式的文件*~

Bash 为ls创建别名,使其不显示该模式的文件*~,bash,grep,alias,ls,Bash,Grep,Alias,Ls,是否有一系列命令可以让ls删除备份文件?我想做一些像 ls | grep -v *~ 但这会在不同的行中显示所有文件,有哪一行使输出与ls相同 当我输入man ls时,我的ls手册页有-B选项 -B Force printing of non-printable characters (as defined by ctype(3) and current locale settings) in file names as \xxx, where xxx i

是否有一系列命令可以让ls删除备份文件?我想做一些像

ls | grep -v *~
但这会在不同的行中显示所有文件,有哪一行使输出与ls相同

当我输入man ls时,我的ls手册页有-B选项

   -B      Force printing of non-printable characters (as defined by ctype(3)
           and current locale settings) in file names as \xxx, where xxx is the
           numeric value of the character in octal.
它与您显示的不一样,我搜索了忽略,但没有弹出结果。顺便说一句,我在mac电脑上,可能有不同版本的ls


或者,我可以告诉目录停止制作备份文件吗?

您可以列出所有以~结尾的文件,其中包括:

*[^~]指定所有不以~结尾的文件。d标志告诉ls不要显示与默认ls命令匹配的任何目录的目录内容


编辑:如果您将ls别名为使用上述命令,它将打破标准ls用法,因此如果您希望ls用法始终排除备份文件,则最好使用ephemient的解决方案。

您可以列出以~结尾的所有文件,包括:

*[^~]指定所有不以~结尾的文件。d标志告诉ls不要显示与默认ls命令匹配的任何目录的目录内容

编辑:如果您将ls别名为使用上述命令,它将破坏标准ls用法,因此如果您希望ls用法始终排除备份文件,最好使用ephemient的解决方案。

假设ls来自GNU coreutils

-B, --ignore-backups do not list implied entries ending with ~ 您还可以在Bash中设置FIGNORE='~',以便*永远不会扩展为包含以~.结尾的文件名。

假设来自GNU coreutils的ls

-B, --ignore-backups do not list implied entries ending with ~ 您还可以在Bash中设置FIGNORE='~',以便*永远不会扩展为包含以~.结尾的文件名。

对于被迫使用没有-B的ls的人,例如,在Mac OS X中使用BSD ls,您可以根据Mansoor Siddiqui的建议为Bash函数创建别名。如果将以下函数添加到保留别名的bash配置文件中。bash_配置文件、.profile、.bashrc、.bash_别名或等效项:

ls_no_hidden() {
  nonflagcount=0
  ARG_ARRAY=(${@})
  flags="-l"
  curdir=`pwd`
  shopt -s nullglob
  # Iterate through args, find all flags (arg starting with dash (-))
  for (( i = 0; i < $# ; i++ )); do
    if [[ ${ARG_ARRAY[${i}]} == -* ]]; then
      flags="${flags} ${ARG_ARRAY[${i}]}";
    else
      ((nonflagcount++));
    fi
  done

  if [[ $nonflagcount -eq 0 ]]; then
    # ls current directory if no non-flag args provided
    FILES=`echo *[^~#]` 
    # check if files are present, before calling ls 
    # to suppress errors if no matches.
    if [[ -n $FILES ]]; then
      ls $flags -d *[^~#]
    fi
  else
    # loop through all args, and run ls for each non-flag
    for (( i = 0; i < $# ; i++ )); do
      if [[ ${ARG_ARRAY[${i}]} != -* ]]; then
        # if directory, enter the directory
        if [[ -d ${ARG_ARRAY[${i}]} ]]; then
          cd ${ARG_ARRAY[${i}]}
          # check that the cd was successful before calling ls
          if [[ $? -eq 0 ]]; then
            pwd # print directory you are listing (feel free to comment out)
            FILES=`echo *[^~#]`
            if [[ -n $FILES ]]; then
              ls $flags -d *[^~#]
            fi
            cd $curdir
          fi
        else
          # if file list the file
          if [[ -f ${ARG_ARRAY[${i}]} ]]; then
            ls $flags ${ARG_ARRAY[${i}]}
          else
            echo "Directory/File not found: ${ARG_ARRAY[${i}]}"
          fi
        fi
      fi
    done
  fi
}
alias l=ls_no_hidden
然后l将映射到ls,但不显示以~或结尾的文件

对于被迫使用没有-B的ls的用户,例如在Mac OS X中使用BSD ls,您可以根据Mansoor Siddiqui的建议为bash函数创建别名。如果将以下函数添加到保留别名的bash配置文件中。bash_配置文件、.profile、.bashrc、.bash_别名或等效项:

ls_no_hidden() {
  nonflagcount=0
  ARG_ARRAY=(${@})
  flags="-l"
  curdir=`pwd`
  shopt -s nullglob
  # Iterate through args, find all flags (arg starting with dash (-))
  for (( i = 0; i < $# ; i++ )); do
    if [[ ${ARG_ARRAY[${i}]} == -* ]]; then
      flags="${flags} ${ARG_ARRAY[${i}]}";
    else
      ((nonflagcount++));
    fi
  done

  if [[ $nonflagcount -eq 0 ]]; then
    # ls current directory if no non-flag args provided
    FILES=`echo *[^~#]` 
    # check if files are present, before calling ls 
    # to suppress errors if no matches.
    if [[ -n $FILES ]]; then
      ls $flags -d *[^~#]
    fi
  else
    # loop through all args, and run ls for each non-flag
    for (( i = 0; i < $# ; i++ )); do
      if [[ ${ARG_ARRAY[${i}]} != -* ]]; then
        # if directory, enter the directory
        if [[ -d ${ARG_ARRAY[${i}]} ]]; then
          cd ${ARG_ARRAY[${i}]}
          # check that the cd was successful before calling ls
          if [[ $? -eq 0 ]]; then
            pwd # print directory you are listing (feel free to comment out)
            FILES=`echo *[^~#]`
            if [[ -n $FILES ]]; then
              ls $flags -d *[^~#]
            fi
            cd $curdir
          fi
        else
          # if file list the file
          if [[ -f ${ARG_ARRAY[${i}]} ]]; then
            ls $flags ${ARG_ARRAY[${i}]}
          else
            echo "Directory/File not found: ${ARG_ARRAY[${i}]}"
          fi
        fi
      fi
    done
  fi
}
alias l=ls_no_hidden

然后l将映射到ls,但不显示以~或结尾的文件

+1,很好的解决方案。实际上,我刚刚意识到这比我的方法要好得多,因为如果将ls-B别名为ls,它不会破坏ls的用法。还有-I,-ignore=PATTERN。您也可以购买-s extglob,然后说ls*~我对这个解决方案有一些问题,请看我的后续问题。@Doboy那么你的ls是BSD的传统。如上所述,这只适用于GNU coreutils。有了BSD ls,你就不走运了。+1,很好的解决方案。实际上,我刚刚意识到这比我的方法要好得多,因为如果将ls-B别名为ls,它不会破坏ls的用法。还有-I,-ignore=PATTERN。您也可以购买-s extglob,然后说ls*~我对这个解决方案有一些问题,请看我的后续问题。@Doboy那么你的ls是BSD的传统。如上所述,这只适用于GNU coreutils。有了BSD ls,你就不走运了。