You are not logged in.

#1 2022-03-30 15:06:13

fekalzrout
Member
Registered: 2022-03-30
Posts: 5

list of files everyone on new line how to execute bash command on them

Hi, when i get output from terminal which looks like this:

[fekal@f screenshots1]$ ls *.jpg | sxiv -tio
2022-01-10-170422_3200x1080_scrot.jpg
2022-01-10-192421_3200x1080_scrot.jpg
2022-01-10-195101_3200x1080_scrot.jpg
2022-01-11-074643_3200x1080_scrot.jpg
2022-01-11-080643_3200x1080_scrot.jpg
2022-01-11-082323_3200x1080_scrot.jpg

how can I use simple command on these files like `cp (this list) ./new-folder`?
I know you can do echo and put them to some list but I would love to know if I can use command on block like this without making list.

Offline

#2 2022-03-30 15:16:44

lambdarch
Member
Registered: 2021-01-10
Posts: 50

Re: list of files everyone on new line how to execute bash command on them

See the xargs command, using the -t option for cp.

Offline

#3 2022-03-30 15:23:17

fekalzrout
Member
Registered: 2022-03-30
Posts: 5

Re: list of files everyone on new line how to execute bash command on them

thank you lambdarch I will look in it.

Offline

#4 2022-03-30 15:45:19

fekalzrout
Member
Registered: 2022-03-30
Posts: 5

Re: list of files everyone on new line how to execute bash command on them

this command:

`ls *.jpg | sxiv -tio | xargs -n1 -I '{}' mv '{}' pokus/`

seems to do what I need.

Offline

#5 2022-03-30 16:01:44

lambdarch
Member
Registered: 2021-01-10
Posts: 50

Re: list of files everyone on new line how to execute bash command on them

-n1 is implied by -I'{}' if I'm not mistaken, and it would be cleaner to use printf '%s\n' instead of ls to better preserve the filenames, although given their form above this is not necessary.

Last edited by lambdarch (2022-03-30 16:02:53)

Offline

#6 2022-03-30 16:12:52

seth
Member
Registered: 2012-09-03
Posts: 33,452

Re: list of files everyone on new line how to execute bash command on them

Online

#7 2022-03-30 16:21:03

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 27,538
Website

Re: list of files everyone on new line how to execute bash command on them

Why are you piping names from ls to sxiv in the first place?  It can be given multiple files on the command line.  This way you'd avoid all the pitfalls of using ls in this way (there are many).  Further, mv can also accept multiple files.  So the following should do what it seems you are trying to do:

mv $(sxiv -tio *.jpg) pokus/

If you don't like that approach, at least get rid of the useless ls:

sxiv -tio *.jpg | xargs ...

edit: actually the second version above is probably safer depending on exactly how sxiv outputs filenames (I have no experience with it).

Last edited by Trilby (2022-03-30 16:22:46)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2022-03-30 16:42:37

fekalzrout
Member
Registered: 2022-03-30
Posts: 5

Re: list of files everyone on new line how to execute bash command on them

ok this works best: `mkdir pokus/ | mv $(sxiv -tio *.jpg) pokus/` 

but this also works: `mkdir copied/ | printf '%s\n' *.jpg| sxiv -tio | xargs -I {} cp {} copied/` and i got rid of warning.

`mkdir cop/ | sxiv -tio *.jpg | xargs -I {} cp {} cop/` this works too

this pass through shellcheck with no warning: `mkdir copy/ | sxiv -tio ./*.jpg | xargs -I {} cp {} copy/` and it works

thanks a bunch to you all that is all i need.

Ps Luke Smith made a tutorial where he uses ls *.jpg| sxiv -tio

bye

Last edited by fekalzrout (2022-03-30 17:09:43)

Offline

#9 2022-03-30 17:12:14

lambdarch
Member
Registered: 2021-01-10
Posts: 50

Re: list of files everyone on new line how to execute bash command on them

mkdir dir | command: Be careful, there is nothing to pipe here, and you could start using the directory before it has been created.
What you want to do is probably mkdir dir && command.

Offline

#10 2022-03-30 17:13:57

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 27,538
Website

Re: list of files everyone on new line how to execute bash command on them

fekalzrout wrote:

Ps Luke Smith made a tutorial where he uses ls *.jpg| sxiv -tio

He's made a lot of other problematic stuff too.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#11 2022-04-03 06:37:32

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 163

Re: list of files everyone on new line how to execute bash command on them

*.jpg in arguments may fail on a big list of files. Args + environment size must not exceed ARG_MAX (128k).

find ... -print0 | xargs -r0 ...

is safer.

Offline

Board footer

Powered by FluxBB