Using ls to find files that end in a character, ignoring extension
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am supposed to use ls to find files that end in a certain letter, but it does not matter if the file has an extension or not.
For example, I want it to do this
> ls
test test.txt test.ascii other.txt
> ls [something]
test test.txt test.ascii
So that I can find files that end with a 't', but it doesn't include the file where the extension ends in t
Edit:
I am supposed to assume that the only period characters in the filename will be for the extension and there will be no others
linux command-line ls
add a comment |Â
up vote
1
down vote
favorite
I am supposed to use ls to find files that end in a certain letter, but it does not matter if the file has an extension or not.
For example, I want it to do this
> ls
test test.txt test.ascii other.txt
> ls [something]
test test.txt test.ascii
So that I can find files that end with a 't', but it doesn't include the file where the extension ends in t
Edit:
I am supposed to assume that the only period characters in the filename will be for the extension and there will be no others
linux command-line ls
If you hadtest.foo.bar
, is.foo.bar
the extension or.bar
?
â muru
Sep 11 at 1:30
.bar
would be, I forgot to mention I am assuming that there is no other period characters in the file names other than the one for the extension, if there is one
â Anthony Rulli
Sep 11 at 1:46
Just in the current directory ?
â Jeff Schaller
Sep 11 at 2:09
2
What exactly are you supposed to use? Onlyls
and its options? Does the person who set this homework know that usually wildcards and pipes are handled by the shell, notls
(with exceptions like GNUls
's--hide
/--ignore
options)?
â muru
Sep 11 at 2:14
The only thing stated was 'use ls'. There is very little help at times which is frustrating and the common response for help is "try looking online"
â Anthony Rulli
Sep 11 at 2:26
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am supposed to use ls to find files that end in a certain letter, but it does not matter if the file has an extension or not.
For example, I want it to do this
> ls
test test.txt test.ascii other.txt
> ls [something]
test test.txt test.ascii
So that I can find files that end with a 't', but it doesn't include the file where the extension ends in t
Edit:
I am supposed to assume that the only period characters in the filename will be for the extension and there will be no others
linux command-line ls
I am supposed to use ls to find files that end in a certain letter, but it does not matter if the file has an extension or not.
For example, I want it to do this
> ls
test test.txt test.ascii other.txt
> ls [something]
test test.txt test.ascii
So that I can find files that end with a 't', but it doesn't include the file where the extension ends in t
Edit:
I am supposed to assume that the only period characters in the filename will be for the extension and there will be no others
linux command-line ls
linux command-line ls
edited Sep 11 at 7:44
Rui F Ribeiro
36.8k1273117
36.8k1273117
asked Sep 11 at 1:28
Anthony Rulli
83
83
If you hadtest.foo.bar
, is.foo.bar
the extension or.bar
?
â muru
Sep 11 at 1:30
.bar
would be, I forgot to mention I am assuming that there is no other period characters in the file names other than the one for the extension, if there is one
â Anthony Rulli
Sep 11 at 1:46
Just in the current directory ?
â Jeff Schaller
Sep 11 at 2:09
2
What exactly are you supposed to use? Onlyls
and its options? Does the person who set this homework know that usually wildcards and pipes are handled by the shell, notls
(with exceptions like GNUls
's--hide
/--ignore
options)?
â muru
Sep 11 at 2:14
The only thing stated was 'use ls'. There is very little help at times which is frustrating and the common response for help is "try looking online"
â Anthony Rulli
Sep 11 at 2:26
add a comment |Â
If you hadtest.foo.bar
, is.foo.bar
the extension or.bar
?
â muru
Sep 11 at 1:30
.bar
would be, I forgot to mention I am assuming that there is no other period characters in the file names other than the one for the extension, if there is one
â Anthony Rulli
Sep 11 at 1:46
Just in the current directory ?
â Jeff Schaller
Sep 11 at 2:09
2
What exactly are you supposed to use? Onlyls
and its options? Does the person who set this homework know that usually wildcards and pipes are handled by the shell, notls
(with exceptions like GNUls
's--hide
/--ignore
options)?
â muru
Sep 11 at 2:14
The only thing stated was 'use ls'. There is very little help at times which is frustrating and the common response for help is "try looking online"
â Anthony Rulli
Sep 11 at 2:26
If you had
test.foo.bar
, is .foo.bar
the extension or .bar
?â muru
Sep 11 at 1:30
If you had
test.foo.bar
, is .foo.bar
the extension or .bar
?â muru
Sep 11 at 1:30
.bar
would be, I forgot to mention I am assuming that there is no other period characters in the file names other than the one for the extension, if there is oneâ Anthony Rulli
Sep 11 at 1:46
.bar
would be, I forgot to mention I am assuming that there is no other period characters in the file names other than the one for the extension, if there is oneâ Anthony Rulli
Sep 11 at 1:46
Just in the current directory ?
â Jeff Schaller
Sep 11 at 2:09
Just in the current directory ?
â Jeff Schaller
Sep 11 at 2:09
2
2
What exactly are you supposed to use? Only
ls
and its options? Does the person who set this homework know that usually wildcards and pipes are handled by the shell, not ls
(with exceptions like GNU ls
's --hide
/--ignore
options)?â muru
Sep 11 at 2:14
What exactly are you supposed to use? Only
ls
and its options? Does the person who set this homework know that usually wildcards and pipes are handled by the shell, not ls
(with exceptions like GNU ls
's --hide
/--ignore
options)?â muru
Sep 11 at 2:14
The only thing stated was 'use ls'. There is very little help at times which is frustrating and the common response for help is "try looking online"
â Anthony Rulli
Sep 11 at 2:26
The only thing stated was 'use ls'. There is very little help at times which is frustrating and the common response for help is "try looking online"
â Anthony Rulli
Sep 11 at 2:26
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
something=
|grep -e ".*t.[^.]*" -e "^[^.]*t$"
add a comment |Â
up vote
0
down vote
If you are using the bash
shell, you can use the extglob
option to do a complex filter. For example:
Solution:
bash$ shopt -s extglob
bash$ ls
other.txt test test.ascii test.txt
bash$ ls *@(t.*|[^.]?[[:alnum:]]t)
test test.ascii test.txt
Explanation:
PATTERN1 (t.*)
matches any file ending with a "t" followed by a file extension:
bash$ ls *@(t.*)
test.ascii test.txt
PATTERN2 ([^.]?[[:alnum:]]t)
matches any file that does NOT have a dot, followed by zero or more alphanumeric chars, and ending in a "t". This matches files that do not have a file extension:
bash$ ls *@([^.]?[[:alnum:]]t)
test
The glob @(PATTERN1|PATTERN2)
matches either pattern exactly once. The combined pattern filters out only what we're looking for.
For more info on bash patterns, see: http://wiki.bash-hackers.org/syntax/pattern
This will not matchfoot.t
for example.
â Isaac
Sep 11 at 11:35
yes, it does matchfoot.t
, due to the fact that PATTERN1 matches. Verified.
â guzzijason
Sep 11 at 14:59
Sorry, I fumbled with the example, wrong example. You are assuming that the extension may be 1, 2, or 3 characters but the pattern will fail with anything longer even if the filename does not end in at
. Test with this filestouch never.abcdet,t
.
â Isaac
Sep 11 at 21:13
Also, the pattern will matchtt.none.never
, which may be argued that it does not end with t just before the extension. And, will match filenames with many dotsone.two.broken.print
. Maybe a pattern is not the right tool for this job.
â Isaac
Sep 11 at 21:25
Ageed. I would personally pipe togrep -E ...
. Bash patterns aren't nearly as powerful as a true regex. I was curious of the problem could be solved purely withls
and no pipe, and I guess the answer is... "almost" :)
â guzzijason
Sep 11 at 23:22
add a comment |Â
up vote
0
down vote
Assuming you need files with only one dot (extension) and
Assuming that you need to search on the present directory (./) you may use:
$ find . -maxdepth 1 -type f -regex './[^.]*t(.[^.]*)?'
./test.txt
./test.ascii
./test
This could be used but will include directories (and will fail if filenames have newlines):
$ printf '%sn' * | grep -e ".*t.[^.]*" -e "^[^.]*t$"
test
test.ascii
test.txt
tt
To allow filenames with newlines use (GNU grep):
$ printf '%s' * | grep -ze ".*t.[^.]*" -e "^[^.]*t$" | xargs -0
test test.ascii test.txt tt
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
something=
|grep -e ".*t.[^.]*" -e "^[^.]*t$"
add a comment |Â
up vote
1
down vote
accepted
something=
|grep -e ".*t.[^.]*" -e "^[^.]*t$"
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
something=
|grep -e ".*t.[^.]*" -e "^[^.]*t$"
something=
|grep -e ".*t.[^.]*" -e "^[^.]*t$"
answered Sep 11 at 2:13
Ipor Sircer
9,3481920
9,3481920
add a comment |Â
add a comment |Â
up vote
0
down vote
If you are using the bash
shell, you can use the extglob
option to do a complex filter. For example:
Solution:
bash$ shopt -s extglob
bash$ ls
other.txt test test.ascii test.txt
bash$ ls *@(t.*|[^.]?[[:alnum:]]t)
test test.ascii test.txt
Explanation:
PATTERN1 (t.*)
matches any file ending with a "t" followed by a file extension:
bash$ ls *@(t.*)
test.ascii test.txt
PATTERN2 ([^.]?[[:alnum:]]t)
matches any file that does NOT have a dot, followed by zero or more alphanumeric chars, and ending in a "t". This matches files that do not have a file extension:
bash$ ls *@([^.]?[[:alnum:]]t)
test
The glob @(PATTERN1|PATTERN2)
matches either pattern exactly once. The combined pattern filters out only what we're looking for.
For more info on bash patterns, see: http://wiki.bash-hackers.org/syntax/pattern
This will not matchfoot.t
for example.
â Isaac
Sep 11 at 11:35
yes, it does matchfoot.t
, due to the fact that PATTERN1 matches. Verified.
â guzzijason
Sep 11 at 14:59
Sorry, I fumbled with the example, wrong example. You are assuming that the extension may be 1, 2, or 3 characters but the pattern will fail with anything longer even if the filename does not end in at
. Test with this filestouch never.abcdet,t
.
â Isaac
Sep 11 at 21:13
Also, the pattern will matchtt.none.never
, which may be argued that it does not end with t just before the extension. And, will match filenames with many dotsone.two.broken.print
. Maybe a pattern is not the right tool for this job.
â Isaac
Sep 11 at 21:25
Ageed. I would personally pipe togrep -E ...
. Bash patterns aren't nearly as powerful as a true regex. I was curious of the problem could be solved purely withls
and no pipe, and I guess the answer is... "almost" :)
â guzzijason
Sep 11 at 23:22
add a comment |Â
up vote
0
down vote
If you are using the bash
shell, you can use the extglob
option to do a complex filter. For example:
Solution:
bash$ shopt -s extglob
bash$ ls
other.txt test test.ascii test.txt
bash$ ls *@(t.*|[^.]?[[:alnum:]]t)
test test.ascii test.txt
Explanation:
PATTERN1 (t.*)
matches any file ending with a "t" followed by a file extension:
bash$ ls *@(t.*)
test.ascii test.txt
PATTERN2 ([^.]?[[:alnum:]]t)
matches any file that does NOT have a dot, followed by zero or more alphanumeric chars, and ending in a "t". This matches files that do not have a file extension:
bash$ ls *@([^.]?[[:alnum:]]t)
test
The glob @(PATTERN1|PATTERN2)
matches either pattern exactly once. The combined pattern filters out only what we're looking for.
For more info on bash patterns, see: http://wiki.bash-hackers.org/syntax/pattern
This will not matchfoot.t
for example.
â Isaac
Sep 11 at 11:35
yes, it does matchfoot.t
, due to the fact that PATTERN1 matches. Verified.
â guzzijason
Sep 11 at 14:59
Sorry, I fumbled with the example, wrong example. You are assuming that the extension may be 1, 2, or 3 characters but the pattern will fail with anything longer even if the filename does not end in at
. Test with this filestouch never.abcdet,t
.
â Isaac
Sep 11 at 21:13
Also, the pattern will matchtt.none.never
, which may be argued that it does not end with t just before the extension. And, will match filenames with many dotsone.two.broken.print
. Maybe a pattern is not the right tool for this job.
â Isaac
Sep 11 at 21:25
Ageed. I would personally pipe togrep -E ...
. Bash patterns aren't nearly as powerful as a true regex. I was curious of the problem could be solved purely withls
and no pipe, and I guess the answer is... "almost" :)
â guzzijason
Sep 11 at 23:22
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If you are using the bash
shell, you can use the extglob
option to do a complex filter. For example:
Solution:
bash$ shopt -s extglob
bash$ ls
other.txt test test.ascii test.txt
bash$ ls *@(t.*|[^.]?[[:alnum:]]t)
test test.ascii test.txt
Explanation:
PATTERN1 (t.*)
matches any file ending with a "t" followed by a file extension:
bash$ ls *@(t.*)
test.ascii test.txt
PATTERN2 ([^.]?[[:alnum:]]t)
matches any file that does NOT have a dot, followed by zero or more alphanumeric chars, and ending in a "t". This matches files that do not have a file extension:
bash$ ls *@([^.]?[[:alnum:]]t)
test
The glob @(PATTERN1|PATTERN2)
matches either pattern exactly once. The combined pattern filters out only what we're looking for.
For more info on bash patterns, see: http://wiki.bash-hackers.org/syntax/pattern
If you are using the bash
shell, you can use the extglob
option to do a complex filter. For example:
Solution:
bash$ shopt -s extglob
bash$ ls
other.txt test test.ascii test.txt
bash$ ls *@(t.*|[^.]?[[:alnum:]]t)
test test.ascii test.txt
Explanation:
PATTERN1 (t.*)
matches any file ending with a "t" followed by a file extension:
bash$ ls *@(t.*)
test.ascii test.txt
PATTERN2 ([^.]?[[:alnum:]]t)
matches any file that does NOT have a dot, followed by zero or more alphanumeric chars, and ending in a "t". This matches files that do not have a file extension:
bash$ ls *@([^.]?[[:alnum:]]t)
test
The glob @(PATTERN1|PATTERN2)
matches either pattern exactly once. The combined pattern filters out only what we're looking for.
For more info on bash patterns, see: http://wiki.bash-hackers.org/syntax/pattern
edited Sep 11 at 11:36
Isaac
7,28911035
7,28911035
answered Sep 11 at 4:14
guzzijason
1517
1517
This will not matchfoot.t
for example.
â Isaac
Sep 11 at 11:35
yes, it does matchfoot.t
, due to the fact that PATTERN1 matches. Verified.
â guzzijason
Sep 11 at 14:59
Sorry, I fumbled with the example, wrong example. You are assuming that the extension may be 1, 2, or 3 characters but the pattern will fail with anything longer even if the filename does not end in at
. Test with this filestouch never.abcdet,t
.
â Isaac
Sep 11 at 21:13
Also, the pattern will matchtt.none.never
, which may be argued that it does not end with t just before the extension. And, will match filenames with many dotsone.two.broken.print
. Maybe a pattern is not the right tool for this job.
â Isaac
Sep 11 at 21:25
Ageed. I would personally pipe togrep -E ...
. Bash patterns aren't nearly as powerful as a true regex. I was curious of the problem could be solved purely withls
and no pipe, and I guess the answer is... "almost" :)
â guzzijason
Sep 11 at 23:22
add a comment |Â
This will not matchfoot.t
for example.
â Isaac
Sep 11 at 11:35
yes, it does matchfoot.t
, due to the fact that PATTERN1 matches. Verified.
â guzzijason
Sep 11 at 14:59
Sorry, I fumbled with the example, wrong example. You are assuming that the extension may be 1, 2, or 3 characters but the pattern will fail with anything longer even if the filename does not end in at
. Test with this filestouch never.abcdet,t
.
â Isaac
Sep 11 at 21:13
Also, the pattern will matchtt.none.never
, which may be argued that it does not end with t just before the extension. And, will match filenames with many dotsone.two.broken.print
. Maybe a pattern is not the right tool for this job.
â Isaac
Sep 11 at 21:25
Ageed. I would personally pipe togrep -E ...
. Bash patterns aren't nearly as powerful as a true regex. I was curious of the problem could be solved purely withls
and no pipe, and I guess the answer is... "almost" :)
â guzzijason
Sep 11 at 23:22
This will not match
foot.t
for example.â Isaac
Sep 11 at 11:35
This will not match
foot.t
for example.â Isaac
Sep 11 at 11:35
yes, it does match
foot.t
, due to the fact that PATTERN1 matches. Verified.â guzzijason
Sep 11 at 14:59
yes, it does match
foot.t
, due to the fact that PATTERN1 matches. Verified.â guzzijason
Sep 11 at 14:59
Sorry, I fumbled with the example, wrong example. You are assuming that the extension may be 1, 2, or 3 characters but the pattern will fail with anything longer even if the filename does not end in a
t
. Test with this files touch never.abcdet,t
.â Isaac
Sep 11 at 21:13
Sorry, I fumbled with the example, wrong example. You are assuming that the extension may be 1, 2, or 3 characters but the pattern will fail with anything longer even if the filename does not end in a
t
. Test with this files touch never.abcdet,t
.â Isaac
Sep 11 at 21:13
Also, the pattern will match
tt.none.never
, which may be argued that it does not end with t just before the extension. And, will match filenames with many dots one.two.broken.print
. Maybe a pattern is not the right tool for this job.â Isaac
Sep 11 at 21:25
Also, the pattern will match
tt.none.never
, which may be argued that it does not end with t just before the extension. And, will match filenames with many dots one.two.broken.print
. Maybe a pattern is not the right tool for this job.â Isaac
Sep 11 at 21:25
Ageed. I would personally pipe to
grep -E ...
. Bash patterns aren't nearly as powerful as a true regex. I was curious of the problem could be solved purely with ls
and no pipe, and I guess the answer is... "almost" :)â guzzijason
Sep 11 at 23:22
Ageed. I would personally pipe to
grep -E ...
. Bash patterns aren't nearly as powerful as a true regex. I was curious of the problem could be solved purely with ls
and no pipe, and I guess the answer is... "almost" :)â guzzijason
Sep 11 at 23:22
add a comment |Â
up vote
0
down vote
Assuming you need files with only one dot (extension) and
Assuming that you need to search on the present directory (./) you may use:
$ find . -maxdepth 1 -type f -regex './[^.]*t(.[^.]*)?'
./test.txt
./test.ascii
./test
This could be used but will include directories (and will fail if filenames have newlines):
$ printf '%sn' * | grep -e ".*t.[^.]*" -e "^[^.]*t$"
test
test.ascii
test.txt
tt
To allow filenames with newlines use (GNU grep):
$ printf '%s' * | grep -ze ".*t.[^.]*" -e "^[^.]*t$" | xargs -0
test test.ascii test.txt tt
add a comment |Â
up vote
0
down vote
Assuming you need files with only one dot (extension) and
Assuming that you need to search on the present directory (./) you may use:
$ find . -maxdepth 1 -type f -regex './[^.]*t(.[^.]*)?'
./test.txt
./test.ascii
./test
This could be used but will include directories (and will fail if filenames have newlines):
$ printf '%sn' * | grep -e ".*t.[^.]*" -e "^[^.]*t$"
test
test.ascii
test.txt
tt
To allow filenames with newlines use (GNU grep):
$ printf '%s' * | grep -ze ".*t.[^.]*" -e "^[^.]*t$" | xargs -0
test test.ascii test.txt tt
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Assuming you need files with only one dot (extension) and
Assuming that you need to search on the present directory (./) you may use:
$ find . -maxdepth 1 -type f -regex './[^.]*t(.[^.]*)?'
./test.txt
./test.ascii
./test
This could be used but will include directories (and will fail if filenames have newlines):
$ printf '%sn' * | grep -e ".*t.[^.]*" -e "^[^.]*t$"
test
test.ascii
test.txt
tt
To allow filenames with newlines use (GNU grep):
$ printf '%s' * | grep -ze ".*t.[^.]*" -e "^[^.]*t$" | xargs -0
test test.ascii test.txt tt
Assuming you need files with only one dot (extension) and
Assuming that you need to search on the present directory (./) you may use:
$ find . -maxdepth 1 -type f -regex './[^.]*t(.[^.]*)?'
./test.txt
./test.ascii
./test
This could be used but will include directories (and will fail if filenames have newlines):
$ printf '%sn' * | grep -e ".*t.[^.]*" -e "^[^.]*t$"
test
test.ascii
test.txt
tt
To allow filenames with newlines use (GNU grep):
$ printf '%s' * | grep -ze ".*t.[^.]*" -e "^[^.]*t$" | xargs -0
test test.ascii test.txt tt
answered Sep 11 at 11:48
Isaac
7,28911035
7,28911035
add a comment |Â
add a comment |Â
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%2f468147%2fusing-ls-to-find-files-that-end-in-a-character-ignoring-extension%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
If you had
test.foo.bar
, is.foo.bar
the extension or.bar
?â muru
Sep 11 at 1:30
.bar
would be, I forgot to mention I am assuming that there is no other period characters in the file names other than the one for the extension, if there is oneâ Anthony Rulli
Sep 11 at 1:46
Just in the current directory ?
â Jeff Schaller
Sep 11 at 2:09
2
What exactly are you supposed to use? Only
ls
and its options? Does the person who set this homework know that usually wildcards and pipes are handled by the shell, notls
(with exceptions like GNUls
's--hide
/--ignore
options)?â muru
Sep 11 at 2:14
The only thing stated was 'use ls'. There is very little help at times which is frustrating and the common response for help is "try looking online"
â Anthony Rulli
Sep 11 at 2:26