Using any file in folder as command input


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using any file in folder as command input
# 1  
Old 02-08-2012
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!
# 2  
Old 02-08-2012
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)
# 3  
Old 02-08-2012
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  
Old 02-08-2012
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.
https://www.unix.com/shell-programmin...-variable.html

Last edited by joeyg; 02-08-2012 at 03:59 PM.. Reason: Added more info
# 5  
Old 02-08-2012
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.
# 6  
Old 02-08-2012
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.
This User Gave Thanks to joeyg For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regarding file input to SQL from command line

Hi friends, Need your help again to get a best approach for the below scenario. I am previously having one shell script which accepts request_id/s as the command line argument. single req_id arg= 1111 Multiple arg= 1111,2222,3333 which gets passed to the embedded sql inside to... (9 Replies)
Discussion started by: Showdown
9 Replies

2. Shell Programming and Scripting

Manipulation of file/folder with output of command

I like organising some of my folders in the format of yyyymmdd to keep them in chronological order. For example, today which is 5th July 2014, I made a folder named 20140705. I want to manipulate these folders from a shell script, but I cannot find out how to do it without typing the literal name... (5 Replies)
Discussion started by: Gen12345
5 Replies

3. Shell Programming and Scripting

Renice command with input argument as a file

Hi all could you help me how to give pids in a file as an argument to renice command. Thanks in Advance.. :) Pradeep (4 Replies)
Discussion started by: nanz143
4 Replies

4. UNIX for Dummies Questions & Answers

Redirection of file input to command

Hello, I'm new to Unix (working with OS X 10.8.5) and therefore at the beginning of my adventure. If I ask something stupid, then this is not intentional, but simple nescience. :rolleyes: I have a problem with the redirection of text file content to echo. I was experimenting with redirection... (6 Replies)
Discussion started by: pseudo
6 Replies

5. UNIX for Dummies Questions & Answers

Tar command to preserve the folder/file arrangement

Hi, I do have question for un tar a file. I have several 'tar'ed files. For example: SRS.tar.bz2. I was trying to untar them in a linux server using the command: tar xvjf SRS.tar.bz2 It worked perfectly. but when I open this file in my mac computer all the files are extracted into a... (7 Replies)
Discussion started by: Lucky Ali
7 Replies

6. Shell Programming and Scripting

Awk command without input file

i have a requirement to compare two time stamps in IF condition and return true whenever the second timestamp is greater than first, i will also be checking, if the timestamp in HHMMSS format( 6 digit time stamp ).Im able to achieve it using awk, however i dont want to give any input file to awk... (3 Replies)
Discussion started by: saikiran_1984
3 Replies

7. Shell Programming and Scripting

Execute commands to specific folder from input file

Hi, I have one text file input.txt, which has folders path as follows: /home/user/automate/abc /home/user/automate/abc/xyz /home/user/automate/test /home/user/automate/test2 /home/user/automate/test2/abc/Main In those folders i have .svn folder. 1) First i want to remove .svn like rm... (5 Replies)
Discussion started by: dragon.1431
5 Replies

8. UNIX for Advanced & Expert Users

Unix Command to rename a file in a zipped folder

Hi, I am into automation work. Is there any UNIX command to rename a file in a zipped folder, without unzipping it..??? Thanks in advance.. (1 Reply)
Discussion started by: Victoria.Sam
1 Replies

9. Shell Programming and Scripting

Moving 100K file to another folder using 1 command

Hi, I need to move 1000s of files from one folder to another. Actually there are 100K+ files. Source dir : source1 Target dir : target1 Now if try cp or mv commands I am getting an error message : Argument List too long. I tried to do it by the time the files are created in the source... (6 Replies)
Discussion started by: unx100
6 Replies

10. UNIX for Dummies Questions & Answers

Disk Usage in GB and Unix command to find the biggest file/folder

Hi All, Please help me out 1) Command to find the disk usage in GB. I know that du -k will give in kilobites. 2) How to find the Biggest file/folder in a given set of files/folders. Thanks in advance Regards, Manas (8 Replies)
Discussion started by: manas6
8 Replies
Login or Register to Ask a Question