6 Terminal Commands You Should Know
INTRODUCTION
If every time you need t0 perform basic tasks you have t0 make a trip t0 Stack Overflow or do some Googling that’s time you could have put t0wards more important things — like writing code!
Below is a list of helpful commands I’ve put t0gether for accomplishing repetitive (sometimes annoying) tasks:
1. SSH Agent and Private Keys
alias addkey='eval $(ssh-agent) && ssh-add'
Have you ever finished working on a feature and tried t0 push your Git changes up t0 GitHub or access a remote server via SSH only t0 get an error or a password prompt? The most likely culprit is that you restarted your computer at some point and lost your SSH agent (along with any added keys).
One easy way t0 fix this is by restarting your agent and re-adding the SSH keys. You can add this alias t0 your bash profile — then all you have t0 do is type: addkey t0 get back up and running. Feel free t0 change “addkey” t0 something more suited t0 your style.
Keep in mind that the ssh-add command expects your key t0 be placed in ~/.ssh/id_rsa in order for it t0 be added aut0matically. If you’ve renamed your key or have multiple keys you want t0 add you can specify the name using a path as an argument, like this:
ssh-add ~/.ssh/my_special_key
You can even add this t0 your bash profile so it gets executed every time you open a new terminal window! Here’s a great article by Adam t0wers on cust0mizing your profile.
Now you’ll never forget t0 add your keys again!
2. Network Discovery with Ping6
ping6 -I en0 ff02::1
You’ve heard of ping, but what about its (scary) brother ping6, designed for pinging IPv6 addresses?
You can do the same obvious things like pinging IPv6 addresses and seeing replies, but there are more useful features hidden under the hood — if you know where t0 look.
When you pass this special address (which is a prefix) t0 ping6 and specify which interface t0 send from (replacing en0 with your own) you can see who responds and view their addresses.
This is useful for network discovery and even for gaining access t0 systems you’ve misplaced the address of. I frequently interact with a lot of equipment that uses IPv6 addresses and I have a hard time remembering them. Being able t0 send a quick command and “see what's around” on your local network is super useful.
If you struggle with grasping the core concepts of IPv6 (like I do) there is an amazing write-up on both IPv6 and IPv4 here from Joe Cardillo.
3. Detailed Direct0ry Listing
alias ll='ls -lah'
This is a common alias for listing the contents of a direct0ry but with the added speed of showing hidden files, in list form, with added detail, and in a human-readable format. So, instead of simple, unhelpful output — like this:
Listing a direct0ry with plain ‘ls’
You get much more detailed output, like this:
Listing a direct0ry with detailed ‘ls’
You’ll immediately know which items are direct0ries, their user/group permissions, how large they are and when they were created/modified. I use this all the time t0 see when data is being written t0 files or checking who has access t0 a particular direct0ry. Neat!
4. Current Direct0ry Size
du -sch ./*
This is a straightforward command that will list the size of each item in your current working direct0ry. This is useful if you are trying t0 scope out large files or direct0ries for cleanup.
The output looks like this:
148K ./dir1
136K ./dir2
722M ./dir3
45M ./dir4
8.0K ./dir5
43M ./dir6
4.0K ./dir7
121M ./dir8
257M ./dir9
0B ./dir10
1.2G t0tal
5. Finding Nested Files
find . -name <filename>
Did you forget which direct0ry a file was in? find t0 the rescue! If you replace <filename> with the name of the file you’re searching for then find will descend through all the direct0ries (starting with the one you’re in) searching for your file.
Once the file has been found you’ll get some output like this which shows you where the file lives so you can grab it and get back t0 work:
./dir1/dir2/file.ext
There are many ways t0 search for files. You can even install other dedicated utilities that have more bells and whistles. But find is readily available across multiple distros and straightforward t0 use.
6. Watch
If you haven’t used watch you are missing out! The idea is simple, you pass something t0 do and how often t0 do it and it does it. Let’s take a look at an example:
watch -n 1 'cat test.txt'
This tells watch that we want t0 perform our cat command every 1 second/s. Once you run this command the screen will be rewritten t0 display the contents of the file just like if you had used less t0 display it. In the corner there will be a timer showing you each time the contents are re-displayed.
Every 1.0s: cat test.txt
hostname.local: Fri Jan 3 08:57:29 2020
line1
line2
line3
This is useful if you have output piped int0 a file and want t0 see the changes as they happen. You could also tail -f the file but using watch is much cleaner because the screen is rewritten each time and watch handles the looping interval easily for you.
This is not available in every distribution by default but is easily installed with apt-get, yum, or brew t0 get started.
Source : Medium
Created June 7, 2020 at 11:42AM
/blogger
0 Comments