Is it possible to remove folder prefix from a `ls` command
Clash Royale CLAN TAG#URR8PPP
up vote
10
down vote
favorite
I am in a bash script and I want to get the list of all files (let say all jar files). I execute the command ls -1 lib/*.jar
and I get the output:
lib/mylib_1.jar
lib/mylib_2.jar
...
Is there any option to have the following output:
mylib_1.jar
mylib_2.jar
...
Making cd lib
before is not an option as I am in a loop and need to be in the parent folder for the actions I want to do inside the loop.
I tried to find information by typing man ls
but I did not find any solution.
A solution with another command would be good as long I can pipe it to my ls
command or self sufficient.
bash shell directory ls
add a comment |Â
up vote
10
down vote
favorite
I am in a bash script and I want to get the list of all files (let say all jar files). I execute the command ls -1 lib/*.jar
and I get the output:
lib/mylib_1.jar
lib/mylib_2.jar
...
Is there any option to have the following output:
mylib_1.jar
mylib_2.jar
...
Making cd lib
before is not an option as I am in a loop and need to be in the parent folder for the actions I want to do inside the loop.
I tried to find information by typing man ls
but I did not find any solution.
A solution with another command would be good as long I can pipe it to my ls
command or self sufficient.
bash shell directory ls
add a comment |Â
up vote
10
down vote
favorite
up vote
10
down vote
favorite
I am in a bash script and I want to get the list of all files (let say all jar files). I execute the command ls -1 lib/*.jar
and I get the output:
lib/mylib_1.jar
lib/mylib_2.jar
...
Is there any option to have the following output:
mylib_1.jar
mylib_2.jar
...
Making cd lib
before is not an option as I am in a loop and need to be in the parent folder for the actions I want to do inside the loop.
I tried to find information by typing man ls
but I did not find any solution.
A solution with another command would be good as long I can pipe it to my ls
command or self sufficient.
bash shell directory ls
I am in a bash script and I want to get the list of all files (let say all jar files). I execute the command ls -1 lib/*.jar
and I get the output:
lib/mylib_1.jar
lib/mylib_2.jar
...
Is there any option to have the following output:
mylib_1.jar
mylib_2.jar
...
Making cd lib
before is not an option as I am in a loop and need to be in the parent folder for the actions I want to do inside the loop.
I tried to find information by typing man ls
but I did not find any solution.
A solution with another command would be good as long I can pipe it to my ls
command or self sufficient.
bash shell directory ls
bash shell directory ls
edited 4 mins ago
asked Apr 15 '14 at 13:29
ÃÂüÃÂÃÂÿ
74821529
74821529
add a comment |Â
add a comment |Â
7 Answers
7
active
oldest
votes
up vote
17
down vote
accepted
Instead of parsing ls you should use find
instead. Then you can also execute basename
on each file to strip the leading directories:
find lib/ -name '*.jar' -exec basename ;
Ok this way is perfect for me as well.
â ÃÂüÃÂÃÂÿ
Apr 15 '14 at 13:46
2
Also see the-maxdepth
option.
â Smith John
Apr 15 '14 at 20:11
add a comment |Â
up vote
12
down vote
With GNU find
there is no need to run basename
for every single file, this will be much faster (especially if there is a lot of files):
find lib -name '*.jar' -printf '%Pn'
add a comment |Â
up vote
10
down vote
How about (cd lib && echo *.jar)
, assuming that you don't have whitespace or special characters in the file names. Parent script never changes directory.
Spaces won't be an issue either.
â terdonâ¦
Apr 15 '14 at 14:39
This is a good idea and will probably be the fastest option. I would doprintf '%sn' *.jar
to get each file on a different line though.
â Graeme
Apr 15 '14 at 15:14
Or evenprintf '%s' *.jar
to eliminate whitespace issues (although this is not the Q).
â Graeme
Apr 15 '14 at 15:16
add a comment |Â
up vote
5
down vote
As Josh Jolly said in his answer, you should never parse ls
, use the approach in his answer instead. Still, here's an awk
solution to remove paths from file names, just don't use it with ls:
find . | awk -F'/' 'print $NF'
The -F'/'
sets the field separator to /
which means that the last field, $NF
, will be the file name.
add a comment |Â
up vote
1
down vote
find
is probably the way to go, but if you really, really do (you don't) want to strip off lib/
from ls -1
, you can use sed
:
$ ls -1 lib/*.jar | sed 's#^lib/##'
mylib_1.jar
mylib_2.jar
add a comment |Â
up vote
0
down vote
You can do this using xargs with a --max-args (or the shorter alias -n) parameter:
ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename
ls --quoting-style=c -1 lib/*.jar | xargs -n 1 basename
Note that we use --quoting-style=c
to make sure spaces in filenames do not screw up xargs.
I didn't notice that. Thank you! Found this gem on another stack exchange. You can introduce quotes to ls results with --quoting-style=c. So the resulting full command would be: ` ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename`
â yoni
May 5 '17 at 22:48
add a comment |Â
up vote
-1
down vote
An alternative way solve your query is to list all the files using ls -R
. Combine the output of ls command with grep to list only .jar files.
You can use following command to do the same for your query:
ls -R lib | grep jar| grep -v jar*
This would fail badly if there were a subdirectory that also contains jar files, or a file named for example "list-of-jars.txt".
â Jules
Apr 16 '14 at 3:14
grep -v jar* would filter out results with file named like your quoted example.
â joshi.mohit86
Apr 16 '14 at 6:18
true. it would also filter out all the intended files. Now I look at it again, I can see no circumstances where the command you suggest would actually produce any output at all...
â Jules
Apr 19 '14 at 3:39
No, it would display files likemylib_2.jar
and filter out files likelist-of-jars.txt
.
â joshi.mohit86
May 13 '14 at 10:07
No, it doesn't. I just tried it.find development -name '*.jar' | wc -l
->70
.ls -R development | grep jar| grep -v jar*
-> no output at all.
â Jules
May 14 '14 at 11:35
add a comment |Â
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
17
down vote
accepted
Instead of parsing ls you should use find
instead. Then you can also execute basename
on each file to strip the leading directories:
find lib/ -name '*.jar' -exec basename ;
Ok this way is perfect for me as well.
â ÃÂüÃÂÃÂÿ
Apr 15 '14 at 13:46
2
Also see the-maxdepth
option.
â Smith John
Apr 15 '14 at 20:11
add a comment |Â
up vote
17
down vote
accepted
Instead of parsing ls you should use find
instead. Then you can also execute basename
on each file to strip the leading directories:
find lib/ -name '*.jar' -exec basename ;
Ok this way is perfect for me as well.
â ÃÂüÃÂÃÂÿ
Apr 15 '14 at 13:46
2
Also see the-maxdepth
option.
â Smith John
Apr 15 '14 at 20:11
add a comment |Â
up vote
17
down vote
accepted
up vote
17
down vote
accepted
Instead of parsing ls you should use find
instead. Then you can also execute basename
on each file to strip the leading directories:
find lib/ -name '*.jar' -exec basename ;
Instead of parsing ls you should use find
instead. Then you can also execute basename
on each file to strip the leading directories:
find lib/ -name '*.jar' -exec basename ;
edited Apr 15 '14 at 13:37
terdonâ¦
124k29236414
124k29236414
answered Apr 15 '14 at 13:30
Josh Jolly
1,292812
1,292812
Ok this way is perfect for me as well.
â ÃÂüÃÂÃÂÿ
Apr 15 '14 at 13:46
2
Also see the-maxdepth
option.
â Smith John
Apr 15 '14 at 20:11
add a comment |Â
Ok this way is perfect for me as well.
â ÃÂüÃÂÃÂÿ
Apr 15 '14 at 13:46
2
Also see the-maxdepth
option.
â Smith John
Apr 15 '14 at 20:11
Ok this way is perfect for me as well.
â ÃÂüÃÂÃÂÿ
Apr 15 '14 at 13:46
Ok this way is perfect for me as well.
â ÃÂüÃÂÃÂÿ
Apr 15 '14 at 13:46
2
2
Also see the
-maxdepth
option.â Smith John
Apr 15 '14 at 20:11
Also see the
-maxdepth
option.â Smith John
Apr 15 '14 at 20:11
add a comment |Â
up vote
12
down vote
With GNU find
there is no need to run basename
for every single file, this will be much faster (especially if there is a lot of files):
find lib -name '*.jar' -printf '%Pn'
add a comment |Â
up vote
12
down vote
With GNU find
there is no need to run basename
for every single file, this will be much faster (especially if there is a lot of files):
find lib -name '*.jar' -printf '%Pn'
add a comment |Â
up vote
12
down vote
up vote
12
down vote
With GNU find
there is no need to run basename
for every single file, this will be much faster (especially if there is a lot of files):
find lib -name '*.jar' -printf '%Pn'
With GNU find
there is no need to run basename
for every single file, this will be much faster (especially if there is a lot of files):
find lib -name '*.jar' -printf '%Pn'
answered Apr 15 '14 at 13:49
Graeme
24.5k46294
24.5k46294
add a comment |Â
add a comment |Â
up vote
10
down vote
How about (cd lib && echo *.jar)
, assuming that you don't have whitespace or special characters in the file names. Parent script never changes directory.
Spaces won't be an issue either.
â terdonâ¦
Apr 15 '14 at 14:39
This is a good idea and will probably be the fastest option. I would doprintf '%sn' *.jar
to get each file on a different line though.
â Graeme
Apr 15 '14 at 15:14
Or evenprintf '%s' *.jar
to eliminate whitespace issues (although this is not the Q).
â Graeme
Apr 15 '14 at 15:16
add a comment |Â
up vote
10
down vote
How about (cd lib && echo *.jar)
, assuming that you don't have whitespace or special characters in the file names. Parent script never changes directory.
Spaces won't be an issue either.
â terdonâ¦
Apr 15 '14 at 14:39
This is a good idea and will probably be the fastest option. I would doprintf '%sn' *.jar
to get each file on a different line though.
â Graeme
Apr 15 '14 at 15:14
Or evenprintf '%s' *.jar
to eliminate whitespace issues (although this is not the Q).
â Graeme
Apr 15 '14 at 15:16
add a comment |Â
up vote
10
down vote
up vote
10
down vote
How about (cd lib && echo *.jar)
, assuming that you don't have whitespace or special characters in the file names. Parent script never changes directory.
How about (cd lib && echo *.jar)
, assuming that you don't have whitespace or special characters in the file names. Parent script never changes directory.
answered Apr 15 '14 at 14:36
Doug O'Neal
2,8021817
2,8021817
Spaces won't be an issue either.
â terdonâ¦
Apr 15 '14 at 14:39
This is a good idea and will probably be the fastest option. I would doprintf '%sn' *.jar
to get each file on a different line though.
â Graeme
Apr 15 '14 at 15:14
Or evenprintf '%s' *.jar
to eliminate whitespace issues (although this is not the Q).
â Graeme
Apr 15 '14 at 15:16
add a comment |Â
Spaces won't be an issue either.
â terdonâ¦
Apr 15 '14 at 14:39
This is a good idea and will probably be the fastest option. I would doprintf '%sn' *.jar
to get each file on a different line though.
â Graeme
Apr 15 '14 at 15:14
Or evenprintf '%s' *.jar
to eliminate whitespace issues (although this is not the Q).
â Graeme
Apr 15 '14 at 15:16
Spaces won't be an issue either.
â terdonâ¦
Apr 15 '14 at 14:39
Spaces won't be an issue either.
â terdonâ¦
Apr 15 '14 at 14:39
This is a good idea and will probably be the fastest option. I would do
printf '%sn' *.jar
to get each file on a different line though.â Graeme
Apr 15 '14 at 15:14
This is a good idea and will probably be the fastest option. I would do
printf '%sn' *.jar
to get each file on a different line though.â Graeme
Apr 15 '14 at 15:14
Or even
printf '%s' *.jar
to eliminate whitespace issues (although this is not the Q).â Graeme
Apr 15 '14 at 15:16
Or even
printf '%s' *.jar
to eliminate whitespace issues (although this is not the Q).â Graeme
Apr 15 '14 at 15:16
add a comment |Â
up vote
5
down vote
As Josh Jolly said in his answer, you should never parse ls
, use the approach in his answer instead. Still, here's an awk
solution to remove paths from file names, just don't use it with ls:
find . | awk -F'/' 'print $NF'
The -F'/'
sets the field separator to /
which means that the last field, $NF
, will be the file name.
add a comment |Â
up vote
5
down vote
As Josh Jolly said in his answer, you should never parse ls
, use the approach in his answer instead. Still, here's an awk
solution to remove paths from file names, just don't use it with ls:
find . | awk -F'/' 'print $NF'
The -F'/'
sets the field separator to /
which means that the last field, $NF
, will be the file name.
add a comment |Â
up vote
5
down vote
up vote
5
down vote
As Josh Jolly said in his answer, you should never parse ls
, use the approach in his answer instead. Still, here's an awk
solution to remove paths from file names, just don't use it with ls:
find . | awk -F'/' 'print $NF'
The -F'/'
sets the field separator to /
which means that the last field, $NF
, will be the file name.
As Josh Jolly said in his answer, you should never parse ls
, use the approach in his answer instead. Still, here's an awk
solution to remove paths from file names, just don't use it with ls:
find . | awk -F'/' 'print $NF'
The -F'/'
sets the field separator to /
which means that the last field, $NF
, will be the file name.
edited Apr 15 '14 at 13:39
answered Apr 15 '14 at 13:34
terdonâ¦
124k29236414
124k29236414
add a comment |Â
add a comment |Â
up vote
1
down vote
find
is probably the way to go, but if you really, really do (you don't) want to strip off lib/
from ls -1
, you can use sed
:
$ ls -1 lib/*.jar | sed 's#^lib/##'
mylib_1.jar
mylib_2.jar
add a comment |Â
up vote
1
down vote
find
is probably the way to go, but if you really, really do (you don't) want to strip off lib/
from ls -1
, you can use sed
:
$ ls -1 lib/*.jar | sed 's#^lib/##'
mylib_1.jar
mylib_2.jar
add a comment |Â
up vote
1
down vote
up vote
1
down vote
find
is probably the way to go, but if you really, really do (you don't) want to strip off lib/
from ls -1
, you can use sed
:
$ ls -1 lib/*.jar | sed 's#^lib/##'
mylib_1.jar
mylib_2.jar
find
is probably the way to go, but if you really, really do (you don't) want to strip off lib/
from ls -1
, you can use sed
:
$ ls -1 lib/*.jar | sed 's#^lib/##'
mylib_1.jar
mylib_2.jar
edited Apr 15 '14 at 15:35
answered Apr 15 '14 at 13:40
user61786
add a comment |Â
add a comment |Â
up vote
0
down vote
You can do this using xargs with a --max-args (or the shorter alias -n) parameter:
ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename
ls --quoting-style=c -1 lib/*.jar | xargs -n 1 basename
Note that we use --quoting-style=c
to make sure spaces in filenames do not screw up xargs.
I didn't notice that. Thank you! Found this gem on another stack exchange. You can introduce quotes to ls results with --quoting-style=c. So the resulting full command would be: ` ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename`
â yoni
May 5 '17 at 22:48
add a comment |Â
up vote
0
down vote
You can do this using xargs with a --max-args (or the shorter alias -n) parameter:
ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename
ls --quoting-style=c -1 lib/*.jar | xargs -n 1 basename
Note that we use --quoting-style=c
to make sure spaces in filenames do not screw up xargs.
I didn't notice that. Thank you! Found this gem on another stack exchange. You can introduce quotes to ls results with --quoting-style=c. So the resulting full command would be: ` ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename`
â yoni
May 5 '17 at 22:48
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can do this using xargs with a --max-args (or the shorter alias -n) parameter:
ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename
ls --quoting-style=c -1 lib/*.jar | xargs -n 1 basename
Note that we use --quoting-style=c
to make sure spaces in filenames do not screw up xargs.
You can do this using xargs with a --max-args (or the shorter alias -n) parameter:
ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename
ls --quoting-style=c -1 lib/*.jar | xargs -n 1 basename
Note that we use --quoting-style=c
to make sure spaces in filenames do not screw up xargs.
edited May 5 '17 at 22:49
answered May 5 '17 at 17:52
yoni
1013
1013
I didn't notice that. Thank you! Found this gem on another stack exchange. You can introduce quotes to ls results with --quoting-style=c. So the resulting full command would be: ` ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename`
â yoni
May 5 '17 at 22:48
add a comment |Â
I didn't notice that. Thank you! Found this gem on another stack exchange. You can introduce quotes to ls results with --quoting-style=c. So the resulting full command would be: ` ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename`
â yoni
May 5 '17 at 22:48
I didn't notice that. Thank you! Found this gem on another stack exchange. You can introduce quotes to ls results with --quoting-style=c. So the resulting full command would be: ` ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename`
â yoni
May 5 '17 at 22:48
I didn't notice that. Thank you! Found this gem on another stack exchange. You can introduce quotes to ls results with --quoting-style=c. So the resulting full command would be: ` ls --quoting-style=c -1 lib/*.jar | xargs --max-args 1 basename`
â yoni
May 5 '17 at 22:48
add a comment |Â
up vote
-1
down vote
An alternative way solve your query is to list all the files using ls -R
. Combine the output of ls command with grep to list only .jar files.
You can use following command to do the same for your query:
ls -R lib | grep jar| grep -v jar*
This would fail badly if there were a subdirectory that also contains jar files, or a file named for example "list-of-jars.txt".
â Jules
Apr 16 '14 at 3:14
grep -v jar* would filter out results with file named like your quoted example.
â joshi.mohit86
Apr 16 '14 at 6:18
true. it would also filter out all the intended files. Now I look at it again, I can see no circumstances where the command you suggest would actually produce any output at all...
â Jules
Apr 19 '14 at 3:39
No, it would display files likemylib_2.jar
and filter out files likelist-of-jars.txt
.
â joshi.mohit86
May 13 '14 at 10:07
No, it doesn't. I just tried it.find development -name '*.jar' | wc -l
->70
.ls -R development | grep jar| grep -v jar*
-> no output at all.
â Jules
May 14 '14 at 11:35
add a comment |Â
up vote
-1
down vote
An alternative way solve your query is to list all the files using ls -R
. Combine the output of ls command with grep to list only .jar files.
You can use following command to do the same for your query:
ls -R lib | grep jar| grep -v jar*
This would fail badly if there were a subdirectory that also contains jar files, or a file named for example "list-of-jars.txt".
â Jules
Apr 16 '14 at 3:14
grep -v jar* would filter out results with file named like your quoted example.
â joshi.mohit86
Apr 16 '14 at 6:18
true. it would also filter out all the intended files. Now I look at it again, I can see no circumstances where the command you suggest would actually produce any output at all...
â Jules
Apr 19 '14 at 3:39
No, it would display files likemylib_2.jar
and filter out files likelist-of-jars.txt
.
â joshi.mohit86
May 13 '14 at 10:07
No, it doesn't. I just tried it.find development -name '*.jar' | wc -l
->70
.ls -R development | grep jar| grep -v jar*
-> no output at all.
â Jules
May 14 '14 at 11:35
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
An alternative way solve your query is to list all the files using ls -R
. Combine the output of ls command with grep to list only .jar files.
You can use following command to do the same for your query:
ls -R lib | grep jar| grep -v jar*
An alternative way solve your query is to list all the files using ls -R
. Combine the output of ls command with grep to list only .jar files.
You can use following command to do the same for your query:
ls -R lib | grep jar| grep -v jar*
answered Apr 15 '14 at 14:04
joshi.mohit86
1
1
This would fail badly if there were a subdirectory that also contains jar files, or a file named for example "list-of-jars.txt".
â Jules
Apr 16 '14 at 3:14
grep -v jar* would filter out results with file named like your quoted example.
â joshi.mohit86
Apr 16 '14 at 6:18
true. it would also filter out all the intended files. Now I look at it again, I can see no circumstances where the command you suggest would actually produce any output at all...
â Jules
Apr 19 '14 at 3:39
No, it would display files likemylib_2.jar
and filter out files likelist-of-jars.txt
.
â joshi.mohit86
May 13 '14 at 10:07
No, it doesn't. I just tried it.find development -name '*.jar' | wc -l
->70
.ls -R development | grep jar| grep -v jar*
-> no output at all.
â Jules
May 14 '14 at 11:35
add a comment |Â
This would fail badly if there were a subdirectory that also contains jar files, or a file named for example "list-of-jars.txt".
â Jules
Apr 16 '14 at 3:14
grep -v jar* would filter out results with file named like your quoted example.
â joshi.mohit86
Apr 16 '14 at 6:18
true. it would also filter out all the intended files. Now I look at it again, I can see no circumstances where the command you suggest would actually produce any output at all...
â Jules
Apr 19 '14 at 3:39
No, it would display files likemylib_2.jar
and filter out files likelist-of-jars.txt
.
â joshi.mohit86
May 13 '14 at 10:07
No, it doesn't. I just tried it.find development -name '*.jar' | wc -l
->70
.ls -R development | grep jar| grep -v jar*
-> no output at all.
â Jules
May 14 '14 at 11:35
This would fail badly if there were a subdirectory that also contains jar files, or a file named for example "list-of-jars.txt".
â Jules
Apr 16 '14 at 3:14
This would fail badly if there were a subdirectory that also contains jar files, or a file named for example "list-of-jars.txt".
â Jules
Apr 16 '14 at 3:14
grep -v jar* would filter out results with file named like your quoted example.
â joshi.mohit86
Apr 16 '14 at 6:18
grep -v jar* would filter out results with file named like your quoted example.
â joshi.mohit86
Apr 16 '14 at 6:18
true. it would also filter out all the intended files. Now I look at it again, I can see no circumstances where the command you suggest would actually produce any output at all...
â Jules
Apr 19 '14 at 3:39
true. it would also filter out all the intended files. Now I look at it again, I can see no circumstances where the command you suggest would actually produce any output at all...
â Jules
Apr 19 '14 at 3:39
No, it would display files like
mylib_2.jar
and filter out files like list-of-jars.txt
.â joshi.mohit86
May 13 '14 at 10:07
No, it would display files like
mylib_2.jar
and filter out files like list-of-jars.txt
.â joshi.mohit86
May 13 '14 at 10:07
No, it doesn't. I just tried it.
find development -name '*.jar' | wc -l
-> 70
. ls -R development | grep jar| grep -v jar*
-> no output at all.â Jules
May 14 '14 at 11:35
No, it doesn't. I just tried it.
find development -name '*.jar' | wc -l
-> 70
. ls -R development | grep jar| grep -v jar*
-> no output at all.â Jules
May 14 '14 at 11:35
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%2f124887%2fis-it-possible-to-remove-folder-prefix-from-a-ls-command%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