LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-19-2015, 12:14 AM   #1
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 159

Rep: Reputation: Disabled
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.
 
Old 01-19-2015, 12:25 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,341
Blog Entries: 1

Rep: Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333
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.

Last edited by astrogeek; 01-19-2015 at 12:28 AM.
 
Old 01-19-2015, 12:26 AM   #3
linux_walt
Member
 
Registered: Dec 2014
Location: Houston, TX
Distribution: Debian wheezy
Posts: 127

Rep: Reputation: 29
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.
 
Old 01-19-2015, 12:37 AM   #4
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 159

Original Poster
Rep: Reputation: Disabled
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?
 
Old 01-19-2015, 12:40 AM   #5
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,479

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
Probably better to use find with the -exec option, rather than piping output from ls.
 
Old 01-19-2015, 12:46 AM   #6
linux_walt
Member
 
Registered: Dec 2014
Location: Houston, TX
Distribution: Debian wheezy
Posts: 127

Rep: Reputation: 29
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.
 
Old 01-19-2015, 12:56 AM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,341
Blog Entries: 1

Rep: Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333
Quote:
Originally Posted by validator456 View Post
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.
 
Old 01-19-2015, 01:03 AM   #8
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 159

Original Poster
Rep: Reputation: Disabled
Okay, thank you for your answers. I will look into it.
 
Old 01-19-2015, 01:31 AM   #9
linux_walt
Member
 
Registered: Dec 2014
Location: Houston, TX
Distribution: Debian wheezy
Posts: 127

Rep: Reputation: 29
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
 
1 members found this post helpful.
Old 01-19-2015, 03:22 AM   #10
dt64
Member
 
Registered: Sep 2012
Distribution: RHEL5/6, CentOS5/6
Posts: 216

Rep: Reputation: 38
Quote:
Originally Posted by validator456 View Post
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
 
Old 01-19-2015, 03:25 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,371

Rep: Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381
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)
 
Old 01-19-2015, 07:49 AM   #12
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 159

Original Poster
Rep: Reputation: Disabled
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.
 
Old 01-19-2015, 07:58 AM   #13
dt64
Member
 
Registered: Sep 2012
Distribution: RHEL5/6, CentOS5/6
Posts: 216

Rep: Reputation: 38
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.
 
Old 01-19-2015, 08:02 AM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,364

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
*.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?
 
Old 01-19-2015, 08:43 AM   #15
validator456
Member
 
Registered: Apr 2013
Location: Rotterdam, The Netherlands
Distribution: Crunchbang Linux
Posts: 159

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply

Tags
ls


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] less command question sryzdn Linux - Newbie 8 02-15-2014 01:09 PM
DD command Question Anony999 Linux - Newbie 8 04-20-2012 09:47 AM
I have a question about the dd command! turtlemicro Linux - Newbie 1 11-05-2011 11:45 PM
Question about the dd command kaplan71 Linux - Software 5 04-09-2010 08:09 AM
Question about command dd miros84 Linux - Software 7 02-10-2010 04:09 PM


All times are GMT -5. The time now is 04:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration