1

Is there a series of commands that does ls then removes backup files? I want to do something like

ls | grep -v *~

but this shows all the files in different lines, any one to make the output identical to ls?

When I type in "man ls" My man page for ls has this option of -B its

   -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.

It is not identical to the one you showed and I searched for ignored but no results popped up. Btw I am on a mac, which might have a different version of ls?

Alternatively, can I tell a directory to stop making backup files?

Doboy
  • 10,411
  • 11
  • 40
  • 48

3 Answers3

9

Assuming ls from GNU coreutils,

       -B, --ignore-backups
              do not list implied entries ending with ~

You can also set FIGNORE='~' in Bash so that * never expands to contain filenames ending in ~.

ephemient
  • 198,619
  • 38
  • 280
  • 391
3

You can list all files ending in ~ with:

ls -d *[^~]

The *[^~] specifies all files that don't end in ~. The -d flag tells ls not to show the directory contents for any directories that it matches (as with the default ls command).

Edit: If you alias your ls to use the command above, it will break the standard ls usage, so you're better off using ephemient's solution if you want your ls usage to always exclude backup files.

Mansoor Siddiqui
  • 20,853
  • 10
  • 48
  • 67
0

For people forced to use ls that doesn't have -B (e.g., using BSD ls in Mac OS X), you can create an alias to a bash function that is based on Mansoor Siddiqui's suggestion. If you add the following function to your bash profile where you keep your aliases (.bash_profile, .profile, .bashrc, .bash_aliases, or equivalent):

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

Then l will be mapped to ls but not show files that end in ~ or #.

dr jimbob
  • 17,259
  • 7
  • 59
  • 81