Page 1 of 1 [ 7 posts ] 

Roninninja
Pileated woodpecker
Pileated woodpecker

User avatar

Joined: 15 Apr 2012
Age: 38
Gender: Male
Posts: 191

02 Nov 2012, 8:49 pm

I am new to UNIX and was wondering if any of you would be willing to help me write a shell script? :?:
the script must be able to:

1. give a long listing,
2. or any number of arguments,
3. one screenful at a time
4. by last modification time,
5. all files and only files, but not other object types

here's what I have so far (it isn't much!)

#!/bin/bash

ls -la | more

I'm confused where to go next!


Any tips on good UNIX resources would also be much appreciated.

Thanks!



one-A-N
Veteran
Veteran

User avatar

Joined: 2 Mar 2010
Age: 70
Gender: Male
Posts: 883
Location: Sydney

02 Nov 2012, 9:13 pm

With ls ... -t will sort by modification time (most recent first)

See here for more details: http://linux.die.net/man/1/ls

Not sure you can skip file types using ls alone ... but one trick might be to pipe the output through grep like this:

ls -F | grep -v '/$ | less

grep -v strips out any line that matches the pattern (in this case, where the line ends with a forward slash (the $ sign indicates the end of a line). By using -F with ls you get a character appended to the line that describes the file type.

If you only want one filetype, then just use grep without the -v:

eg ls -F | grep '/' | less will just list the subdirectories.

Play around with these options (and grep) and see what you can do.

PS: try using "less" instead of "more". Just type "man less" and read the description. It is "more" with extra features.

and you might want to try this:

ls -ltF | grep -v '[*/=>@|]$' | less

It means: list long details of all files ordered by most recently modified, marking the file type with a trailing character; then from the output strip out any line that ends with a "filetype character" (see ls -F); then paginate and display using 'less'

You can always insert any command line arguments after the -F inside your script ... like this:

ls -ltF $@ | etc etc

The '$@' means "all command line arguments when the script is called"



amboxer21
Deinonychus
Deinonychus

User avatar

Joined: 23 Jun 2011
Age: 38
Gender: Male
Posts: 350
Location: New Jersey

03 Nov 2012, 12:18 am

If you want to list files and only files. No directories. Without ascending into any dirs, you could use the find command in conjunction with prune and specify the type using the f option for file.

find . ! -name . -prune -type d

It will replicate the ls -a command without printing directories.

if you want to display a page at a time use with with the find command

find . ! -name . -prune -type d | less

I'm not exactly sure what your really after. Maybe you could explain a bit better. Sorry.



UrchinStar47
Sea Gull
Sea Gull

User avatar

Joined: 23 Feb 2009
Age: 38
Gender: Male
Posts: 216

03 Nov 2012, 10:14 am

Code:
#!/bin/bash
ls -Alt | egrep -v '^d' | more

That should do what you want.

If you want reverse order, go with:

Code:
#!/bin/bash
ls -Alt | egrep -v '^d' | sort -r | more



Roninninja
Pileated woodpecker
Pileated woodpecker

User avatar

Joined: 15 Apr 2012
Age: 38
Gender: Male
Posts: 191

04 Nov 2012, 12:54 pm

Basically I need a script that lists all files, omitting directories and that organizes when they were most recently modified.



mutley
Tufted Titmouse
Tufted Titmouse

User avatar

Joined: 29 Oct 2012
Age: 35
Gender: Male
Posts: 31
Location: Huddersfield, UK

04 Nov 2012, 1:12 pm

Something like this, or am I missing something?

Code:
ls -ltaR | grep '^-' | less


If you want just the date and file name:

Code:
ls -ltaR | grep '^-' | awk '{ print $6,  $7,  $8,  $9 }'


I'm not sure what you mean by "or any number of arguments.

Quote:
Any tips on good UNIX resources would also be much appreciated.

This is a short but useful intro:

http://mywiki.wooledge.org/BashGuide


_________________
If you want to talk, please pm me, I'd be glad to hear from you.


mutley
Tufted Titmouse
Tufted Titmouse

User avatar

Joined: 29 Oct 2012
Age: 35
Gender: Male
Posts: 31
Location: Huddersfield, UK

04 Nov 2012, 1:39 pm

I've just realised, I think you want all the files sorted by modified time, and the above only sorts within directories. Is the following what you want? It will only work if you use a date format that sorts lexicographically. I think it's a bit more complex otherwise.

Code:
ls -ltaR --time-style=long-iso | grep "^-" | sort -r -k 6 | less


_________________
If you want to talk, please pm me, I'd be glad to hear from you.