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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-04-2017, 04:59 AM
|
#1
|
LQ Newbie
Registered: Apr 2016
Posts: 2
Rep: 
|
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 ?
|
|
|
03-04-2017, 06:46 AM
|
#2
|
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
|
gedit does not read a list of files from a pipe.
Also,
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.
|
|
|
03-04-2017, 08:56 AM
|
#3
|
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
|
Quote:
Originally Posted by sergeyrar
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
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.
|
|
|
03-06-2017, 03:35 AM
|
#5
|
Senior Member
Registered: Feb 2003
Distribution: debian
Posts: 4,137
|
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)
|
|
|
03-06-2017, 04:07 AM
|
#6
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,669
|
Quote:
Originally Posted by Shadow_7
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).
|
|
|
03-06-2017, 06:11 PM
|
#7
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Quote:
Originally Posted by sergeyrar
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:
is much better
|
|
|
03-06-2017, 06:33 PM
|
#8
|
Member
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634
|
For the current directory only, the below is correct.
However, for a recursive search starting from the current directory, this could work
Code:
gedit $(find . -type f -iname \*.c -printf '%p ')
|
|
|
All times are GMT -5. The time now is 03:58 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|