Cat N files in a folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cat N files in a folder
# 1  
Old 02-18-2017
Cat N files in a folder

I am new to Linux and I am trying to cat only N files in a folder. N is dynamically given number at runtime.
If I give N as 2 then cat only 2 files in the folder
and If I give N as 5 then cat only 5 files in the folder.

Is there any way to do that?
# 2  
Old 02-18-2017
Assume 1000 files AND this is NOT homework.
The first 5 files of what?

Latest date, earliest date, file length, file extension, any 5 from any random files, file size, first 5, last five, 5 consecutive files somewhere inside those 1000 files, 5 with an RE somewhere inside the filename and so on...

Please be more specific...

Also show us any attempts you have tried, OS and shell you wish to use...
# 3  
Old 02-18-2017
This is not a homework. I am trying to do couple of experiments using cat command. So wanted to try this one

The files are sorted in the order of name . For Example to display the results of first N consecutive files. I am not yet started with any programming as I don't know how to start it.
# 4  
Old 02-18-2017
There are thousands of ways to do this (if I'm guessing correctly that you want to cat the 1st five unhidden files in the current working directory sorted alphanumerically and that you are using a POSIX conforming shell and that none of the filenames in your directory contain any <newline> or IFS characters). One trivial way to do that if my assumptions are correct would be:
Code:
cat $(ls | head -n 5)

If any of my assumptions are incorrect, you would need to actually tell us what programming environment you're using and accurately describe what you are really trying to do.
# 5  
Old 02-19-2017
I am mostly into hadoop and started learning the basic linux and shell scripting.
Right now I am using RedHat OS.

I have written a program to cat data so that I can go to each sub folder and generate the concatenation of data as output text file for each folder.

Right now I am trying to go to each sub folder and generate output text file for N consecutive files. N is dynamically given number for each sub folder to work it out.
# 6  
Old 02-19-2017
We would be happy to try to help you if you would be willing to tell us exactly what it is that you're try8ing to do. You have made three posts in this thread and what you want has changed in each post.

What code have you written?

What is it failing to do that you want to do?

How are these dynamic values supposed to be determined?

In what directory are these subfolders (i.e., directories) to be found?

Are all of the directories in the parent directory to be processed, is there a list of specific directories to be processed, do the directories to be processed have a certain naming convention that separates them from other directories in the parent directory? In other words; how do you identify what directories are to be processed and how many dynamic files are to be concatenated from each one?
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 02-20-2017
Quote:
Originally Posted by KMusunuru
This is not a homework. I am trying to do couple of experiments using cat command. So wanted to try this one
There are many ways, but none of this is related to the cat command. Also, it depends on which shell you are using. Here is for instance how to cat the first N non-dot files in alphabetic order when running zsh:

Code:
cat *([1,$N])

(Maybe a similar solution also exists with bash).
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. 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

6. 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

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

Moving Files from one folder to another folder

Hi, I have a folder which contain the log files. The folder may contain sub folders as well. I want to move the contents of the log folder to tmp folder periodically. I have used the command. LOG_DIR=/logs DESTINATION_DIR=/tmp/logs find ${LOG_DIR} -mtime +1 -exec mv {}... (10 Replies)
Discussion started by: farooqpervaiz
10 Replies
Login or Register to Ask a Question