Bash alias for complicated ls command does not work.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash alias for complicated ls command does not work.
# 1  
Old 03-16-2010
Bash alias for complicated ls command does not work.

I'm trying to set up an alias in .bash_aliases to show just the filenames of the files in a directory, which the following command will do:

Code:
ls -l | grep ^- | awk '{print $NF}'

kjb.zip
ap.zip
tor.zip

However when I set up the following alias in .bash_aliases:

Code:
alias lf="ls -l | grep ^- | awk '{print $NF}'"

it only partially works. I get just the files listed but the -l longlisting too (permissions, owner, size, date, filename) and not just the filename on its own.

I'm guessing this is to do with the " and ' formatting but I can't seem to format it correctly. All the variations I've tried either fail with an awk error because the 'print' section isn't right, or do the same thing as already stated.

Any ideas? Thanks all.

Last edited by Franklin52; 03-17-2010 at 04:36 AM.. Reason: Please use code tags!
# 2  
Old 03-16-2010
Try this.

Code:
alias lf="ls -la|grep \^\-|awk '{print \$9}'"


Last edited by methyl; 03-16-2010 at 01:03 PM.. Reason: First version included directorries.
# 3  
Old 03-16-2010
Define a function:

Code:
lf() {
  perl -le'
    -f and print for glob "*"
    '
    }

With zsh it would be as simple as:

Code:
print -l -- *(.)

If you want to include dotfiles:

Code:
lf() {
  perl -le'
    -f and print for glob "* .?*"
    '
  }

With zsh:

Code:
print -l -- *(D.)

# 4  
Old 03-16-2010
Quote:
Originally Posted by methyl
Try this.

Code:
alias lf="ls -la|grep \^\-|awk '{print \$9}'"

That gave me a series of empty lines, but the right amount for it to be one line per file in dir.

When I replaced the $9 with $NF it worked perfectly. Thanks for your help it is appreciated.

Cheers.

PS. Thanks too radoulov, but I prefer the alias solution now that it is working.
# 5  
Old 03-16-2010
The reason it is not working is because it is expanding the $NF to an empty string when you define the alias:

Code:
>alias test_alias="ls -l | grep ^- | awk '{print $NF}'"

>alias test_alias
test_alias='ls -l | grep ^- | awk '\''{print }'\'

Try to excape the $ and it'll most likely work:

Code:
>alias test_alias="ls -l | grep ^- | awk '{print \$NF}'"
>alias test_alias
test_alias='ls -l | grep ^- | awk '\''{print $NF}'\'

Note that I tested this on ksh, not on bash.
# 6  
Old 03-16-2010
soleil4716 - thanks that also works.

However I've descovered a problem with my original command. It does not work with filenames with spaces in them. Printing everything after the final space only.

EG. filename is:

this is a test.txt

will ouput just

test.txt

Damn, just when I thought it was working.

Any ideas how to fix this?

Thanks again.
# 7  
Old 03-16-2010
Quote:
Originally Posted by gencon
soleil4716 - thanks that also works.

However I've descovered a problem with my original command. It does not work with filenames with spaces in them.[...]
Any ideas how to fix this?
Yes,
try the code I posted Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Alias does not work with bash profile

Hi, Below is what i have in my profile: alias wldm='cd /opt/app/wls' If i use bash or ksh shell this alias does not work. What should be done for this alias to work with all these simultaneously -> No Shell, bash shell, and ksh shell (14 Replies)
Discussion started by: mohtashims
14 Replies

2. UNIX for Dummies Questions & Answers

Using alias to create subshell and work in it

using kerberos to access remote server. first I execute kshell to create subshell and then kinit username@domain in that subshell. After that it prompts to enter password. Since I need to do this over and over, I am trying creating alias. I tried kshell; kinit username@domain. Did not work,... (7 Replies)
Discussion started by: analyst
7 Replies

3. Shell Programming and Scripting

complicated exclude option in find command

Hi all, In a directory, I have many video files. Example : As you can see, some of the video files come with a .aspx file (wich means the video is actually being uploaded and not entirely written on the FS) I try to write a bash script that would find all video files in the ... (1 Reply)
Discussion started by: gniagnia
1 Replies

4. UNIX for Dummies Questions & Answers

A very simple script, but alias won't work

I am new to unix and therefore I did a lot of reading before posting. So please, if this has been answered before, forgive me for re-posting and point me to the right place for the answer. I have spent many hours searching the net and read over 50 posts in this forum and even tried a few thing but... (20 Replies)
Discussion started by: sssccc
20 Replies

5. Shell Programming and Scripting

complicated alias command

hi guys i m making one alias which will set variable , invoke sqlplus and also set prompt of sqlplus,,i have made successfully upto invoking sqlplus in unix but cant pass command in sqlplus here is the command alias sett='export ORACLE_SID=devdb2;sqlplus system/system@test' now this... (3 Replies)
Discussion started by: tapia
3 Replies

6. Linux

Complicated Join command!

Hi I have a serious issue when trying to join to files so I have two files, one for meals and one for people Meal1:Turkey:Potato Chips:Twinkie:Coke:5.95 Meal2:Ham & Cheese:Doritos:Cookie:Sprite:6.49 Meal3:Vegetarian:Cheese Crackers:Brownie:Pepsi:5.75 Meal4:Tuna:Cheese Puffs:Eclair:Diet... (1 Reply)
Discussion started by: ehshi1992
1 Replies

7. Shell Programming and Scripting

Global alias does not work in shell script

Hi Linux Set up - alias ls='ls -l' Then run script #! /bin/ksh sub() { ls } sub Is there any way to get it working. I don't want to define alias inside of the program Thank you (2 Replies)
Discussion started by: zam
2 Replies

8. UNIX for Dummies Questions & Answers

Alias, function or script (bash) to "revert" cd command?

In all of my brief and superficial experience with Unix or Linux, the one curious and consistent thing has been that 'cd ./' (back up one directory level) has done absolutely nothing in any of them. Now I understand that, at least for bash, 'cd ./' appears to have been substituted by 'cd ..' Am... (1 Reply)
Discussion started by: SilversleevesX
1 Replies

9. Shell Programming and Scripting

alias doesn't work

Hi I have put alias ll='ls -la' in .profile file but it doesn't work. On hand it works it looks like the .profile file is not beeing read. How to check whitch file is loaded? ,profile? .bash_profile? My system: SunOS mion 5.10 Generic Shell: /bin/pfksh Thanks (2 Replies)
Discussion started by: miojamo
2 Replies

10. UNIX for Dummies Questions & Answers

more command does not work in bash shell

is there a different command to display contents of a file on the output in bash shell? i tried more and it does not work. (7 Replies)
Discussion started by: npatwardhan
7 Replies
Login or Register to Ask a Question