LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-20-2016, 01:35 PM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 258

Rep: Reputation: Disabled
cd && ls


hi guys,

on a previous linux system i was able to write this little "alias/script"


Code:
alias ll='ls -alhF'
change_dir_and_list
 {
 cd "$1" && ll
 }
alias cd='change_dir_and_list'

and it worked perfectly, now the little "alias/script" doesn't work. if i remember right before i was running redhat enterprise 7. now my linux box is running centOS linux 6.7

can somebody tell me what i missed or if there's another way to do this?

my goal is so that everytime i cd a listing of all the files in that directory is shown

thanks!

Todd
 
Old 12-20-2016, 02:25 PM   #2
timl
Member
 
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 744

Rep: Reputation: 156Reputation: 156
What exactly doesn't work? I had to change the first line as ll is already defined on my F25 system
Quote:
[tim@comedian ~]$ cat elias
alias lll='ls -alhF'
change_dir_and_list
{
cd "$1" && lll
}
alias cd='change_dir_and_list'
[tim@comedian ~]$ ./elias Downloads/
./elias: line 2: change_dir_and_list: command not found
./elias: line 4: lll: command not found
 
Old 12-20-2016, 02:53 PM   #3
atjurhs
Member
 
Registered: Aug 2012
Posts: 258

Original Poster
Rep: Reputation: Disabled
it comes back saying

Code:
change_dir_and_list: command not found

Last edited by atjurhs; 12-20-2016 at 02:56 PM. Reason: corrected cmd/error syntax
 
Old 12-20-2016, 02:53 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
If I may offer:
Code:
alias cd="cd $1 && ls -alhF"
 
Old 12-20-2016, 03:03 PM   #5
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Current: Fedora and OpenSUSE. Past: Debian, Arch, RedHat (pre-RHEL). FreeBSD & OpenBSD, macOS (hack)
Posts: 1,272
Blog Entries: 7

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by atjurhs View Post
hi guys,

on a previous linux system i was able to write this little "alias/script"


Code:
alias ll='ls -alhF'
change_dir_and_list
 {
 cd "$1" && ll
 }
alias cd='change_dir_and_list'
How are you using this "alias/script?"

If you are using it as a script, why are you even using an alias in it? That alias goes out of scope when the script completes.

If you want to use the alias or included function from the shell prompt, you must put in a startup file or at the least source it before trying to use it, or the shell will not no about it.

Before use, or add it somewhere to your shell's startup files:


Code:
source aliasfile
or
Code:
. aliasfile
The period is a synonym for the source builtin. I prefer using the more verbose source as it's obvious (I hate going through my distro's stock startup files to find stuff, as searching for a period is a PITA).

Last edited by goumba; 12-20-2016 at 03:06 PM.
 
Old 12-20-2016, 04:28 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
I believe the change_dir_and_list must have a "(){" immediately following the command, as in
Code:
change_dir_and_list(){
 cd "$1" && ll
}
alias cd='change_dir_and_list'
 
Old 12-21-2016, 08:41 AM   #7
atjurhs
Member
 
Registered: Aug 2012
Posts: 258

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
I believe the change_dir_and_list must have a "(){" immediately following the command, as in
Code:
change_dir_and_list(){
 cd "$1" && ll
}
alias cd='change_dir_and_list'
jpollard, you are correct the alias does required the () to run, i forgot to include that

many thanks!

Todd
 
Old 12-21-2016, 09:21 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,976

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
Remember to mark as SOLVED once you have a solution
 
Old 12-21-2016, 01:19 PM   #9
atjurhs
Member
 
Registered: Aug 2012
Posts: 258

Original Poster
Rep: Reputation: Disabled
yep, grail, it's been a while since my last posting, i forgot, thanks for the reminder!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: RDO Liberty Set up for three Nodes Controller&Network&Compute (ML2&OVS&VXLAN) on CentOS 7.1 LXer Syndicated Linux News 0 10-23-2015 03:50 AM
LXer: RDO Kilo Set up for three F22 VM Nodes Controller&Network&Compute (ML2&OVS&VXLAN) LXer Syndicated Linux News 0 05-31-2015 07:22 AM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Japanese canna won't work : Warning: かな漢字変&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
Phục hồi dữ liệu bị mất???, cứ pollsite General 1 06-27-2005 12:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration