Make your own linux command??

p4p1 [lexostras]
9 years ago | edited 9 years ago

0

Ok so I have no clue where to post this so I am asking in programming.
I am learning python on linux and I find myself doing this command a lot:
clear && ls
I like doing this so that my terminal looks a lot more cleaner after all my testing. After doing this command a thousand of times I made a little .sh file with this command In it but I would like to be able to do this command In one shot. I meen that I would prefer to type just one small command instead of clear && ls or \. clear. sh (no spaces)
If you don’t get what I mean, I am just trying to create my own linux command that clears the terminal and then lists the files of the current directory :)
Please help

16replies
7voices
266views
[deleted user]
9 years ago | edited 9 years ago

0

To achieve that you can simply create bash script and put it in one of paths folders.

Script:
#!/bin/sh clear && ls

Save it as command_name.sh file and change its permissions to 755 (chmod 755 filename), then in your terminal echo $PATH variable to see in which folders you can put your file to act like “command”, after that just move your script to one of folders in $PATH variable, e.g. /usr/local/bin

You may also need to use sudo command sometimes during this process. To call that script file you only have to write command_name from command_name.sh

p4p1 [lexostras]
9 years ago

0

Thanks a lot @souvarine :) I jsut did it. Do you know why wen I do the normal command ls some files appear with diferent colors but when I do it with the command that I made the files are all of the same color

[deleted user]
9 years ago | edited 9 years ago

0

@lexostras, to colorize the output when calling ‘ls’ from script, you should add –color=always parameter, then your command shoud look like this:
clear && ls --color=always

Problem is that “ls” has different default values when called directly from terminal emulator and from script file.

dloser
9 years ago

0

Another option is using ‘alias’:
alias cls='clear && ls'
You can add this to your ‘.bashrc’ if you like.

If you look in ‘.bashrc’, you’ll also see why doing it with that little script doesn’t work in the same way. The command ‘ls’ is made an alias for ‘ls –color’ (or something similar), but only when running interactively, which a script is not.

p4p1 [lexostras]
9 years ago

0

thank you @dloser do you know a good reference to make scipts like that one that I am making because I am having a lot of fun here :) and I tried out the alias cls='clear && ls'
but when I restart the teminal It does not work and when I put your code in the file that I made for the command, It does not work. Do you know a way to change that??

nakee
9 years ago

0

Like he said you need to put it in your .bashrc (or .profile/.cshrc/.zshrc depending on your shell).

nakee
9 years ago

0

do note though, that colors are done by ansi escape codes and might cause problems when piping

p4p1 [lexostras]
9 years ago

0

ok thanks @nakee

tl0tr
9 years ago

0

Hope this helps you in setting it up.

nakee
9 years ago

0

@tl0tr I don’t get how people can stand those videos, it’s waisting 15 minutes on something you can find in 2 scanning some tutorials in google

tl0tr
9 years ago

0

@nakee : I am a visual learner. That’s how I learn. I just posted the video cause it might help @lexostras in answering the questions that he/she has.

nakee
9 years ago

0

@tl0tr but reading is also visual learning:)
Anyhow it was not meant as attack, I’m just honestly curirous at how is it better than reading

Richard Brook [RichardBrook]
9 years ago | edited 9 years ago

0

@nakee , everyone is different. I am more like @tl0tr too. I learn more on youtube than any other site. For me learning python was on youtube, and not codecademy.com . Google was only to find additional information :D

nakee
9 years ago

0

code academy is boring I learned from crash crush on python:)

Mr. Cyph3r [MrCyph3r]
9 years ago

1

Hmm I also like to watch videos sometimes, but till now I found that when you go deep into a topic the best source of info is still only written.

p4p1 [lexostras]
9 years ago

0

Thanks to all of you guys I finaly made it thx a lot :)

Discussion thread has been locked. You can no longer add new posts.
1 of 17

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss