Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
thanks in adv for looking through the long winded post...
i'm trying to cat a bunch of text files together into one, but i'm running into problems and i think it's because of how 'find' is working, but i'm not really sure. here's the command i'm using
for d in `find . -maxdepth 1 -mindepth 1 -type d | sort -V`; do
for f in `find $d -maxdepth 1 -mindepth 1 -type f -name "*.dat" | sort -V`; do
cat $f >> out_file
done
done
In the find command you should put curly bracket in either single or double quotes. Otherwise it would be interpreted by the shell and not find command.
In the find command you should put curly bracket in either single or double quotes. Otherwise it would be interpreted by the shell and not find command.
Makes no difference in this case. The shell is smarter than that.
The output from find is unsorted, just the order in which it encounters the entries in each directory, similar to what you get if you run "ls -U" to get unsorted output. Some filesystems do store directories in a manner that keeps the names sorted. Most do not. If you want sorted output, pipe the result through sort.
Your naming convention is wrong.
f01.dat, etc
dir01, etc
if the system will grow to hundreds of directories or dat files
f001.dat, etc
dir001, etc
same idea if you want to sort by date-time
20140115-1835
a good rule of programming - the earlier you fix a problem the easier it is to fix
corollary - the more times you try and fix it the worse it gets
you did not specify the order you want. I think find will not sort/reorder the files it found, that is the order of appearance. If you need something else you need to implement that.
for d in `find . -maxdepth 1 -mindepth 1 -type d | sort -V`; do
for f in `find $d -maxdepth 1 -mindepth 1 -type f -name "*.dat" | sort -V`; do
cat $f >> out_file
done
done
This may help.
An execellent example of how not to use find (or almost any other command).
In the find command you should put curly bracket in either single or double quotes. Otherwise it would be interpreted by the shell and not find command.
If you want f10 to sort after f9, then you have to pad 9 to 09.
However, find does not sort files alphabetically; it prints them in the order they are listed in the directory. Shell expansion does sort files alphabetically.
Code:
cat dir*/*.dat > "$file"
If that gives an "Argument list too long" error, proceed directory by directory:
Code:
for dir in dir*
do
cat "$dir"/*.dat
done > "$file"
Last edited by cfajohnson; 01-15-2015 at 08:56 PM.
There's no *need* for printf either. Assuming the OP is using bash, printf is not a shell builtin, a new process is spawned either way. Indeed there are many way to get that output, feel free to post as many of them as you like.
In some cases, you will find it better to use a file than trying to get all the find loops, and sort loops in the proper order.
Find the files and put them in a file... Now you can try various sorts to try and get them in the order you want. When the file is the way you want (or you can just edit one that is close and force it , then use the file as input (for i in $(cat file); do..., or better, while read f; do... done<file)
For future reference, it is easy to sort file names in alphabetical order. That is why the suggestions to use the form "name.nnn" (or similar) where the nnn is the number of the file with leading zeros. This allows a simple sort to work easily.
For future reference, it is easy to sort file names in alphabetical order. That is why the suggestions to use the form "name.nnn" (or similar) where the nnn is the number of the file with leading zeros. This allows a simple sort to work easily.
If using the sort command from coreutils you can use the -V option to get the desired results without leading zeros.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.