Bash — Geek Mode On.

Shell? Well sorry, this is a technical blog so I am not talking about the beach. I am talking about this shell.

Bash command I used somewhere in this article

How many of you have used this before? If you have then read on maybe you’ll learn something new. If you have not then read on you might achieve enlightenment.

Now on to the topic. Why do we care about this “Shell”?

Well three major points according to me:

Commence. GodMode
  1. Ever played a game with cheats on? Something like that. But for your computer. In layman’s terms, I mean that it gives you more options than you could have imagined.
  2. Ever wanted some functionalities but couldn’t find them? Something like moving all mp3 and mp4 files to two folders “easily”.(more on that later)
  3. Loads of fun things and extremely helpful workflows.

Now before that some basic commands on bash:

(Open the terminal and try them out on any directory)

ls: Show contents

mkdir: Create a folder (eg: mkdir new)

cat: Display contents of a file (eg: cat new.txt)

cd: Change directory (eg: cd /Documents/)

rm: Remove file (eg: rm new.txt)

Note1: For folders use rm -rf

WARNING: DO NOT BLAME ME IF YOU DELETE EVERYTHING DEAR TO YOU (BE CAREFUL)

scp: Copy files

mv: Move files

touch: Creates a blank file of any name. Really useful.

vi: Open vim editor. Or any file in vim. (An article on that at some point)

sudo: Admin rights. Be careful. Be warned.

What could happen

Well. A word of caution here. With great power, comes a great urge to mess around. But better read twice and make sure what you are doing. I hold no responsibility if you manage to delete your best friends photos. Or lose your assignment a minute before upload time. Or sit crying at 1:50 am because your computer tells you that you have no GUI. Or set your laptop on fire.

For those of you Brave Souls who have ventured beyond that paragraph. I have some good news for you!!

None of that is going to happen!! (I was seriously not kidding about being careful though).

Try them along with me.

What do we need to do the fun stuff? Loops and I/O. And basically well fingers. (Trust me just look at the code.)

These are some issues I face everyday and their solutions. You’ll see why I love bash by the end.

1) Okay first up. I like to know what all movies I have. But they are on my hard drive and Its annoying to keep connecting it. So I want to make a list of all the files that are in my movies folder. How do I do that? Assuming all files are mkv or mp4. If its not just change the m to ?

Just cd to your folder. And type this:

(ls -R *.m??)>list.txt

A run down of some things in this

  • is a wildcard which stands for anything matching a pattern

? Is basically one character you don’t know.

> is output to a file

-R for subdirectories

Theres a cat(lame pun)

2) Have multiple text files with contents but want them in one file?

In one folder:

cat *.txt>file.txt

In one sub folder:

cat *.txt */* >file.txt

3) Download a file from the internet. Large file or multiple files. I use it for downloading novels with chapters.

For example if the novel has 200 chapters I can use:

curl -0 https://link/{1..200}.html

(Personally I use a download accelerator called axel. You can download it if you want.)

4) Another thing which I find really useful is that if I have a list of links in a file and I need to download them. Instead of doing them manually. I put them in a file. Say links.txt

Now to download all of them I use.

cat links.txt|while read line
do
curl -0 $line
done

5) Download an entire website. Please don’t try and download wikipedia or something.

wget — random-wait -r -p -e robots=off -U mozilla http://www.test.com

6) I am not a twitter fan and I don’t use this a lot but for all you geeks out there you can post directly from your shell using

curl -u user:pass -d status=”Tweeting from my shell” http://twitter.com/statuses/update.xml
Ta ta ta da ta da da

7) Watch star wars via telnet

telnet towel.blinkenlights.nl

8) Another useful thing for code or text files. If you are one of those people who keep multiple copies of files and change them. And you forgot exactly what you’ve changed then you can use

diff file1 file2

(It looks really cool too)

9) This is another command which you don’t realise you need until you know it. It saves the output to an image.

<command> | convert label:@- ip.png

10) So I have a lot of files in my Download folder and my disk space is running out. I want to find the 10 biggest files/ folders in there. I use:

du -s * | sort -n | tail

11) This one is a bit of a nerdy thing but really useful sometimes. If you want to know your Public ip address. Say you want to check if your VPN works or something like that use:

curl ifconfig.me
Coffeeee

12) How many of you run on coffee?? Well I am one of them atleast. We all use it to stay awake. So why not caffeinate to keep your computer on? Run

caffeinate

The time can also be specified.

13) Now for one of my favourite commands. Copy all files of a specific type to folders of that type.

Lets assume we have movies of mkv and mp4.

Documents of pdf and docs.

And Music of m4a and mp3.

mkdir Music Movies Documents
mv -v {*.mp3,*.m4a} Music/;mv -v {*.mkv,*.mp4} Movies/;mv -v {*.docx,*.pdf} Documents/

Well, this is far from an exhaustive list of fun things you can do in bash. I hope you learnt something new from it. And I also hope you realise that any of these commands can be modified to fit any purpose.

As a bonus. I will give two of the most useful commands in the history of man.(Now do your homework and try them out).

Thank you and always remember.