Append all files in a folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append all files in a folder
# 1  
Old 06-28-2010
Append all files in a folder

Hi All,

I have directory in which I have around 50 files with filename as:

abcs_1
afaa_2
asda_3
agfa_4
.
.
sada_50

I want to append all files in sada_50 i.e first ssdd_49 in sada_50. Then
append asda_48 in (ssdd_49 in sada_50).

As number of files are more I do not feel like going for "cat 48 49 > 50" and I want to write a generic script in which I mention the max file number (here 50) and I get all files in single.

I have tried to write but not I am new to shell and not able to find exact soln.

$ appendfiles.bash 50

Code:
maxcount=$1
count=maxcount
MY_DIR=/home/my/dir/

maxfile= 'ls -l' | grep $maxcount

echo $maxfile

while test $count -le 1
do
echo $count
count=`expr $count - 1`
echo $count

nextminfile= 'ls -l' | grep $count
cat MY_DIR/newDir/$nextminfile > MY_DIR/newDir/$maxfile
done

Please help.
Thanks !!
# 2  
Old 06-28-2010
Hi,

This should get you going. You can compare the differences and take it from there:


Code:
maxcount=$1
count=$maxcount
MY_DIR=/home/my/dir/
if [ ! -d $MY_DIR/newDir ]; then
  mkdir -p  $MY_DIR/newDir
fi
cd $MY_DIR/newDir
# maxfile= 'ls -l' | grep $maxcount
maxfile=*_$maxcount
echo $maxfile
until test $count -le 1
do
  echo $count
  count=`expr $count - 1`
  echo $count
  # nextminfile= 'ls -l' | grep $count
  nextminfile=*_$count
  echo $nextminfile
  cat $nextminfile >> $maxfile
done

# 3  
Old 06-29-2010
Quote:
Originally Posted by Scrutinizer
Hi,

This should get you going. You can compare the differences and take it from there:


Code:
maxfile=*_$maxcount
...<snip>...
nextminfile=*_$count

Variable assignments do not undergo filename expansion. Those asterisks will be taken literally.

Regards,
Alister

---------- Post updated at 03:08 PM ---------- Previous update was at 03:07 PM ----------

A possible alternative or starting point:
Code:
#!/bin/sh

i=0 max=$1
while [ $((++i)) -lt $max ]; do
    cat *_$i
done > "$(echo *_$max)" 2>/dev/null

Regards,
Alister
# 4  
Old 06-29-2010
Quote:
Originally Posted by alister
Variable assignments do not undergo filename expansion. Those asterisks will be taken literally.

Regards,
Alister
They do not need to. The expansion is done in this statement:
Code:
cat $nextminfile >> $maxfile

# 5  
Old 06-30-2010
Quote:
Originally Posted by pandeyak
As number of files are more I do not feel like going for "cat 48 49 > 50"
How much more? (How big?) - At least for me, the following worked, read: processed several hundred small plain text files without any problem (on a workstation, though):

Code:
cat $( ls *.txt ) > collection

# 6  
Old 06-30-2010
This should work if i have understood your question properly

Code:
for i in `ls /path/to/dir`
do
`echo $i >> out.txt`
done

# 7  
Old 06-30-2010
Hi


Code:
awk 'FNR==1{split(FILENAME,a,"_"); if (a[2]<m) y=system("cat " FILENAME);}' m=50 * > sada_50

Guru.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

3. Shell Programming and Scripting

Move files from Space Folder to other folder

I want to move a folder with spaces from one folder to another. I have two folders like this, 1).RT_032-222 -4444-01/ 2). RT_032-555 -7777-01/ I want to move files from 2 to 1 through shell script.Here I want to assign this like a user defined variable like as Source branch... (2 Replies)
Discussion started by: kannansoft1985
2 Replies

4. Shell Programming and Scripting

How to copy files with the same filenames as those in another folder to that same folder?

Hello All A similar question like this was asked before but I need to change part of the question. I've two folders, Folder A contains some image files in 150 subfolders; Folder B contains text files in 350 subfolders. All image files in Folder A have the same filename as the text... (5 Replies)
Discussion started by: chlade
5 Replies

5. UNIX for Dummies Questions & Answers

How to append time to folder

Hi i just want to create a folder with current time. For example. foldername_09042010 How do i modify mkdir or write script to do this? (1 Reply)
Discussion started by: pinga123
1 Replies

6. Shell Programming and Scripting

delete all the files in folder a which exist in folder b

Hi , I need a script which basically deltes all files in folder a which are alreasy present in folder b say folder a has files abc.txt pqr .txt and b has abc.txt pqr.txt rmr.txt then file abc.txt and pqr.txt from a should be deleted (6 Replies)
Discussion started by: viv1
6 Replies

7. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

8. Shell Programming and Scripting

check how many files in folder or total files in folder

Hi all, I have copied all my files to one folder.and i want to check how many files (count) in the folder recently moved or total files in my folder? please send me the query asap. (3 Replies)
Discussion started by: durgaprasad
3 Replies

9. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

10. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies
Login or Register to Ask a Question