LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 05: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, 07:46 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Current: Debian and OpenSUSE. Past: Arch, RedHat (pre-RHEL). FreeBSD & OpenBSD novice, Hackintosh
Posts: 1,192
Blog Entries: 7

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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, 09:56 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,591

Rep: Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066
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, 12:34 PM   #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 06:21 AM.
 
Old 03-06-2017, 04:35 AM   #5
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,000
Blog Entries: 1

Rep: Reputation: 846Reputation: 846Reputation: 846Reputation: 846Reputation: 846Reputation: 846Reputation: 846
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, 05:07 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,576

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
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, 07:11 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137
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, 07:33 PM   #8
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 607

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
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


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] How-to: Modify many files in a directory with a single command tramni1980 Slackware 7 04-24-2012 01:58 AM
ls command and displaying the number of files in your current directory? revsarah Linux - General 5 10-15-2010 07: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 11:16 PM

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

All times are GMT -5. The time now is 06:59 AM.

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