How to open with an editor all C files in a current directory using a single Linux command
Linux - NewbieThis 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.
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.
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.
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
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).
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.