Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
Hi.
I want to run two commands from terminal and write the results to a file.
I want the output file to contain a listing of the hostname and directory .xyz if it exists on that server.
I've created a server-list file which contains 100 servers.
so, I attempted to do this, but cannot get the hostname from each server to write to the file, but the ls -a command does appear in the file.
rather than listing the content of .xyz, you list the content of Tom's home directory
you don't look for a directory named .xyz. The dot has a special meaning for grep, namely "any character". So, you look for a file whose name has at least four characters and includes "xyz"
Something like this might be your solution:
Code:
ssh Tom@192.168.1.5 sh -c "ls -a .xyz 2>/dev/null || hostname"
This runs a shell on the remote system and asks that shell to
execute the ls
discard any error messages from the ls command
run hostname only if the ls command succeeds
If you want the hostname to be printed even if there is no .xyz directory, replace the double pipe by a semicolon.
Last edited by berndbausch; 11-06-2015 at 10:49 PM.
It's hard to manage all the backslashes and other quoting mechanisms when nesting command lines like this. MadeinGermany has got it right I think. A test or an ls are simpler solutions.
I appreciate the feedback and hints you've provided! I noticed the output lists the information on two separate lines. What if I want the output to list the computer name and directory name or if it exists on one line? What would the command be to achieve this? Thanks again!
I appreciate the feedback and hints you've provided! I noticed the output lists the information on two separate lines. What if I want the output to list the computer name and directory name or if it exists on one line? What would the command be to achieve this? Thanks again!
Without knowing the command you are entering, it's kind of difficult to answer your question.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.