|
Home > Archive > Unix questions > March 2004 > Pls help with Unix shell question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Pls help with Unix shell question
|
|
|
| Hi all,
Not having worked with Unix script for a while, suddenly, I
have a need. The only thing I can do on Unix now is "vi", which I guess I
will never forget.
I have a bunch of files, ending with ".data" in a Unix directory, which I
want to process in date order with a program. I recall doing
something similar in the old days. Does anyone have an example?
Thanks,
Bill
| |
| Bit Twister 2004-03-05, 10:34 am |
| ["Followup-To:" header set to comp.unix.misc.]
On Fri, 05 Mar 2004 15:06:46 GMT, wl wrote:
> Hi all,
>
> Not having worked with Unix script for a while, suddenly, I
> have a need. The only thing I can do on Unix now is "vi", which I guess I
> will never forget.
>
> I have a bunch of files, ending with ".data" in a Unix directory, which I
> want to process in date order with a program. I recall doing
> something similar in the old days. Does anyone have an example?
It probably will depend on which date, creation, updated, .....
you could use find to get a list and if you want only files with a key
word you might need xargs.
http://groups.google.com/advanced_group_search
find xargs in the first box
*linux* in Newsgroup, U need 2 use *, pick English
| |
| Icarus Sparry 2004-03-05, 11:36 am |
| On Fri, 05 Mar 2004 15:06:46 +0000, wl wrote:
> Hi all,
>
> Not having worked with Unix script for a while, suddenly, I
> have a need. The only thing I can do on Unix now is "vi", which I guess I
> will never forget.
>
> I have a bunch of files, ending with ".data" in a Unix directory, which I
> want to process in date order with a program. I recall doing
> something similar in the old days. Does anyone have an example?
Assuming that you don't have any newline characters in your filenames,
this is easy.
The clues you need are
1) The shell will expand *.data into a list of files.
2) ls -t will sort files into time order. You might need '-u' or '-a'
depending on which time you want. Note well that Unix does not store the
'Creation' time. You might want '-r' as well.
3) The shell has 'while' loops.
Put this together and you get
ls -t *.data | while read filename
do
myprogram $filename
done
| |
| Michael Heiming 2004-03-05, 11:36 am |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
wl <bli01@yahoo.com> wrote:
[..]
> I have a bunch of files, ending with ".data" in a Unix directory, which I
> want to process in date order with a program. I recall doing
> something similar in the old days. Does anyone have an example?
for f in `ls -atr *.data`
do
<something with> $f
done
Hint:
man ls
- --
Michael Heiming (GPG-Key ID: 0xEDD27B94)
Remove +SIGNS and www. if you expect an answer, sorry for
inconvenience, but I get tons of spam.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD4DBQFASKGkAkPEju3Se5QRAnpIAKCtOGlOADWl
NV3PgUu12/4jH3oC3QCXYJNB
9/OQqkVrvlKCHtbvorwJxw==
=ku4g
-----END PGP SIGNATURE-----
| |
| Michael Tosch 2004-03-05, 12:34 pm |
| In article <aI02c.33676$hm4.26678@newsread3.news.atl.earthlink.net>, "wl" <bli01@yahoo.com> writes:
> Hi all,
>
> Not having worked with Unix script for a while, suddenly, I
> have a need. The only thing I can do on Unix now is "vi", which I guess I
> will never forget.
>
> I have a bunch of files, ending with ".data" in a Unix directory, which I
> want to process in date order with a program. I recall doing
> something similar in the old days. Does anyone have an example?
>
> Thanks,
> Bill
>
>
maybe you want
ls -t *.data | program
ls -lt
shows how it is sorted.
ls -ltr
sorts backwards.
Man pages:
man ls
--
Michael Tosch
IT Specialist
HP Managed Services Germany
Phone +49 2407 575 313
Mail: michael.tosch@hp.com
| |
| Chris F.A. Johnson 2004-03-05, 12:34 pm |
| On Fri, 05 Mar 2004 at 15:49 GMT, Michael Heiming wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> wl <bli01@yahoo.com> wrote:
> [..]
>
>
> for f in `ls -atr *.data`
This breaks is there are spaces in the file names. Use:
ls -atr *.data | while IFS= read -r f
Of course, this will not work if there are newlines in any of the
file names. But I would consider that a broken file system.
> do
><something with> $f
> done
>
> Hint:
> man ls
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| Michael Heiming 2004-03-05, 1:34 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Chris F.A. Johnson <c.fa.johnson@rogers.com> wrote:
> On Fri, 05 Mar 2004 at 15:49 GMT, Michael Heiming wrote:
Hi Chris!
[color=darkred]
> This breaks is there are spaces in the file names. Use:
> ls -atr *.data | while IFS= read -r f
> Of course, this will not work if there are newlines in any of the
> file names. But I would consider that a broken file system.
Yep, albeit I'd call using spaces in filenames broken, sure most
*nix FS will handle it just fine, albeit it requires lots of
quoting in scripts and isn't IMHO worth the trouble.
- --
Michael Heiming (GPG-Key ID: 0xEDD27B94)
Remove +SIGNS and www. if you expect an answer, sorry for
inconvenience, but I get tons of spam.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFASL82AkPEju3Se5QRAil/AKCxrrfd1F0GUGZbXE3W2UrBXdpJ4ACgxpAM
lQqRoTqDCPAsWVsu10o4OjM=
=xPR1
-----END PGP SIGNATURE-----
| |
| Kenny McCormack 2004-03-06, 8:33 am |
| In article <ovea2c.n3l.ln@news.heiming.de>,
Michael Heiming <michael+USENET@www.heiming.de> wrote:
....
>
>Yep, albeit I'd call using spaces in filenames broken, sure most
>*nix FS will handle it just fine, albeit it requires lots of
>quoting in scripts and isn't IMHO worth the trouble.
Unfortunately, spaces in filenames have become a fact of life, and cannot
be ignored. The operative question is: Does Windows (where most of these
problems originate) allow filenames with newlines? Luckily, I believe the
answer is no.
| |
| Michael Heiming 2004-03-06, 8:33 am |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[ Followup-To: comp.unix.shell ]
Kenny McCormack <gazelle@yin.interaccess.com> wrote:
> In article <ovea2c.n3l.ln@news.heiming.de>,
> Michael Heiming <michael+USENET@www.heiming.de> wrote:
> ...
[color=darkred]
> Unfortunately, spaces in filenames have become a fact of life, and cannot
> be ignored. The operative question is: Does Windows (where most of these
Luckily not on my systems, so I can easily ignore the problem.
> problems originate) allow filenames with newlines? Luckily, I believe the
> answer is no.
Don't know, I don't do "M$ Windows".
;)
- --
Michael Heiming (GPG-Key ID: 0xEDD27B94)
Remove +SIGNS and www. if you expect an answer, sorry for
inconvenience, but I get tons of spam.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFASc2sAkPEju3Se5QRAjOnAKDXNsG63MEt
GdMlvQD5U8BVQI9pSQCgjD8n
+TwqRnL4gBzqM57ecmHmqMA=
=WIQ5
-----END PGP SIGNATURE-----
| |
| Ross Tucker 2004-03-07, 12:33 am |
| Hi Bill:
You could try a script something like the following:
----------------------------------------------------
#! /bin/sh
for x in `ls -t *.data`
do
echo "Processes go here"
done
----------------------------------------------------
Note that the "`" quote character is the "backquote" or "backtick"
character on the same key as the tilde "~" character. (Should be
upper left corner of the keyboard.)
An alternative format for:
for x in `ls -t *.data`
at least for the Korn and Bash shells, but not the Bourne shell, is:
for x in $(ls -t *.data)
The "ls" command can display and sort by all three times stored in
each file's inode. The date/times are:
- last Access Date/Time (last time file was accessed/read)
- last Change Date/Time (last time file was modified)
- last Inode Change Date/Time (i.e.: changed files permissions or
ownership or date/time (via "touch" command) or number of links
or Group ownership or disk storage block numbers. Those are
the only actions I can think of that changes the inode but not
the file's contents.)
The "ls" command can display files according to these three times.
The invocation would be:
- ls -t # Last Change Time
- ls -tu # Last Access Time
- ls -tc # Last Inode Change Time
You can pick the switches that best fit your "process in date order"
requirement.
Note that most books and articles STILL INCORRECTLY STATE that a
file's inode contains the original Creation Date. UNIX does not
store that date, they store the Last Change date instead. (In the
case where a file is never modified, the Last Change Date is also the
file's Creation Date.)
Hope this helps. If not, feel free to ask via email (for
clarifications) if you would like to, since I haven't read newsgroups
very much these past few years.
Ross
wl wrote:
> Hi all,
>
> Not having worked with Unix script for a while, suddenly, I
> have a need. The only thing I can do on Unix now is "vi", which I
> guess I will never forget.
>
> I have a bunch of files, ending with ".data" in a Unix directory,
> which I
> want to process in date order with a program. I recall doing
> something similar in the old days. Does anyone have an example?
>
> Thanks,
> Bill
| |
| Dennis McCunney 2004-03-07, 12:33 am |
| Kenny McCormack wrote:
> Unfortunately, spaces in filenames have become a fact of life, and cannot
> be ignored. The operative question is: Does Windows (where most of these
> problems originate) allow filenames with newlines? Luckily, I believe the
> answer is no.
Correct. Windows will throw an error if you try to do that.
______
Dennis
| |
| Chris F.A. Johnson 2004-03-07, 2:33 am |
| On Sun, 07 Mar 2004 at 04:45 GMT, Ross Tucker wrote:
> Hi Bill:
>
> You could try a script something like the following:
> ----------------------------------------------------
> #! /bin/sh
> for x in `ls -t *.data`
> do
> echo "Processes go here"
> done
> ----------------------------------------------------
> Note that the "`" quote character is the "backquote" or "backtick"
> character on the same key as the tilde "~" character. (Should be
> upper left corner of the keyboard.)
>
> An alternative format for:
> for x in `ls -t *.data`
> at least for the Korn and Bash shells, but not the Bourne shell, is:
> for x in $(ls -t *.data)
Neither of which should be used, as it will break if there are
files with spaces in their names. Instead use:
ls -t *.data | ( IFS=
while read file
do
: do whatever
done
)
Other inadvisable characters may still cause problems, but they
are rarely encountered.
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| Michael Tosch 2004-03-08, 7:34 am |
| In article <c2chqs$v7$1@yin.interaccess.com>, gazelle@yin.interaccess.com (Kenny McCormack) writes:
> In article <ovea2c.n3l.ln@news.heiming.de>,
> Michael Heiming <michael+USENET@www.heiming.de> wrote:
> ....
>
> Unfortunately, spaces in filenames have become a fact of life, and cannot
> be ignored. The operative question is: Does Windows (where most of these
> problems originate) allow filenames with newlines? Luckily, I believe the
> answer is no.
>
The empire strikes back:
let's together demand that a *nix file manager defaults to
N:ew\File
and
N:ew\Folder
--
Michael Tosch
IT Specialist
HP Managed Services Germany
Phone +49 2407 575 313
Mail: michael.tosch@hp.com
|
|
|
|
|