exclude certain files in ls
up vote
39
down vote
favorite
I would like to run ls and exclude certain files in the output.
When I run the following command, I get all files with each on one line:
$ ls -1
file1
file2
file3
temp
I would like to run this command in a way so it shows
$ ls -1 <insert magic here> temp
file1
file2
file3
bash ls
add a comment |
up vote
39
down vote
favorite
I would like to run ls and exclude certain files in the output.
When I run the following command, I get all files with each on one line:
$ ls -1
file1
file2
file3
temp
I would like to run this command in a way so it shows
$ ls -1 <insert magic here> temp
file1
file2
file3
bash ls
add a comment |
up vote
39
down vote
favorite
up vote
39
down vote
favorite
I would like to run ls and exclude certain files in the output.
When I run the following command, I get all files with each on one line:
$ ls -1
file1
file2
file3
temp
I would like to run this command in a way so it shows
$ ls -1 <insert magic here> temp
file1
file2
file3
bash ls
I would like to run ls and exclude certain files in the output.
When I run the following command, I get all files with each on one line:
$ ls -1
file1
file2
file3
temp
I would like to run this command in a way so it shows
$ ls -1 <insert magic here> temp
file1
file2
file3
bash ls
bash ls
asked Aug 18 '14 at 14:54
Alice Ryhl
3671412
3671412
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
57
down vote
accepted
ls -I <filename>
-I = Ignore the filename, i.e., don't list the specified file.
To ignore more than one file add an -I before each filename.
ls -I file1 -I file2
To ignore files by their name extensions do the following, for example.
ls -I "*.jpg" -I "*.svg"
4
If you use the long-form option--ignore
you can extend that to glob patterns e.g.ls --ignore="file?"
orls --ignore="file*"
– steeldriver
Aug 18 '14 at 15:23
2
you can use glob patterns with the short form as well by quoting the patterns
– verboze
Dec 29 '15 at 5:42
add a comment |
up vote
15
down vote
For me if I use -I once, it works but if I use twice it doesn't.
e.g:
ls -I *.csv
works.
But
ls -I *.csv -I *.txt
doesn't work and returns txt files instead.
--ignore did the trick for me. This is what I needed and worked.
ls -lhrt --ignore="*.gz" --ignore="*.1"
That will list me files from log folder where it exclude old backup logs.
3
quoting *.csv in the first form works, e.g..,ls -I '*.txt'
. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
– verboze
Dec 29 '15 at 5:40
Double-quoting worked for me as well. Thanks @verboze
– user38537
Sep 29 '16 at 20:41
add a comment |
up vote
3
down vote
ls --ignore={"*.jpg","*.png","*.svg"}
3
This is simply using bash's brace expansion to get three--ignore
options. You could have also used--ignore="*."{jpg,png,svg}
– muru
Feb 14 '17 at 0:27
add a comment |
up vote
1
down vote
I think that this produces the output you're looking for:
ls -1 !(temp)
Apparently, you need shopt -s extglob
for that to work
(I have it enabled, so I guess some time in the distant past I found it useful and enabled it).
I guess you could also use grep to filter the output:
ls -1 | grep -v '^temp$'
Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
57
down vote
accepted
ls -I <filename>
-I = Ignore the filename, i.e., don't list the specified file.
To ignore more than one file add an -I before each filename.
ls -I file1 -I file2
To ignore files by their name extensions do the following, for example.
ls -I "*.jpg" -I "*.svg"
4
If you use the long-form option--ignore
you can extend that to glob patterns e.g.ls --ignore="file?"
orls --ignore="file*"
– steeldriver
Aug 18 '14 at 15:23
2
you can use glob patterns with the short form as well by quoting the patterns
– verboze
Dec 29 '15 at 5:42
add a comment |
up vote
57
down vote
accepted
ls -I <filename>
-I = Ignore the filename, i.e., don't list the specified file.
To ignore more than one file add an -I before each filename.
ls -I file1 -I file2
To ignore files by their name extensions do the following, for example.
ls -I "*.jpg" -I "*.svg"
4
If you use the long-form option--ignore
you can extend that to glob patterns e.g.ls --ignore="file?"
orls --ignore="file*"
– steeldriver
Aug 18 '14 at 15:23
2
you can use glob patterns with the short form as well by quoting the patterns
– verboze
Dec 29 '15 at 5:42
add a comment |
up vote
57
down vote
accepted
up vote
57
down vote
accepted
ls -I <filename>
-I = Ignore the filename, i.e., don't list the specified file.
To ignore more than one file add an -I before each filename.
ls -I file1 -I file2
To ignore files by their name extensions do the following, for example.
ls -I "*.jpg" -I "*.svg"
ls -I <filename>
-I = Ignore the filename, i.e., don't list the specified file.
To ignore more than one file add an -I before each filename.
ls -I file1 -I file2
To ignore files by their name extensions do the following, for example.
ls -I "*.jpg" -I "*.svg"
edited Feb 13 '17 at 22:48
jdthood
10.3k14161
10.3k14161
answered Aug 18 '14 at 14:57
Sudheer
3,22531826
3,22531826
4
If you use the long-form option--ignore
you can extend that to glob patterns e.g.ls --ignore="file?"
orls --ignore="file*"
– steeldriver
Aug 18 '14 at 15:23
2
you can use glob patterns with the short form as well by quoting the patterns
– verboze
Dec 29 '15 at 5:42
add a comment |
4
If you use the long-form option--ignore
you can extend that to glob patterns e.g.ls --ignore="file?"
orls --ignore="file*"
– steeldriver
Aug 18 '14 at 15:23
2
you can use glob patterns with the short form as well by quoting the patterns
– verboze
Dec 29 '15 at 5:42
4
4
If you use the long-form option
--ignore
you can extend that to glob patterns e.g. ls --ignore="file?"
or ls --ignore="file*"
– steeldriver
Aug 18 '14 at 15:23
If you use the long-form option
--ignore
you can extend that to glob patterns e.g. ls --ignore="file?"
or ls --ignore="file*"
– steeldriver
Aug 18 '14 at 15:23
2
2
you can use glob patterns with the short form as well by quoting the patterns
– verboze
Dec 29 '15 at 5:42
you can use glob patterns with the short form as well by quoting the patterns
– verboze
Dec 29 '15 at 5:42
add a comment |
up vote
15
down vote
For me if I use -I once, it works but if I use twice it doesn't.
e.g:
ls -I *.csv
works.
But
ls -I *.csv -I *.txt
doesn't work and returns txt files instead.
--ignore did the trick for me. This is what I needed and worked.
ls -lhrt --ignore="*.gz" --ignore="*.1"
That will list me files from log folder where it exclude old backup logs.
3
quoting *.csv in the first form works, e.g..,ls -I '*.txt'
. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
– verboze
Dec 29 '15 at 5:40
Double-quoting worked for me as well. Thanks @verboze
– user38537
Sep 29 '16 at 20:41
add a comment |
up vote
15
down vote
For me if I use -I once, it works but if I use twice it doesn't.
e.g:
ls -I *.csv
works.
But
ls -I *.csv -I *.txt
doesn't work and returns txt files instead.
--ignore did the trick for me. This is what I needed and worked.
ls -lhrt --ignore="*.gz" --ignore="*.1"
That will list me files from log folder where it exclude old backup logs.
3
quoting *.csv in the first form works, e.g..,ls -I '*.txt'
. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
– verboze
Dec 29 '15 at 5:40
Double-quoting worked for me as well. Thanks @verboze
– user38537
Sep 29 '16 at 20:41
add a comment |
up vote
15
down vote
up vote
15
down vote
For me if I use -I once, it works but if I use twice it doesn't.
e.g:
ls -I *.csv
works.
But
ls -I *.csv -I *.txt
doesn't work and returns txt files instead.
--ignore did the trick for me. This is what I needed and worked.
ls -lhrt --ignore="*.gz" --ignore="*.1"
That will list me files from log folder where it exclude old backup logs.
For me if I use -I once, it works but if I use twice it doesn't.
e.g:
ls -I *.csv
works.
But
ls -I *.csv -I *.txt
doesn't work and returns txt files instead.
--ignore did the trick for me. This is what I needed and worked.
ls -lhrt --ignore="*.gz" --ignore="*.1"
That will list me files from log folder where it exclude old backup logs.
answered Jul 13 '15 at 1:47
Damodar Bashyal
26137
26137
3
quoting *.csv in the first form works, e.g..,ls -I '*.txt'
. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
– verboze
Dec 29 '15 at 5:40
Double-quoting worked for me as well. Thanks @verboze
– user38537
Sep 29 '16 at 20:41
add a comment |
3
quoting *.csv in the first form works, e.g..,ls -I '*.txt'
. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
– verboze
Dec 29 '15 at 5:40
Double-quoting worked for me as well. Thanks @verboze
– user38537
Sep 29 '16 at 20:41
3
3
quoting *.csv in the first form works, e.g..,
ls -I '*.txt'
. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders– verboze
Dec 29 '15 at 5:40
quoting *.csv in the first form works, e.g..,
ls -I '*.txt'
. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders– verboze
Dec 29 '15 at 5:40
Double-quoting worked for me as well. Thanks @verboze
– user38537
Sep 29 '16 at 20:41
Double-quoting worked for me as well. Thanks @verboze
– user38537
Sep 29 '16 at 20:41
add a comment |
up vote
3
down vote
ls --ignore={"*.jpg","*.png","*.svg"}
3
This is simply using bash's brace expansion to get three--ignore
options. You could have also used--ignore="*."{jpg,png,svg}
– muru
Feb 14 '17 at 0:27
add a comment |
up vote
3
down vote
ls --ignore={"*.jpg","*.png","*.svg"}
3
This is simply using bash's brace expansion to get three--ignore
options. You could have also used--ignore="*."{jpg,png,svg}
– muru
Feb 14 '17 at 0:27
add a comment |
up vote
3
down vote
up vote
3
down vote
ls --ignore={"*.jpg","*.png","*.svg"}
ls --ignore={"*.jpg","*.png","*.svg"}
answered Feb 13 '17 at 22:11
Kartikey Tanna
1335
1335
3
This is simply using bash's brace expansion to get three--ignore
options. You could have also used--ignore="*."{jpg,png,svg}
– muru
Feb 14 '17 at 0:27
add a comment |
3
This is simply using bash's brace expansion to get three--ignore
options. You could have also used--ignore="*."{jpg,png,svg}
– muru
Feb 14 '17 at 0:27
3
3
This is simply using bash's brace expansion to get three
--ignore
options. You could have also used --ignore="*."{jpg,png,svg}
– muru
Feb 14 '17 at 0:27
This is simply using bash's brace expansion to get three
--ignore
options. You could have also used --ignore="*."{jpg,png,svg}
– muru
Feb 14 '17 at 0:27
add a comment |
up vote
1
down vote
I think that this produces the output you're looking for:
ls -1 !(temp)
Apparently, you need shopt -s extglob
for that to work
(I have it enabled, so I guess some time in the distant past I found it useful and enabled it).
I guess you could also use grep to filter the output:
ls -1 | grep -v '^temp$'
Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.
add a comment |
up vote
1
down vote
I think that this produces the output you're looking for:
ls -1 !(temp)
Apparently, you need shopt -s extglob
for that to work
(I have it enabled, so I guess some time in the distant past I found it useful and enabled it).
I guess you could also use grep to filter the output:
ls -1 | grep -v '^temp$'
Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.
add a comment |
up vote
1
down vote
up vote
1
down vote
I think that this produces the output you're looking for:
ls -1 !(temp)
Apparently, you need shopt -s extglob
for that to work
(I have it enabled, so I guess some time in the distant past I found it useful and enabled it).
I guess you could also use grep to filter the output:
ls -1 | grep -v '^temp$'
Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.
I think that this produces the output you're looking for:
ls -1 !(temp)
Apparently, you need shopt -s extglob
for that to work
(I have it enabled, so I guess some time in the distant past I found it useful and enabled it).
I guess you could also use grep to filter the output:
ls -1 | grep -v '^temp$'
Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.
edited Oct 31 at 19:18
zx485
1,45231114
1,45231114
answered Oct 31 at 18:24
Max Waterman
111
111
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f512961%2fexclude-certain-files-in-ls%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown