|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Using any file in folder as command input
Hi all, I have a command I need to run on a large number of folders. It needs only one input, namely one of the 150 files in the folder (doesn't matter which one): Code:
command filename The problem is that the names of the files are random, so I was trying to just get the first file by using list and inputting this by making a shell variable of it, but I've had no success as a 4-month Unix newbie... Thanks for any tips! |
| Sponsored Links | |
|
|
|
#2
|
||||
|
||||
|
Well, Code:
ls -1 | head -1 would show you the first file in the current directory. (by the way, that is -1 as in the number 1 and not the letter L) |
| Sponsored Links | ||
|
|
|
#3
|
|||
|
|||
|
That sounds good, but how do I now input the output of this to a new command? I can only input the name of the file to the command (it is a custom-made medical imaging command)...
|
|
#4
|
||||
|
||||
|
You can do it directly with one of the following two commands. depending on your unix flavor, one or both may work. Code:
ls -l `ls -1 | head -1` ls -l $(ls -1 | head -1) Take a look here for examples on assigning output to variables. http://www.unix.com/shell-programmin...-variable.html Last edited by joeyg; 02-08-2012 at 02:59 PM.. Reason: Added more info |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Thanks for the reply again. I still cannot get it to work (I'm using tcsh by the way). Why do you add another ls -1? I tried this: Code:
set foo = $(ls -1 | head -1) echo $foo and Code:
set foo = 'ls -1 | head -1' echo $foo But it only returns the command, not the filename. |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
Code:
ls -l `ls -1 | head -1` That is LS -L in lower case Simply a command to execute on the result of the stuff inside ` quotes. Make sure to use the ` at the top left of the keyboard, and not the ' next to the ENTER key. |
| The Following User Says Thank You to joeyg For This Useful Post: | ||
Linnnnn (02-08-2012) | ||
| Sponsored Links | ||
|
|
![]() |
| Tags |
| input output, list |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Awk command without input file | saikiran_1984 | Shell Programming and Scripting | 3 | 01-11-2011 11:45 AM |
| Execute commands to specific folder from input file | dragon.1431 | Shell Programming and Scripting | 5 | 05-21-2010 04:03 AM |
| Unix Command to rename a file in a zipped folder | Victoria.Sam | UNIX for Advanced & Expert Users | 1 | 04-21-2010 06:09 PM |
| Moving 100K file to another folder using 1 command | unx100 | Shell Programming and Scripting | 6 | 12-05-2009 02:29 AM |
| Disk Usage in GB and Unix command to find the biggest file/folder | manas6 | UNIX for Dummies Questions & Answers | 8 | 07-30-2008 07:54 AM |
|
|