how to find first files in a directory and combine them as a single file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find first files in a directory and combine them as a single file?
# 1  
Old 06-28-2012
how to find first files in a directory and combine them as a single file?

i have below list of files in a directory.

Code:
/root/admin/files/file1.txt
/root/admin/files/file2.txt
/root/admin/files/file3.txt
/root/admin/files/pattern.txt
/root/admin/files/server.txt

i need combine the above text files in the below sequence,

file1.txt, pattern.txt,server.txt => group1.txt
file2.txt => group2.txt
file3.txt => group3.txt

i need help find the first files (file1.txt, pattern.txt,server.txt) in a shell script running in a another directory

Code:
/root/admin/script/grouping.sh

# 2  
Old 06-28-2012
Just hard code it in that shell script? I don't see any generic pattern that could be used in another case. Use redirection to aggregate those files.
# 3  
Old 06-28-2012
Okay. I thought there would be some way to get this done instead of hard code.

How to assign file name to a variable here like the below one,

Code:
fn=`ls -ltr /root/admin/files/file* | head -1`
echo cat fn.txt pattern.txt server.txt >> group1.txt

in the above code i am getting the filename with entire path and timestamp. but i need filename alone. please help

Last edited by vel4ever; 06-28-2012 at 08:16 AM..
# 4  
Old 06-28-2012
Code:
 
ls -ltr /root/admin/files/file* | head -1 | awk '{print $NF}'

# 5  
Old 06-28-2012
awk removed the timestamp but still i am getting the entire path

Code:
/root/admin/files/file1.txt

but it has to be file1.txt Smilie
# 6  
Old 06-28-2012
try this

Code:
  
ls -ltr /root/admin/files/file* | head -1 | awk -F "/"  '{print $NF}'

here i have set the field seprator as "/" awk -F "/" and $NF means last field

Last edited by max_hammer; 06-28-2012 at 09:15 AM..
This User Gave Thanks to max_hammer For This Post:
# 7  
Old 06-28-2012
Have you tried this (it gives only file name):

Code:
 
cd /root/admin/files
ls |awk '{print $1}'|head -1

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find directory is getting files in every 10 mins, if not then when last time file received

Dears, I am looking for a script which will work as a watch directory. I ha directory which keep getting files in every 10 mins and some time delay. I want to monitor if the directory getting the files in every 10 mins if not captured the last received file time and calculate the delay. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Shell Programming and Scripting

Search Pattern and combine into single file

Hi Experts Please help me out with the following thing: 2 files and want the output file: {No for using FOR loop because I got 22 million lines} Tried that "It processes only 8000 records per hour" I need a faster way out !!! FileA: 9051 9052 9053 9054 9055 9056 9057 9058 9059 ... (5 Replies)
Discussion started by: navkanwal
5 Replies

3. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

4. Shell Programming and Scripting

Combine Multiple Files into Single One File One after other

I am trying to combine 4 .dat files into one single Output file Inputs are:- file123.dat, file256.dat, file378.dat & file490 Expected Output:- FileName=file1 {text from file1} EOF {blank line} FileName=file2 {text from file2} EOF {blank line} FileName=file3 {text from file3} EOF... (4 Replies)
Discussion started by: lancesunny
4 Replies

5. Shell Programming and Scripting

Find all the files under a specific directory and zip them into a single file.

Hi, Is there a way to find all the files from a specific location and then zip them into a single file, even if they are in multiple directories? (3 Replies)
Discussion started by: rudoraj
3 Replies

6. Shell Programming and Scripting

wanted to find both link file and ordinary file using single find command

find . -type fl o/p is only the ordinary file. where in it wont give the link files. (2 Replies)
Discussion started by: nikhil jain
2 Replies

7. Shell Programming and Scripting

How to find empty files in a directory and write their file names in a text?

I need to find empty files in a directory and write them into a text file. Directory will contain old files as well, i need to get the empty files for the last one hour only. (1 Reply)
Discussion started by: vel4ever
1 Replies

8. Shell Programming and Scripting

Combine in single file

I have 2 files: phone.txt and mobile.txt ex. phone.txt MOBILENO|DISABLE_DATE 919687877754|9/1/2011| 919687877762|9/1/2011| 919687880573|9/1/2011| 919687882658|9/2/2011| Ex. mobile.txt MOBILENO |TIME 919687880573|2011-09-17 12:23:40| 919687882658|2011-10-10 21:15:33|... (4 Replies)
Discussion started by: khingx
4 Replies

9. Solaris

Find files older than x days and create a consolidated single tar file.

Hello, I need help in finding files older than x days and creating a single consolidated tar file combining them. Can anyone please provide me a script? Thanks, Dawn (3 Replies)
Discussion started by: Dawn Bosch
3 Replies
Login or Register to Ask a Question