How to run multiple command in a single line? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How to run multiple command in a single line?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-14-2012
boy18nj boy18nj is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 12 December 2012, 4:46 PM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
How to run multiple command in a single line?

Normally i would do this-

cd abc
ls -ltr

I wish to run above command in a single line, like this-

cd abc | ls -ltr

But above command doesn't works, it simply runs the second command, ignoring the 1st one.

Sponsored Links
    #2  
Old 04-14-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 506 Times in 485 Posts
Replace the pipe (|) with a semicolon like this:


Code:
cd dir-name; ls -lrt

It will change the directory and then list it's contents.
Sponsored Links
    #3  
Old 04-14-2012
boy18nj boy18nj is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 12 December 2012, 4:46 PM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
This is so cool. Thanks

Does this means I am not innovative or dumb...I know how to script...why this stuff did not come to my mind?

There is a one way to know, can you tell me how did you first come to know about this? I've read unix books, but none talk like this at command level.
    #4  
Old 04-14-2012
jlliagre jlliagre is online now Forum Advisor  
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Last Activity: 30 September 2014, 5:22 AM EDT
Location: Paris
Posts: 4,424
Thanks: 16
Thanked 468 Times in 423 Posts
Quote:
Originally Posted by agama View Post
Replace the pipe (|) with a semicolon like this:


Code:
cd dir-name; ls -lrt

It will change the directory and then list it's contents.
A better way is to list the directory contents only if it exists and is readable:

Code:
cd dir-name && ls -lrt

The Following User Says Thank You to jlliagre For This Useful Post:
agama (04-14-2012)
Sponsored Links
    #5  
Old 04-14-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 506 Times in 485 Posts
Quote:
Does this means I am not innovative or dumb...I know how to script...why this stuff did not come to my mind?

There is a one way to know, can you tell me how did you first come to know about this? I've read unix books, but none talk like this at command level.
Some of these kinds of things come with time, and experience and through asking questions as you did. Hard to say when I first learned something like this. The one thing that I do know is that I tend to learn something new with nearly every question that I answer. This turns out to be no exception when looking at jlliagre's post -- I tend to think in traditional if-then-else constructs when I depend on successful command execution and thus don't think about the command && command style one liners which as pointed out is better than simply banging two commands onto one line.

Last edited by agama; 04-14-2012 at 03:03 PM.. Reason: clarification
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
running multiple command in a single line anuragpgtgerman Shell Programming and Scripting 6 03-29-2012 07:19 AM
Multiple lines in a single column to be merged as a single line for a record Bhuvaneswari Shell Programming and Scripting 1 08-11-2011 03:16 AM
Grep multiple strings in multiple files using single command gagan4599 UNIX for Dummies Questions & Answers 2 01-19-2011 06:22 AM
make multiple line containing a pattern into single line VTAWKVT Shell Programming and Scripting 13 12-04-2008 05:40 PM
single line input to multiple line output with sed mitch Shell Programming and Scripting 8 07-16-2008 01:57 PM



All times are GMT -4. The time now is 05:47 AM.