“Yet Another How To” about useful linux terminal commands

Hi there, if you have ended up on this post, well that means you might have performed that infamous “How to” search regarding Linux command line. It’s all good, I have done such things before and will probably continue doing that. Today I actually decided I should start taking notes of what I type in my day-to-day Ops affairs. Kindly note that these commands are what I type on the servers I manage, on a daily basis, please refrain from shooting yourself on the foot with some of the commands you will see on this page that you don’t understand. You ALONE will be responsible for any damage it might have caused , not me πŸ˜€ (I might give a little though). These are sort of personal notes.

Resources in this post are listed like shown below:

How to create multiple level folders

Let’s say we will like to create a folder structure under kodjo home folder

cd /home/kodjo
mkdir -p avatar/the_last_air_bender/season_1

Voila we have created folder “avatar” and in it, created the folder “the_last_air_bender”
and finally created another folder in it which is “season_1”.


How to create multiple folders simultaneously with one mkdir command

Let’s say we will like to create multiple folders simultaneously under kodjo home folder

cd /home/kodjo
mkdir -p {the_last_air_bender,the_legend_of_korra}

That’s it, in our kodjo folder we have 2 folders added simultaneously.

How to create multiple levels folder structure

Let’s say we will like to create a folder structure under kodjo home folder

cd /home/kodjo
mkdir -p avatar/{the_last_airbender,the_legend_of_korra}

a one liner version would be:

cd /home/kodjo;mkdir -p avatar/{the_last_air_bender,the_legend_of_korra} 

What have we done here ? We have moved into our home folder in this instance /home/kodjo and we have created the “avatar” folder containing 2 other folders created simultaneously that is “the_last_air_bender” and “the_legend_of_korra”. We can even do better by adding another level, having each of the folders contain 3 other sub folders:

cd /home/kodjo
mkdir -p avatar/{the_last_air_bender,the_legend_of_korra}/{season_1,season_2,season_3}

Voila! Each of the folders have sibling folders season_1 , season_2 and season_3.

How to list all hidden files in a folder

To list all the hidden files , you will need to use the switch a ( that is assuming you already know the main command).

ls -la

How to list items with size in a human readable format

ls -lh

How to list items ordered by date

ls -t

If you want it in the reverse order you can do the following:

ls -tr

but you can see that it’s still not really human readable . To solve that we can do

ls -lh -tr # or ls -lhtr

All commands are for the current folder. It it’s about another location, then the path should be appended to the command.

How to see all total space on a system

At times we want to see and check the amount of space each partition is using
# switch h is for the size to be human readable 
sudo df -h 

it will show like the output below from a fedora 19 machine:

/dev/mapper/fedora-root   50G   18G   29G  38% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G   12M  1.9G   1% /dev/shm
tmpfs                    1.9G  960K  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
tmpfs                    1.9G  400K  1.9G   1% /tmp
/dev/sda6                477M  122M  326M  28% /boot
/dev/mapper/fedora-home   58G  6.6G   49G  13% /home
/dev/sda5                232G  128G  104G  56% /media/kodjo/Repo

How to see disk usage of a folder or a path

One of the thing that one should notice or should have noticed is that the list command with the human readable switch option displays the correct size of a file but not for a folder . Below is the output of : ls -lh command

total 48M
drwxrwxrwx. 1 root root    0 Jun 27  2011 glassfish
drwxrwxrwx. 1 root root 4.0K Apr 25  2013 Head_First_Collection
-rwxrwxrwx. 1 root root  34M Apr 27  2009 HeadFirst Java [h33t][t0mcru1s3] .pdf
-rwxrwxrwx. 1 root root 6.4M May  3  2013 java_concurrency_in_practice.pdf
drwxrwxrwx. 1 root root    0 Jun 27  2011 jboss tools
drwxrwxrwx. 1 root root    0 Jun 27  2011 maven
drwxrwxrwx. 1 root root 4.0K Jun 30  2010 OReilly.Head.First.Servlets.and.JSP.2nd.Edition.Mar.2008.eBook-BBL
drwxrwxrwx. 1 root root    0 Jun 23  2011 ORM
-rwxrwxrwx. 1 root root 3.0M Apr  7  2010 SCJP Sun Certified Programmer for Java 6 (Exam 310-065).pdf
drwxrwxrwx. 1 root root 4.0K Jan  9  2012 spring
-rwxrwxrwx. 1 root root 4.3M Dec 19  2008 the-definitive-guide-to-terracotta-cluster-the-jvm-for-spring-hibernate-and-pojo-scalability.9781.pdf

But I do know I have items at least in the glassfish folder and in the ORM folder. Normally I would have run it command : du -h with the the human readable switch but since du is a little talkative(recursion) I have shut its mouth with another switch : –max-depth to reduce its mouth to only what I need to know . Below is the actual command that I have run:

# Notice the dot(.) which is the local folder
du -h --max-depth=1 . 

Below is the output of the above command:

14M	./glassfish
450M	./Head_First_Collection
9.3M	./jboss tools
4.8M	./maven
40M	./OReilly.Head.First.Servlets.and.JSP.2nd.Edition.Mar.2008.eBook-BBL
19M	./ORM
22M	./spring
605M	.

See glassfish is finally saying its true size?

I will stop at this point and I will be updating as and when I discover anything (through usually pain πŸ™‚ ) .

See ya!

4 Comments

  1. Henry Sampson

    Great post!! You should update this till it is a comprehensive set of what make Linux so cool.

    Reply
  2. kodjo-kuma djomeda (Post author)

    Hi Henry, Thanks, suggestions are welcomed! ^_^

    Reply
  3. Godfred

    Very concise and helpful, especially the “sudo df -h” command. Could you please make a post on ssh commands, I believe it’d be greatly beneficial.

    Reply
    1. kodjo-kuma djomeda (Post author)

      Thanks Godfred. Will try to update this as often as possible

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *

captcha * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.