Avoid listing of files that end with ~ (backup files)
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
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
linux command-line filenames
New contributor
add a comment |Â
up vote
8
down vote
favorite
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
linux command-line filenames
New contributor
It looks like you are trying to avoid listing files that end with a~
. These file are backup files created my some text editors.
â ctrl-alt-delor
4 hours ago
Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, â¦)?
â ctrl-alt-delor
4 hours ago
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
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
linux command-line filenames
New contributor
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
linux command-line filenames
linux command-line filenames
New contributor
New contributor
edited 23 mins ago
wjandrea
464312
464312
New contributor
asked 7 hours ago
curious_one
411
411
New contributor
New contributor
It looks like you are trying to avoid listing files that end with a~
. These file are backup files created my some text editors.
â ctrl-alt-delor
4 hours ago
Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, â¦)?
â ctrl-alt-delor
4 hours ago
add a comment |Â
It looks like you are trying to avoid listing files that end with a~
. These file are backup files created my some text editors.
â ctrl-alt-delor
4 hours ago
Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, â¦)?
â ctrl-alt-delor
4 hours ago
It looks like you are trying to avoid listing files that end with a
~
. These file are backup files created my some text editors.â ctrl-alt-delor
4 hours ago
It looks like you are trying to avoid listing files that end with a
~
. These file are backup files created my some text editors.â ctrl-alt-delor
4 hours ago
Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, â¦)?
â ctrl-alt-delor
4 hours ago
Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, â¦)?
â ctrl-alt-delor
4 hours ago
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
16
down vote
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.
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 -l | grep -v "~$"
add a comment |Â
up vote
14
down vote
ls
has an option for that: -B
Ignore backups:
ls --ignore-backups
4
That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD,ls -B
means something different.
â ilkkachu
6 hours ago
add a comment |Â
up vote
10
down vote
If you're using Bash, make sure extglob
is enable:
shopt -s extglob
Then you can use:
ls -d !(*~)
-d
to not show directories contents!(*~)
all files except the ones ending with~
On "zsh", you can do the same:
setopt extended_glob
ls -d ^*~
But the general idea is to use ls --ignore-backups
.
1
given the Linux tab, it's not a stretch to assume bash, butshopt -s extglob
is a bash-specific feature.
â Jeff Schaller
6 hours ago
@JeffSchaller You're right :) I updated my answer to expand it a little bit ...
â Ravexina
4 hours ago
add a comment |Â
up vote
4
down vote
This should help:
ls -l | grep -v '~'
Reason: The ~ char is replaced by your home directory before the command is executed. Try
echo ~
and
echo '~'
add a comment |Â
up vote
0
down vote
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)
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)
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
16
down vote
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.
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 -l | grep -v "~$"
add a comment |Â
up vote
16
down vote
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.
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 -l | grep -v "~$"
add a comment |Â
up vote
16
down vote
up vote
16
down vote
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.
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 -l | grep -v "~$"
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.
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 -l | grep -v "~$"
edited 2 hours ago
Foon
1585
1585
answered 7 hours ago
ilkkachu
51.7k678144
51.7k678144
add a comment |Â
add a comment |Â
up vote
14
down vote
ls
has an option for that: -B
Ignore backups:
ls --ignore-backups
4
That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD,ls -B
means something different.
â ilkkachu
6 hours ago
add a comment |Â
up vote
14
down vote
ls
has an option for that: -B
Ignore backups:
ls --ignore-backups
4
That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD,ls -B
means something different.
â ilkkachu
6 hours ago
add a comment |Â
up vote
14
down vote
up vote
14
down vote
ls
has an option for that: -B
Ignore backups:
ls --ignore-backups
ls
has an option for that: -B
Ignore backups:
ls --ignore-backups
answered 7 hours ago
hschou
1,803410
1,803410
4
That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD,ls -B
means something different.
â ilkkachu
6 hours ago
add a comment |Â
4
That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD,ls -B
means something different.
â ilkkachu
6 hours ago
4
4
That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD,
ls -B
means something different.â ilkkachu
6 hours ago
That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD,
ls -B
means something different.â ilkkachu
6 hours ago
add a comment |Â
up vote
10
down vote
If you're using Bash, make sure extglob
is enable:
shopt -s extglob
Then you can use:
ls -d !(*~)
-d
to not show directories contents!(*~)
all files except the ones ending with~
On "zsh", you can do the same:
setopt extended_glob
ls -d ^*~
But the general idea is to use ls --ignore-backups
.
1
given the Linux tab, it's not a stretch to assume bash, butshopt -s extglob
is a bash-specific feature.
â Jeff Schaller
6 hours ago
@JeffSchaller You're right :) I updated my answer to expand it a little bit ...
â Ravexina
4 hours ago
add a comment |Â
up vote
10
down vote
If you're using Bash, make sure extglob
is enable:
shopt -s extglob
Then you can use:
ls -d !(*~)
-d
to not show directories contents!(*~)
all files except the ones ending with~
On "zsh", you can do the same:
setopt extended_glob
ls -d ^*~
But the general idea is to use ls --ignore-backups
.
1
given the Linux tab, it's not a stretch to assume bash, butshopt -s extglob
is a bash-specific feature.
â Jeff Schaller
6 hours ago
@JeffSchaller You're right :) I updated my answer to expand it a little bit ...
â Ravexina
4 hours ago
add a comment |Â
up vote
10
down vote
up vote
10
down vote
If you're using Bash, make sure extglob
is enable:
shopt -s extglob
Then you can use:
ls -d !(*~)
-d
to not show directories contents!(*~)
all files except the ones ending with~
On "zsh", you can do the same:
setopt extended_glob
ls -d ^*~
But the general idea is to use ls --ignore-backups
.
If you're using Bash, make sure extglob
is enable:
shopt -s extglob
Then you can use:
ls -d !(*~)
-d
to not show directories contents!(*~)
all files except the ones ending with~
On "zsh", you can do the same:
setopt extended_glob
ls -d ^*~
But the general idea is to use ls --ignore-backups
.
edited 4 hours ago
answered 7 hours ago
Ravexina
1,147720
1,147720
1
given the Linux tab, it's not a stretch to assume bash, butshopt -s extglob
is a bash-specific feature.
â Jeff Schaller
6 hours ago
@JeffSchaller You're right :) I updated my answer to expand it a little bit ...
â Ravexina
4 hours ago
add a comment |Â
1
given the Linux tab, it's not a stretch to assume bash, butshopt -s extglob
is a bash-specific feature.
â Jeff Schaller
6 hours ago
@JeffSchaller You're right :) I updated my answer to expand it a little bit ...
â Ravexina
4 hours ago
1
1
given the Linux tab, it's not a stretch to assume bash, but
shopt -s extglob
is a bash-specific feature.â Jeff Schaller
6 hours ago
given the Linux tab, it's not a stretch to assume bash, but
shopt -s extglob
is a bash-specific feature.â Jeff Schaller
6 hours ago
@JeffSchaller You're right :) I updated my answer to expand it a little bit ...
â Ravexina
4 hours ago
@JeffSchaller You're right :) I updated my answer to expand it a little bit ...
â Ravexina
4 hours ago
add a comment |Â
up vote
4
down vote
This should help:
ls -l | grep -v '~'
Reason: The ~ char is replaced by your home directory before the command is executed. Try
echo ~
and
echo '~'
add a comment |Â
up vote
4
down vote
This should help:
ls -l | grep -v '~'
Reason: The ~ char is replaced by your home directory before the command is executed. Try
echo ~
and
echo '~'
add a comment |Â
up vote
4
down vote
up vote
4
down vote
This should help:
ls -l | grep -v '~'
Reason: The ~ char is replaced by your home directory before the command is executed. Try
echo ~
and
echo '~'
This should help:
ls -l | grep -v '~'
Reason: The ~ char is replaced by your home directory before the command is executed. Try
echo ~
and
echo '~'
answered 7 hours ago
Nico Mittenzwey
8114
8114
add a comment |Â
add a comment |Â
up vote
0
down vote
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)
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)
add a comment |Â
up vote
0
down vote
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)
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)
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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)
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)
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)
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)
answered 41 mins ago
Greg A. Woods
33026
33026
add a comment |Â
add a comment |Â
curious_one is a new contributor. Be nice, and check out our Code of Conduct.
curious_one is a new contributor. Be nice, and check out our Code of Conduct.
curious_one is a new contributor. Be nice, and check out our Code of Conduct.
curious_one is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471577%2favoid-listing-of-files-that-end-with-backup-files%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It looks like you are trying to avoid listing files that end with a
~
. These file are backup files created my some text editors.â ctrl-alt-delor
4 hours ago
Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, â¦)?
â ctrl-alt-delor
4 hours ago