LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 03-04-2017, 04:59 AM   #1
sergeyrar
LQ Newbie
 
Registered: Apr 2016
Posts: 2

Rep: Reputation: Disabled
Question How to open with an editor all C files in a current directory using a single Linux command


Hi,

I've tried this command :

ls | grep c$ | gedit
list all files and send only those ending with c to gedit - seems legit, no ?


but only this one seems to work -

gedit *.c &

Can anyone please explain why is that ?
 
Old 03-04-2017, 06:46 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
gedit does not read a list of files from a pipe.

Also,

Code:
ls | grep c$
will return any file or directory that ends with "c" be it a C source file, or a file ending in 'c', such as "Public", "src", etc. Clean up the grep term with something like '.c$', or better yet stick to the wildcard.
 
Old 03-04-2017, 08:56 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by sergeyrar View Post
Hi,

I've tried this command :

ls | grep c$ | gedit
list all files and send only those ending with c to gedit - seems legit, no ?


but only this one seems to work -
this one here
Quote:
Originally Posted by sergeyrar View Post
gedit *.c &

Can anyone please explain why is that ?
That'd be the one I'd tell you to use. it is the most logical one. It grabs everything according to the arguments specified.
 
Old 03-04-2017, 11:34 AM   #4
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
Quote:
greybot: DO NOT USE ls' output for anything. ls is a tool for interactively looking at directory metadata. Any attempts at parsing ls' output with code are broken. Globs are much more simple AND correct: ''for file in *.txt''. Read http://mywiki.wooledge.org/ParsingLs
This one is slightly related to, may offer further insight:
http://mywiki.wooledge.org/BashPitfa...8ls_.2A.mp3.29
Well: and perhaps this:
http://mywiki.wooledge.org/glob

Last edited by nodir; 03-06-2017 at 05:21 AM.
 
Old 03-06-2017, 03:35 AM   #5
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Mostly because ls returns a \n separated list when piped and *.c returns a space separated list. Plus order of precedence issues.

$ gedit $(ls * | grep -i \\.c$ | while read ITEM; do echo -n $ITEM" "; done)
 
Old 03-06-2017, 04:07 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by Shadow_7 View Post
Mostly because ls returns a \n separated list when piped and *.c returns a space separated list. Plus order of precedence issues.

$ gedit $(ls * | grep -i \\.c$ | while read ITEM; do echo -n $ITEM" "; done)
this is not really true.
Code:
gedit $(ls *.c)
# will do the same, and actually
gedit *.c 
#is enough
#but even
gedit $(ls -1 *.c)
#should work
ls * and the whole pipe-chain is completely useless and overkill.
The correct answer was (see post #1): gedit cannot read from pipe
and also do not use the output of ls for anything at all (but human reading - this was mentioned too).
 
Old 03-06-2017, 06:11 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by sergeyrar View Post
Hi,

I've tried this command :

ls | grep c$ | gedit
list all files and send only those ending with c to gedit - seems legit, no ?


but only this one seems to work -

gedit *.c &

Can anyone please explain why is that ?
Your first one doesn't work because you're passing the names to gedit incorrectly. As goumba said, gedit does not read filenames from stdin. You need to use xargs:
Code:
ls | grep c$ | xargs gedit
Still a pretty hacky solution though, the second one:
Code:
gedit *.c
is much better
 
Old 03-06-2017, 06:33 PM   #8
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
For the current directory only, the below is correct.
Code:
gedit *.c
However, for a recursive search starting from the current directory, this could work
Code:
gedit $(find . -type f -iname \*.c -printf '%p ')
 
  


Reply

Tags
files, pipes, regex



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] How-to: Modify many files in a directory with a single command tramni1980 Slackware 7 04-24-2012 12:58 AM
ls command and displaying the number of files in your current directory? revsarah Linux - General 5 10-15-2010 06:40 PM
[SOLVED] Script to open all subdirectories of a single directory and then run a command (help) Euler2 Linux - Newbie 7 03-24-2010 10:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:02 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
Open Source Consulting | Domain Registration