How to list only the filename of files ending in `.tar.gz` within a directory
up vote
4
down vote
favorite
I am trying to list .tar.gz
only using the following command:
ls *.tar.gz -l
.. It shows me the following list:
-rw-rw-r-- 1 osm osm 949 Nov 27 16:17 file1.tar.gz
-rw-rw-r-- 1 osm osm 949 Nov 27 16:17 file2.tar.gz
However, I just need to list it this way:
file1.tar.gz
file2.tar.gz
and also not:
file1.tar.gz file2.tar.gz
How to do it?
ls
add a comment |
up vote
4
down vote
favorite
I am trying to list .tar.gz
only using the following command:
ls *.tar.gz -l
.. It shows me the following list:
-rw-rw-r-- 1 osm osm 949 Nov 27 16:17 file1.tar.gz
-rw-rw-r-- 1 osm osm 949 Nov 27 16:17 file2.tar.gz
However, I just need to list it this way:
file1.tar.gz
file2.tar.gz
and also not:
file1.tar.gz file2.tar.gz
How to do it?
ls
3
it is a good habit to use options before filenames. and you need to use -1 instead of -l
– AnonymousLurker
12 hours ago
7
ls -1 *.tar.gz
, but what do you want to use the list for? If you are doing something to those filenames, thenls
is not the right way to do it. See Why *not* parse `ls` (and what do to instead)?
– Kusalananda
12 hours ago
1
the manual you need isman ls
. Ensure that your display font has good contrast between 1 (one) and l (lower case letter ell).
– ctrl-alt-delor
10 hours ago
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I am trying to list .tar.gz
only using the following command:
ls *.tar.gz -l
.. It shows me the following list:
-rw-rw-r-- 1 osm osm 949 Nov 27 16:17 file1.tar.gz
-rw-rw-r-- 1 osm osm 949 Nov 27 16:17 file2.tar.gz
However, I just need to list it this way:
file1.tar.gz
file2.tar.gz
and also not:
file1.tar.gz file2.tar.gz
How to do it?
ls
I am trying to list .tar.gz
only using the following command:
ls *.tar.gz -l
.. It shows me the following list:
-rw-rw-r-- 1 osm osm 949 Nov 27 16:17 file1.tar.gz
-rw-rw-r-- 1 osm osm 949 Nov 27 16:17 file2.tar.gz
However, I just need to list it this way:
file1.tar.gz
file2.tar.gz
and also not:
file1.tar.gz file2.tar.gz
How to do it?
ls
ls
edited 8 hours ago
Stephen Kitt
158k24346423
158k24346423
asked 12 hours ago
McLan
1443
1443
3
it is a good habit to use options before filenames. and you need to use -1 instead of -l
– AnonymousLurker
12 hours ago
7
ls -1 *.tar.gz
, but what do you want to use the list for? If you are doing something to those filenames, thenls
is not the right way to do it. See Why *not* parse `ls` (and what do to instead)?
– Kusalananda
12 hours ago
1
the manual you need isman ls
. Ensure that your display font has good contrast between 1 (one) and l (lower case letter ell).
– ctrl-alt-delor
10 hours ago
add a comment |
3
it is a good habit to use options before filenames. and you need to use -1 instead of -l
– AnonymousLurker
12 hours ago
7
ls -1 *.tar.gz
, but what do you want to use the list for? If you are doing something to those filenames, thenls
is not the right way to do it. See Why *not* parse `ls` (and what do to instead)?
– Kusalananda
12 hours ago
1
the manual you need isman ls
. Ensure that your display font has good contrast between 1 (one) and l (lower case letter ell).
– ctrl-alt-delor
10 hours ago
3
3
it is a good habit to use options before filenames. and you need to use -1 instead of -l
– AnonymousLurker
12 hours ago
it is a good habit to use options before filenames. and you need to use -1 instead of -l
– AnonymousLurker
12 hours ago
7
7
ls -1 *.tar.gz
, but what do you want to use the list for? If you are doing something to those filenames, then ls
is not the right way to do it. See Why *not* parse `ls` (and what do to instead)?– Kusalananda
12 hours ago
ls -1 *.tar.gz
, but what do you want to use the list for? If you are doing something to those filenames, then ls
is not the right way to do it. See Why *not* parse `ls` (and what do to instead)?– Kusalananda
12 hours ago
1
1
the manual you need is
man ls
. Ensure that your display font has good contrast between 1 (one) and l (lower case letter ell).– ctrl-alt-delor
10 hours ago
the manual you need is
man ls
. Ensure that your display font has good contrast between 1 (one) and l (lower case letter ell).– ctrl-alt-delor
10 hours ago
add a comment |
5 Answers
5
active
oldest
votes
up vote
21
down vote
The -1
option (the digit “one”, not lower-case “L”) will list one file per line with no other information:
ls -1 -- *.tar.gz
add a comment |
up vote
10
down vote
If you only need the filenames, you could use printf
:
printf '%sn' *.tar.gz
... the shell will expand the *.tar.gz
wildcard to the filenames, then printf
will print them, with each followed by a newline. This output would differ a bit from that of ls
in the case of filenames with newlines embedded in them:
setup
$ touch file{1,2}.tar.gz
$ touch file$'n'3.tar.gz
ls
$ ls -1 -- *.tar.gz
file1.tar.gz
file2.tar.gz
file?3.tar.gz
printf
$ printf '%sn' *.tar.gz
file1.tar.gz
file2.tar.gz
file
3.tar.gz
5
See alsols -b
,ls -Q
, ls--quoting-style=...
.
– Stephen Kitt
12 hours ago
add a comment |
up vote
0
down vote
Or with GNU find
:
find -name "*.tar.gz" -printf '%Pn'
In contrary to ls
with *
it will search for .tar.gz
files recursively:
$ find -name "*.tar.gz" -printf '%Pn'
file1.tar.gz
dir/file3.tar.gz
file2.tar.gz
add a comment |
up vote
0
down vote
A slightly more roundabout and loopy way:
for i in *.tar.gz; do
echo "$i"
done
EDIT: added quotes to handle weird filenames
2
touch -- '-e a.tar.gz'
for a reason to quote your variables and unix.stackexchange.com/q/65803/117549 for reasons to use printf instead of echo
– Jeff Schaller
8 hours ago
oof, I just spent 5 minutes trying to figure out how to remove the resulting-e a.tar.gz
file
– snetch
8 hours ago
sorry!rm -- '-e a.tar.gz'
should do the trick
– Jeff Schaller
8 hours ago
You're good, it was a lesson, and I did learn that--
works forrm
and a lot of others.
– snetch
8 hours ago
add a comment |
up vote
0
down vote
ls
behaves differently when its output is piped. For example:
ls # outputs filenames in columns
ls | cat # passes one filename per line to the cat command
So if you want see all your *.tar.gz
files, one per line, you can do this:
ls *.tar.gz | cat
But what if you don't want to pipe your output? That is, is there a way to force ls
to output the filenames one to a line without piping the output?
Yes, with the -1
switch. (That's a dash with the number 1.) So you can use these commands:
ls -1 # shows all (non-hidden) files, one per line
ls -1 *.tar.gz # shows only *.tar.gz files, one per line
New contributor
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
21
down vote
The -1
option (the digit “one”, not lower-case “L”) will list one file per line with no other information:
ls -1 -- *.tar.gz
add a comment |
up vote
21
down vote
The -1
option (the digit “one”, not lower-case “L”) will list one file per line with no other information:
ls -1 -- *.tar.gz
add a comment |
up vote
21
down vote
up vote
21
down vote
The -1
option (the digit “one”, not lower-case “L”) will list one file per line with no other information:
ls -1 -- *.tar.gz
The -1
option (the digit “one”, not lower-case “L”) will list one file per line with no other information:
ls -1 -- *.tar.gz
answered 12 hours ago
Stephen Kitt
158k24346423
158k24346423
add a comment |
add a comment |
up vote
10
down vote
If you only need the filenames, you could use printf
:
printf '%sn' *.tar.gz
... the shell will expand the *.tar.gz
wildcard to the filenames, then printf
will print them, with each followed by a newline. This output would differ a bit from that of ls
in the case of filenames with newlines embedded in them:
setup
$ touch file{1,2}.tar.gz
$ touch file$'n'3.tar.gz
ls
$ ls -1 -- *.tar.gz
file1.tar.gz
file2.tar.gz
file?3.tar.gz
printf
$ printf '%sn' *.tar.gz
file1.tar.gz
file2.tar.gz
file
3.tar.gz
5
See alsols -b
,ls -Q
, ls--quoting-style=...
.
– Stephen Kitt
12 hours ago
add a comment |
up vote
10
down vote
If you only need the filenames, you could use printf
:
printf '%sn' *.tar.gz
... the shell will expand the *.tar.gz
wildcard to the filenames, then printf
will print them, with each followed by a newline. This output would differ a bit from that of ls
in the case of filenames with newlines embedded in them:
setup
$ touch file{1,2}.tar.gz
$ touch file$'n'3.tar.gz
ls
$ ls -1 -- *.tar.gz
file1.tar.gz
file2.tar.gz
file?3.tar.gz
printf
$ printf '%sn' *.tar.gz
file1.tar.gz
file2.tar.gz
file
3.tar.gz
5
See alsols -b
,ls -Q
, ls--quoting-style=...
.
– Stephen Kitt
12 hours ago
add a comment |
up vote
10
down vote
up vote
10
down vote
If you only need the filenames, you could use printf
:
printf '%sn' *.tar.gz
... the shell will expand the *.tar.gz
wildcard to the filenames, then printf
will print them, with each followed by a newline. This output would differ a bit from that of ls
in the case of filenames with newlines embedded in them:
setup
$ touch file{1,2}.tar.gz
$ touch file$'n'3.tar.gz
ls
$ ls -1 -- *.tar.gz
file1.tar.gz
file2.tar.gz
file?3.tar.gz
printf
$ printf '%sn' *.tar.gz
file1.tar.gz
file2.tar.gz
file
3.tar.gz
If you only need the filenames, you could use printf
:
printf '%sn' *.tar.gz
... the shell will expand the *.tar.gz
wildcard to the filenames, then printf
will print them, with each followed by a newline. This output would differ a bit from that of ls
in the case of filenames with newlines embedded in them:
setup
$ touch file{1,2}.tar.gz
$ touch file$'n'3.tar.gz
ls
$ ls -1 -- *.tar.gz
file1.tar.gz
file2.tar.gz
file?3.tar.gz
printf
$ printf '%sn' *.tar.gz
file1.tar.gz
file2.tar.gz
file
3.tar.gz
answered 12 hours ago
Jeff Schaller
36.6k1052121
36.6k1052121
5
See alsols -b
,ls -Q
, ls--quoting-style=...
.
– Stephen Kitt
12 hours ago
add a comment |
5
See alsols -b
,ls -Q
, ls--quoting-style=...
.
– Stephen Kitt
12 hours ago
5
5
See also
ls -b
, ls -Q
, ls --quoting-style=...
.– Stephen Kitt
12 hours ago
See also
ls -b
, ls -Q
, ls --quoting-style=...
.– Stephen Kitt
12 hours ago
add a comment |
up vote
0
down vote
Or with GNU find
:
find -name "*.tar.gz" -printf '%Pn'
In contrary to ls
with *
it will search for .tar.gz
files recursively:
$ find -name "*.tar.gz" -printf '%Pn'
file1.tar.gz
dir/file3.tar.gz
file2.tar.gz
add a comment |
up vote
0
down vote
Or with GNU find
:
find -name "*.tar.gz" -printf '%Pn'
In contrary to ls
with *
it will search for .tar.gz
files recursively:
$ find -name "*.tar.gz" -printf '%Pn'
file1.tar.gz
dir/file3.tar.gz
file2.tar.gz
add a comment |
up vote
0
down vote
up vote
0
down vote
Or with GNU find
:
find -name "*.tar.gz" -printf '%Pn'
In contrary to ls
with *
it will search for .tar.gz
files recursively:
$ find -name "*.tar.gz" -printf '%Pn'
file1.tar.gz
dir/file3.tar.gz
file2.tar.gz
Or with GNU find
:
find -name "*.tar.gz" -printf '%Pn'
In contrary to ls
with *
it will search for .tar.gz
files recursively:
$ find -name "*.tar.gz" -printf '%Pn'
file1.tar.gz
dir/file3.tar.gz
file2.tar.gz
answered 8 hours ago
Arkadiusz Drabczyk
7,57521734
7,57521734
add a comment |
add a comment |
up vote
0
down vote
A slightly more roundabout and loopy way:
for i in *.tar.gz; do
echo "$i"
done
EDIT: added quotes to handle weird filenames
2
touch -- '-e a.tar.gz'
for a reason to quote your variables and unix.stackexchange.com/q/65803/117549 for reasons to use printf instead of echo
– Jeff Schaller
8 hours ago
oof, I just spent 5 minutes trying to figure out how to remove the resulting-e a.tar.gz
file
– snetch
8 hours ago
sorry!rm -- '-e a.tar.gz'
should do the trick
– Jeff Schaller
8 hours ago
You're good, it was a lesson, and I did learn that--
works forrm
and a lot of others.
– snetch
8 hours ago
add a comment |
up vote
0
down vote
A slightly more roundabout and loopy way:
for i in *.tar.gz; do
echo "$i"
done
EDIT: added quotes to handle weird filenames
2
touch -- '-e a.tar.gz'
for a reason to quote your variables and unix.stackexchange.com/q/65803/117549 for reasons to use printf instead of echo
– Jeff Schaller
8 hours ago
oof, I just spent 5 minutes trying to figure out how to remove the resulting-e a.tar.gz
file
– snetch
8 hours ago
sorry!rm -- '-e a.tar.gz'
should do the trick
– Jeff Schaller
8 hours ago
You're good, it was a lesson, and I did learn that--
works forrm
and a lot of others.
– snetch
8 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
A slightly more roundabout and loopy way:
for i in *.tar.gz; do
echo "$i"
done
EDIT: added quotes to handle weird filenames
A slightly more roundabout and loopy way:
for i in *.tar.gz; do
echo "$i"
done
EDIT: added quotes to handle weird filenames
edited 8 hours ago
answered 8 hours ago
snetch
12719
12719
2
touch -- '-e a.tar.gz'
for a reason to quote your variables and unix.stackexchange.com/q/65803/117549 for reasons to use printf instead of echo
– Jeff Schaller
8 hours ago
oof, I just spent 5 minutes trying to figure out how to remove the resulting-e a.tar.gz
file
– snetch
8 hours ago
sorry!rm -- '-e a.tar.gz'
should do the trick
– Jeff Schaller
8 hours ago
You're good, it was a lesson, and I did learn that--
works forrm
and a lot of others.
– snetch
8 hours ago
add a comment |
2
touch -- '-e a.tar.gz'
for a reason to quote your variables and unix.stackexchange.com/q/65803/117549 for reasons to use printf instead of echo
– Jeff Schaller
8 hours ago
oof, I just spent 5 minutes trying to figure out how to remove the resulting-e a.tar.gz
file
– snetch
8 hours ago
sorry!rm -- '-e a.tar.gz'
should do the trick
– Jeff Schaller
8 hours ago
You're good, it was a lesson, and I did learn that--
works forrm
and a lot of others.
– snetch
8 hours ago
2
2
touch -- '-e a.tar.gz'
for a reason to quote your variables and unix.stackexchange.com/q/65803/117549 for reasons to use printf instead of echo– Jeff Schaller
8 hours ago
touch -- '-e a.tar.gz'
for a reason to quote your variables and unix.stackexchange.com/q/65803/117549 for reasons to use printf instead of echo– Jeff Schaller
8 hours ago
oof, I just spent 5 minutes trying to figure out how to remove the resulting
-e a.tar.gz
file– snetch
8 hours ago
oof, I just spent 5 minutes trying to figure out how to remove the resulting
-e a.tar.gz
file– snetch
8 hours ago
sorry!
rm -- '-e a.tar.gz'
should do the trick– Jeff Schaller
8 hours ago
sorry!
rm -- '-e a.tar.gz'
should do the trick– Jeff Schaller
8 hours ago
You're good, it was a lesson, and I did learn that
--
works for rm
and a lot of others.– snetch
8 hours ago
You're good, it was a lesson, and I did learn that
--
works for rm
and a lot of others.– snetch
8 hours ago
add a comment |
up vote
0
down vote
ls
behaves differently when its output is piped. For example:
ls # outputs filenames in columns
ls | cat # passes one filename per line to the cat command
So if you want see all your *.tar.gz
files, one per line, you can do this:
ls *.tar.gz | cat
But what if you don't want to pipe your output? That is, is there a way to force ls
to output the filenames one to a line without piping the output?
Yes, with the -1
switch. (That's a dash with the number 1.) So you can use these commands:
ls -1 # shows all (non-hidden) files, one per line
ls -1 *.tar.gz # shows only *.tar.gz files, one per line
New contributor
add a comment |
up vote
0
down vote
ls
behaves differently when its output is piped. For example:
ls # outputs filenames in columns
ls | cat # passes one filename per line to the cat command
So if you want see all your *.tar.gz
files, one per line, you can do this:
ls *.tar.gz | cat
But what if you don't want to pipe your output? That is, is there a way to force ls
to output the filenames one to a line without piping the output?
Yes, with the -1
switch. (That's a dash with the number 1.) So you can use these commands:
ls -1 # shows all (non-hidden) files, one per line
ls -1 *.tar.gz # shows only *.tar.gz files, one per line
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
ls
behaves differently when its output is piped. For example:
ls # outputs filenames in columns
ls | cat # passes one filename per line to the cat command
So if you want see all your *.tar.gz
files, one per line, you can do this:
ls *.tar.gz | cat
But what if you don't want to pipe your output? That is, is there a way to force ls
to output the filenames one to a line without piping the output?
Yes, with the -1
switch. (That's a dash with the number 1.) So you can use these commands:
ls -1 # shows all (non-hidden) files, one per line
ls -1 *.tar.gz # shows only *.tar.gz files, one per line
New contributor
ls
behaves differently when its output is piped. For example:
ls # outputs filenames in columns
ls | cat # passes one filename per line to the cat command
So if you want see all your *.tar.gz
files, one per line, you can do this:
ls *.tar.gz | cat
But what if you don't want to pipe your output? That is, is there a way to force ls
to output the filenames one to a line without piping the output?
Yes, with the -1
switch. (That's a dash with the number 1.) So you can use these commands:
ls -1 # shows all (non-hidden) files, one per line
ls -1 *.tar.gz # shows only *.tar.gz files, one per line
New contributor
edited 6 hours ago
New contributor
answered 6 hours ago
J-L
1012
1012
New contributor
New contributor
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484481%2fhow-to-list-only-the-filename-of-files-ending-in-tar-gz-within-a-directory%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
3
it is a good habit to use options before filenames. and you need to use -1 instead of -l
– AnonymousLurker
12 hours ago
7
ls -1 *.tar.gz
, but what do you want to use the list for? If you are doing something to those filenames, thenls
is not the right way to do it. See Why *not* parse `ls` (and what do to instead)?– Kusalananda
12 hours ago
1
the manual you need is
man ls
. Ensure that your display font has good contrast between 1 (one) and l (lower case letter ell).– ctrl-alt-delor
10 hours ago