Shell Scripting: Piping part of ls output

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Shell Scripting: Piping part of ls output

Post by nigma »

I've got a bunch of files in the form:
Apple (2006-04-11)
Berry (2006-01-13)

I'd like to run ls on the directory containing these files and then pipe only the date portion (YYYY-MM-DD) of the filenames into another file.

Can anyone point me the right direction?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

untested

Code: Select all

ls | sed "s/.*(//g" | sed "s/)//g" > file.list
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or

Code: Select all

ls | sed "s/[^0-9-]//g" > file.list
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

or

Code: Select all

ls | cut -d " " -f 2
cut -d " " -f 2 : display second field of a space-sparated list.
see http://www.gnu.org/software/coreutils/m ... ils_8.html
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

volka wrote:or

Code: Select all

ls | cut -d " " -f 2
cut -d " " -f 2 : display second field of a space-sparated list.
see http://www.gnu.org/software/coreutils/m ... ils_8.html
Heh, cool, how come I've never seen cut used before? :( I should do more bash scripting :)
Post Reply