My requirement is to list all files in a directory, except files ending with a ~
(backup files).
我的要求是列出目录中的所有文件,但以〜(备份文件)结尾的文件除外。
I tried to use command:
我试着用命令:
ls -l | grep -v ~
I get this output:
我得到这个输出:
asdasad
asdasad~
file_names.txt
normaltest.txt
target_filename
testshell1.sh
testshell1.sh~
testshell2.sh
testshell2.sh~
testtwo.txt
testtwo.txt~
test.txt
test.txt~
I want to get only these files:
我想只获取这些文件:
asdasad
file_names.txt
normaltest.txt
target_filename
testshell1.sh
testshell2.sh
testtwo.txt
test.txt
6 个解决方案
#1
41
The GNU implementation of ls
(found on most Linux systems) has an option for that: -B
Ignore backups:
ls的GNU实现(在大多数Linux系统上都有)有一个选项:-B忽略备份:
ls --ignore-backups
#2
40
ls -l | grep -v ~
The reason this doesn't work is that the tilde gets expanded to your home directory, so grep
never sees a literal tilde. (See e.g. Bash's manual on Tilde Expansion.) You need to quote it to prevent the expansion, i.e.
这不起作用的原因是代字号扩展到您的主目录,因此grep永远不会看到文字代字号。 (参见例如Bash关于Tilde扩展的手册。)你需要引用它以防止扩展,即
ls -l | grep -v "~"
Of course, this will still remove any output lines with a tilde anywhere, even in the middle of a file name or elsewhere in the ls
output (though it's probably not likely to appear in usernames, dates or such). If you really only want to ignore files that end with a tilde, you can use
当然,即使在文件名的中间或ls输出中的其他地方,这仍将删除任何波形符输出行(尽管它可能不会出现在用户名,日期等中)。如果你真的只想忽略以波浪号结尾的文件,你可以使用
ls -l | grep -v "~$"
#3
18
If you're using Bash, make sure extglob
is enabled:
如果您正在使用Bash,请确保启用了extglob:
shopt -s extglob
Then you can use:
然后你可以使用:
ls -d !(*~)
-
-d
to not show directories' contents -
!(*~)
all files except the ones ending with~
-d不显示目录的内容
!(*〜)除以〜结尾的文件外的所有文件
On "zsh", you can do the same:
在“zsh”上,您可以这样做:
setopt extended_glob
ls -d ^*~
But the general idea is to use ls --ignore-backups
.
但一般的想法是使用ls --ignore-backups。
#4
9
This should help:
这应该有所帮助:
ls -l | grep -v '~'
Reason: The ~ char is replaced by your home directory before the command is executed. Try
原因:在执行命令之前,~copy被主目录替换。尝试
echo ~
and
echo '~'
#5
4
As mentioned in other replies the reason you're likely having problems is because you didn't quote or escape the tilde.
正如其他回复中所提到的,你可能遇到问题的原因是因为你没有引用或逃脱波浪号。
For one particular use case I've used shell filename expansion to do something similar. I've been using it since about 2003, which means it has worked on a very wide variety of shell implementations on different kinds of systems. (here I also use -C
because I want a nice display in sorted columns)
对于一个特定的用例,我使用shell文件名扩展来做类似的事情。我从2003年开始使用它,这意味着它已经在不同类型的系统上进行了各种各样的shell实现。 (这里我也使用-C因为我希望在排序列中有一个很好的显示)
ls -C *[!~]
(a limitation with using shell filename expansion is of course is that a directory with very large number of (non-backup) files will cause the expansion to exceed the available argument list size, though on most modern systems this limit is quite high, often 250KB or more, sometimes much more)
(使用shell文件名扩展的限制当然是具有大量(非备份)文件的目录将导致扩展超过可用参数列表大小,但在大多数现代系统上,此限制非常高,通常250KB或更多,有时甚至更多)
#6
-1
ls | grep -v '~$'
is the quick solution. It uses ls to list all (non-hidden) files, then uses grep with -v
(inverted matching, i.e., exclusion) to exclude all lines with the tilde (~
) at the end ($
).
ls | grep -v'〜$'是快速解决方案。它使用ls列出所有(非隐藏)文件,然后使用带有-v(反向匹配,即排除)的grep来排除末尾($)的波浪号(〜)的所有行。
HOWEVER, if you have ANY files named like something1, something2, it's a HUGE smelly indicator that you have a bad workflow, which you should fix at the source, not with clunky hacks like modifying how ls works.
但是,如果你有任何名为something1,something2的文件,它是一个巨大的臭味指示器,你有一个糟糕的工作流程,你应该在源头修复,而不是笨拙的黑客,如修改ls如何工作。
If you find yourself naming files with version numbers, like test1 and test2 or testone and testtwo, then what you really want is a version control system, like git
.
如果您发现自己使用版本号命名文件,例如test1和test2或者testone和testtwo,那么您真正想要的是版本控制系统,例如git。
This is especially true in your case, where you have multiple versions of multiple files, and the versioning seems to need to be coordinated between all of the files.
在您拥有多个文件的多个版本的情况下尤其如此,并且版本控制似乎需要在所有文件之间进行协调。
Version control systems let you essentially overlay a time window onto the filesystem, so you only see the one test
file, but under the hood (through the version control program) all versions are available to search through, retrieve, revert to, compare with, etc.
版本控制系统允许您基本上将时间窗口覆盖到文件系统上,因此您只能看到一个测试文件,但在引擎盖下(通过版本控制程序)所有版本都可用于搜索,检索,恢复,比较,等等
Once you have this set up, you no longer need backup files from your editor, because version control has the history of the files since you started using it, even if you've made a thousand changes since then. You can even "branch" the history timeline, trying out a new idea, and going back in time and then forward to try a different idea, if you need to.
完成此设置后,您不再需要编辑器中的备份文件,因为版本控制具有自您开始使用文件以来的文件历史记录,即使您从那时起已进行了一千次更改。您甚至可以“分支”历史时间轴,尝试新想法,然后回过头来尝试不同的想法,如果需要的话。
#1
41
The GNU implementation of ls
(found on most Linux systems) has an option for that: -B
Ignore backups:
ls的GNU实现(在大多数Linux系统上都有)有一个选项:-B忽略备份:
ls --ignore-backups
#2
40
ls -l | grep -v ~
The reason this doesn't work is that the tilde gets expanded to your home directory, so grep
never sees a literal tilde. (See e.g. Bash's manual on Tilde Expansion.) You need to quote it to prevent the expansion, i.e.
这不起作用的原因是代字号扩展到您的主目录,因此grep永远不会看到文字代字号。 (参见例如Bash关于Tilde扩展的手册。)你需要引用它以防止扩展,即
ls -l | grep -v "~"
Of course, this will still remove any output lines with a tilde anywhere, even in the middle of a file name or elsewhere in the ls
output (though it's probably not likely to appear in usernames, dates or such). If you really only want to ignore files that end with a tilde, you can use
当然,即使在文件名的中间或ls输出中的其他地方,这仍将删除任何波形符输出行(尽管它可能不会出现在用户名,日期等中)。如果你真的只想忽略以波浪号结尾的文件,你可以使用
ls -l | grep -v "~$"
#3
18
If you're using Bash, make sure extglob
is enabled:
如果您正在使用Bash,请确保启用了extglob:
shopt -s extglob
Then you can use:
然后你可以使用:
ls -d !(*~)
-
-d
to not show directories' contents -
!(*~)
all files except the ones ending with~
-d不显示目录的内容
!(*〜)除以〜结尾的文件外的所有文件
On "zsh", you can do the same:
在“zsh”上,您可以这样做:
setopt extended_glob
ls -d ^*~
But the general idea is to use ls --ignore-backups
.
但一般的想法是使用ls --ignore-backups。
#4
9
This should help:
这应该有所帮助:
ls -l | grep -v '~'
Reason: The ~ char is replaced by your home directory before the command is executed. Try
原因:在执行命令之前,~copy被主目录替换。尝试
echo ~
and
echo '~'
#5
4
As mentioned in other replies the reason you're likely having problems is because you didn't quote or escape the tilde.
正如其他回复中所提到的,你可能遇到问题的原因是因为你没有引用或逃脱波浪号。
For one particular use case I've used shell filename expansion to do something similar. I've been using it since about 2003, which means it has worked on a very wide variety of shell implementations on different kinds of systems. (here I also use -C
because I want a nice display in sorted columns)
对于一个特定的用例,我使用shell文件名扩展来做类似的事情。我从2003年开始使用它,这意味着它已经在不同类型的系统上进行了各种各样的shell实现。 (这里我也使用-C因为我希望在排序列中有一个很好的显示)
ls -C *[!~]
(a limitation with using shell filename expansion is of course is that a directory with very large number of (non-backup) files will cause the expansion to exceed the available argument list size, though on most modern systems this limit is quite high, often 250KB or more, sometimes much more)
(使用shell文件名扩展的限制当然是具有大量(非备份)文件的目录将导致扩展超过可用参数列表大小,但在大多数现代系统上,此限制非常高,通常250KB或更多,有时甚至更多)
#6
-1
ls | grep -v '~$'
is the quick solution. It uses ls to list all (non-hidden) files, then uses grep with -v
(inverted matching, i.e., exclusion) to exclude all lines with the tilde (~
) at the end ($
).
ls | grep -v'〜$'是快速解决方案。它使用ls列出所有(非隐藏)文件,然后使用带有-v(反向匹配,即排除)的grep来排除末尾($)的波浪号(〜)的所有行。
HOWEVER, if you have ANY files named like something1, something2, it's a HUGE smelly indicator that you have a bad workflow, which you should fix at the source, not with clunky hacks like modifying how ls works.
但是,如果你有任何名为something1,something2的文件,它是一个巨大的臭味指示器,你有一个糟糕的工作流程,你应该在源头修复,而不是笨拙的黑客,如修改ls如何工作。
If you find yourself naming files with version numbers, like test1 and test2 or testone and testtwo, then what you really want is a version control system, like git
.
如果您发现自己使用版本号命名文件,例如test1和test2或者testone和testtwo,那么您真正想要的是版本控制系统,例如git。
This is especially true in your case, where you have multiple versions of multiple files, and the versioning seems to need to be coordinated between all of the files.
在您拥有多个文件的多个版本的情况下尤其如此,并且版本控制似乎需要在所有文件之间进行协调。
Version control systems let you essentially overlay a time window onto the filesystem, so you only see the one test
file, but under the hood (through the version control program) all versions are available to search through, retrieve, revert to, compare with, etc.
版本控制系统允许您基本上将时间窗口覆盖到文件系统上,因此您只能看到一个测试文件,但在引擎盖下(通过版本控制程序)所有版本都可用于搜索,检索,恢复,比较,等等
Once you have this set up, you no longer need backup files from your editor, because version control has the history of the files since you started using it, even if you've made a thousand changes since then. You can even "branch" the history timeline, trying out a new idea, and going back in time and then forward to try a different idea, if you need to.
完成此设置后,您不再需要编辑器中的备份文件,因为版本控制具有自您开始使用文件以来的文件历史记录,即使您从那时起已进行了一千次更改。您甚至可以“分支”历史时间轴,尝试新想法,然后回过头来尝试不同的想法,如果需要的话。