ls |more in bash

GigiG

New Member
Joined
Sep 20, 2023
Messages
9
Reaction score
1
Credits
65
Hi. How can I set an alias in .bashrc to list a folder like alias ll='ls -lA |more', giving the name of the folder at the end of ll?
 


Hi. How can I set an alias in .bashrc to list a folder like alias ll='ls -lA |more', giving the name of the folder at the end of ll?
Hey there! If I'm understanding you correctly, you want to create an alias in your .bashrc file that will list a specific folder when you type 'll' followed by the folder name, is that right? Well, you can achieve that by modifying your .bashrc file. Here's what you can do:

1. Open your .bashrc file using a text editor. You can use the following command to open it in the nano editor:

Code:
nano ~/.bashrc

2. Scroll to the end of the file and add the following line:

Code:
alias ll='ls -lA | more'

3. Save the changes and exit the text editor.

4. Now, whenever you want to list the contents of a specific folder using the 'll' alias, you can do it like this:

Code:
ll /path/to/folder

Just replace /path/to/folder with the actual path of the folder you want to list. The output will be displayed using the 'more' command, allowing you to scroll through it.

Hope that helps, dude! Let me know if you have any other questions.
 
Hi TuxBot! And thanks for the fast reply.
Yes, this is exactly what I'd like to do. I already have an alias alias ll='ls -lA | more' in my .bashrc but, for example, if I use it on my home folder to list the Templates folder content with ll Templates, I got:
... drwx------ 6 gigi gigi 4096 Jun 26 09:35 .thunderbird drwxr-xr-x 3 gigi gigi 4096 May 29 14:46 .var drwxr-xr-x 3 gigi gigi 4096 Aug 28 11:21 Videos drwx------ 6 gigi gigi 4096 Sep 20 09:53 .vnc drwxrwxr-x 2 gigi gigi 4096 Feb 14 2023 Warpinator -rw-rw-r-- 1 gigi gigi 209 May 24 16:03 .wget-hsts -rw------- 1 gigi gigi 98 Sep 20 09:22 .Xauthority -rw------- 1 gigi gigi 12012 Sep 20 13:43 .xsession-errors -rw------- 1 gigi gigi 9937 Sep 19 16:49 .xsession-errors.old *** Templates/: directory ***
at the end of the home folder listing ...
 
Hi TuxBot! And thanks for the fast reply.
Yes, this is exactly what I'd like to do. I already have an alias alias ll='ls -lA | more' in my .bashrc but, for example, if I use it on my home folder to list the Templates folder content with ll Templates, I got:
... drwx------ 6 gigi gigi 4096 Jun 26 09:35 .thunderbird drwxr-xr-x 3 gigi gigi 4096 May 29 14:46 .var drwxr-xr-x 3 gigi gigi 4096 Aug 28 11:21 Videos drwx------ 6 gigi gigi 4096 Sep 20 09:53 .vnc drwxrwxr-x 2 gigi gigi 4096 Feb 14 2023 Warpinator -rw-rw-r-- 1 gigi gigi 209 May 24 16:03 .wget-hsts -rw------- 1 gigi gigi 98 Sep 20 09:22 .Xauthority -rw------- 1 gigi gigi 12012 Sep 20 13:43 .xsession-errors -rw------- 1 gigi gigi 9937 Sep 19 16:49 .xsession-errors.old *** Templates/: directory ***
at the end of the home folder listing ...
Hey again! Ah, I see what you mean. You want the 'll' alias to display the contents of the specified folder without showing the contents of the current folder at the end. No worries, I got your back!

To achieve that, you can modify the 'll' alias by using the 'cd' command to navigate to the specified folder before running the 'ls' command. Here's how you can do it:

1. Open your .bashrc file using a text editor:

Code:
nano ~/.bashrc

2. Locate the line that defines the 'll' alias, which should be:

Code:
alias ll='ls -lA | more'

3. Replace it with the following line:

Code:
alias ll='cd "$@" && ls -lA | more'

4. Save the changes and exit the text editor.

Now, when you use the 'll' alias followed by the folder name, it will navigate to the specified folder and list its contents without displaying the contents of the current folder at the end.

Give it a shot, bro! Let me know if it works for you or if you have any more questions. Stay groovy!
 
This is a good tip. May I return back to the current folder using 'pwd' and 'cd [pwd]'?
 
This is a good tip. May I return back to the current folder using 'pwd' and 'cd [pwd]'?
Absolutely, my dude! You can definitely return back to the current folder using the pwd command and cd with the resulting directory.

Here's how you can do it:

1. Use the pwd command in your terminal to get the current directory's path. It will output something like /path/to/current/folder.

2. Then, to return back to that folder, you can use the cd command followed by the path obtained from pwd. For example:

Code:
shell
   cd /path/to/current/folder

This will navigate you back to the original folder you were in.

It's a handy trick to quickly switch back to the original directory without having to memorize or type out the whole path again.

Keep surfing through the terminal, my friend! Let me know if there's anything else I can help you with.
 
Yes but I'd like to have an alias that will do all these stuff (set pwd, cd, list and return back to pwd) in one command, or alias.
 
Yes but I'd like to have an alias that will do all these stuff (set pwd, cd, list and return back to pwd) in one command, or alias.
I gotcha, dude! If you want an alias that sets the current folder, lists its contents, and then returns back to the original folder in one command, here's what you can do:

1. Open your .bashrc file using a text editor:

Code:
nano ~/.bashrc

2. Add the following lines at the end of the file:

Code:
alias cll='CURR_DIR=$(pwd); cd "$@" && ls -lA | more && cd "$CURR_DIR"'

This alias is called 'cll', short for "current folder list." It first stores the current directory path in the 'CURR_DIR' variable, then changes to the specified directory, lists its contents, and finally returns back to the original directory using the 'cd "$CURR_DIR"' command.

3. Save the changes and exit the text editor.

Now, whenever you want to set the current folder, list its contents, and return back to the original folder, simply use the 'cll' alias followed by the desired folder path. For example:

Code:
cll /path/to/folder

This will give you the listing of the specified folder and take you back to the original directory when you're done. Talk about convenience, right?

Give it a try, my friend! Let me know if it works for you or if you need any further assistance. Stay stoked!
 
Mmmmmm ... I received:
bash: cd: too many arguments
Whoops, my bad, dude! It looks like there was a small oversight in the alias. Let's fix that up for you.

Please modify the alias 'cll' in your .bashrc file as follows:

Code:
alias cll='CURR_DIR=$(pwd); cd "$@" && ls -lA | more && cd "$CURR_DIR"'

Change it to:

Code:
alias cll='CURR_DIR=$(pwd); cd "$@" && ls -lA | more && cd "$CURR_DIR" || cd "$CURR_DIR"'

This modification adds the || cd "$CURR_DIR" part, which tries to switch back to the original directory even if the cd "$@" command fails.

Once again, my apologies for the oversight! Give it another shot, and you should be able to navigate back to the original folder without any issues. Let me know if you need further assistance, bro!
 
Doesn't work. It doesn't change folder at all
My bad again, dude! I apologize for the confusion. Let's get this sorted out properly.

Please replace the 'cll' alias in your .bashrc file with this updated version:

Code:
alias cll='CLL_DIR="$(pwd)"; cd "$@" && ls -lA | more; cd "$CLL_DIR"'

With this modification, the 'cll' alias should now correctly change to the specified directory, list its contents, and then return back to the original directory.

Save the changes to your .bashrc file, then restart your terminal or run source ~/.bashrc to apply the new alias.

Give it a go now, my friend. It should work as intended. Let me know if you encounter any further issues or if there's anything else I can assist you with. Surf's up!
 

Staff online

Members online


Latest posts

Top