LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Ls-command question (https://www.linuxquestions.org/questions/linux-newbie-8/ls-command-question-4175531469/)

validator456 01-19-2015 12:14 AM

Ls-command question
 
Example: ls -m on the map fujifilm110 gives me:
DSCF1457.JPG, DSCF1458.JPG, DSCF1459.JPG, DSCF1460.JPG, DSCF1461.JPG,
DSCF1462.JPG, DSCF1463.JPG, DSCF1464.JPG, DSCF1465.JPG, DSCF1466.JPG,
DSCF1467.JPG, DSCF1468.JPG, DSCF1469.JPG, DSCF1470.JPG, DSCF1472.JPG,
DSCF1473.JPG, DSCF1474_a.JPG, DSCF1474.JPG, DSCF1475.JPG, DSCF1476.JPG,
DSCF1477_a.JPG, DSCF1477.JPG, DSCF1478.JPG, DSCF1479.JPG, DSCF1489.JPG,
DSCF1490.JPG, DSCF1491.JPG, DSCF1492.JPG, DSCF1494.JPG, DSCF1495.JPG,
DSCF1496.JPG, DSCF1497_a.JPG, DSCF1497.JPG, DSCF1501.JPG, DSCF1502.JPG,
DSCF1503.JPG, DSCF1504.JPG, DSCF1505.JPG

Question: Can I get an output of ls equal to this one but without the commas?

This comes in handy when I want to apply one command to many files at once (but not an entire directory). It is a recurring irritation to me.

astrogeek 01-19-2015 12:25 AM

Pass that output through sed...

Code:

ls -m fujifilm110 | sed 's/,//g'
... but of course your filenames must not contain any commas - they shouldn't, but you never know...

I have taken a guess at your directory name too, adjust to your uses.

linux_walt 01-19-2015 12:26 AM

Hello, I'm new to linux and didn't know about the -m option. However, without the '-m' you will not get the commas. Is that ok, or maybe I am misunderstanding.

validator456 01-19-2015 12:37 AM

Okay, but that produces too much space between the files. Let's say I want to apply the command sudo chmod 700 to all the files, the files must become one single line with exactly one space between the files.

That can be done in a text-browser of course, but is there a way on the commandline?

descendant_command 01-19-2015 12:40 AM

Probably better to use find with the -exec option, rather than piping output from ls.

linux_walt 01-19-2015 12:46 AM

Man, it's so late and I was bored, waiting for something to compile. Meanwhile I made a test directry, added a couple of files and experimented with commands like:

ls | mv /* /.* ..whatever, without realising I was in the root directory! Almost made a very big disaster.

astrogeek 01-19-2015 12:56 AM

Quote:

Originally Posted by validator456 (Post 5303245)
Okay, but that produces too much space between the files. Let's say I want to apply the command sudo chmod 700 to all the files, the files must become one single line with exactly one space between the files.

That can be done in a text-browser of course, but is there a way on the commandline?

As descendant_command says, find is probably your best option for that use case.

man find, and look at the exec option. You might also want to look at xargs for some use cases as well.

validator456 01-19-2015 01:03 AM

Okay, thank you for your answers. I will look into it.

linux_walt 01-19-2015 01:31 AM

This looks promising.

Folder contents:
Code:

~/test$ ls
file1.jpg  file2.jpg  otherfile1

Listing with only one space between file names:
Code:

~/test$ echo $(ls | grep .jpg$)
file1.jpg file2.jpg

Good night

dt64 01-19-2015 03:22 AM

Quote:

Originally Posted by validator456 (Post 5303245)
Okay, but that produces too much space between the files. Let's say I want to apply the command sudo chmod 700 to all the files, the files must become one single line with exactly one space between the files.

That can be done in a text-browser of course, but is there a way on the commandline?

Maybe there is a reason I don't see ATM, but why don't you go with the classic for loop to handle one file after the other??

e.g. in a script
Code:

#!/bin/bash
  for i in *; do
    sudo chmod 700 $i
  done

e.g. in a single line
Code:

for i in *; do sudo chmod 700 $i; done

chrism01 01-19-2015 03:25 AM

I just created a bunch of files, some with '.JPG' and just did
Code:

chmod 600 *.JPG
worked a treat, no need for ls or anything else ....
I'd also say that even if DO you need to pipe the list into another cmd, *nix doesn't care about the num of spaces between the filenames (filenames containing spaces are a special case)

validator456 01-19-2015 07:49 AM

My question was about listing all the files in a specific order (one space in between).

Not how to apply the chmod-command to a large amount of files.

dt64 01-19-2015 07:58 AM

wenn, the example you gace was to use this file list with a command like chmod, hence our minds started working this direction. It should be easy enough to substitute the actual commands.

If you just want to put them out on screen you could start with my 1line from above in a way like
Code:

for i in *; do echo -n $i; echo -n " "; done; echo
this reads out all the files specified and prints them on screen, add another space in between and does a "carriags return" or "newline" and the end.
if you need this output in a file, feel free to redirect output to wherever needed.

pan64 01-19-2015 08:02 AM

*.JPG (or *.jpg) will list those files. and you can do a chmod or whatever command using this syntax. but why do you need a specific order?

validator456 01-19-2015 08:43 AM

My mistake, pan64. What I meant is one space in between. E.g.: how to move many files of a directory to another directory.

So: sudo mv file1 file2 file3 file4 file5 file6 ~/images/wallpapers/shared.

What you can do is open nano or vim or even geany. Then to take the ouput of ls -m and remove the "," and make it one line. Then to copy/paste it after the command. Done. That is how I have done it all this time.

My question was: can you use the ls-command in such a way that you don't have to use an editor. Using the grep-command will do the trick.


All times are GMT -5. The time now is 05:39 AM.