DEV Community

Cover image for Linux - I am Love with Terminal
Abhishek S. Sharma
Abhishek S. Sharma

Posted on

Linux - I am Love with Terminal

Forget your bird, Love Linux

Click on playground and roll on your sleeves :c) -

playground

There are four types of the shell, we have in Linux-

  • Broune Shell -Sh shell
  • C Shell - csh or tcsh
  • Z shell -zsh
  • My Favorite - Bourne again shell -bash

  • To check shell type-

    echo #SHELL

  • To print on the terminal

    echo Hi from terminal !!

  • To view the current directory, display absolute path, means path from the root

    pwd

  • To view all the files in a directory, -a will display hidden files as well

    ls
    ls -a

  • To change directory, it is case-sensitive, and you have to type in the name of the folder exactly as it is.

    cd .. - To go back from a folder to the folder before that
    cd my_directory - to goto my_directory folder

  • When folder name has space e.g. my bird

    cd my\ bird

  • Create a new directory

    mkdir abhishek

  • When folder name has a space e.g. abhishek sharma

    mkdir abhishek\ sharma

  • make directory hierarchy from one command

    mkdir -p /dir_1/dir_2/dir_3/dir_4

  • Run multiple commands in a single line

    cd abhishek\ sharma; mkdir devops_tools; ls

  • Create a file

    touch file_1.txt

  • Write content in the file

    cat > file_1.txt

  • write anything, write- "this is my first file in Linux" and type Ctrl+d to save

  • another way to do it

    echo "this is my first file in linux" > file_1.txt

  • View content

    cat file_1.txt

  • Remove directory and file commands

    mkdir removable_dir
    rmdir removable_dir
    mkdir removable_dir
    cd removable_dir; touch removable_file.txt
    rmdir removable_dir
    ---it gives you an error

  • Notes: only blank directories can be deleted with rmdir; to delete a directory, which has content in it, we will use rm -r

    rm -r removable_dir

  • To delete a file

    rm file_1.txt

  • Copy commands-

  • Copy a file to another file

    cp {options} source_file target_file
    cp new_file.txt copy_file.txt

  • Copy File(s) to another directory or folder

    cp {options} source_file target_directory
    touch /dir_1/dir_2/file_4.txt
    cp -v /dir_1/dir_2/file_4.txt /dir_1/dir_2/dir_3/dir_4

  • Copy directory to directory

    cp {options} source_directory target_directory

  • To copy a directory from one place to another use -r or -R option in cp command

    cp -r /dir_1/dir_2/dir_3/dir_4 /dir_1/dir_2/

  • If the destination directory already has the same file, still you want to copy that file, to overwrite it use *-i*

    cat > dir_1/dir_2/file_4.txt - this is me. press ctrl+d
    cp -i /dir_1/dir_2/file_4.txt /dir_1/dir_2/dir_3/dir_4

  • Move command- mv command to move the files one place to another

    touch dir_1/dir_2/file_3.txt
    mv -v dir_1/dir_2/file_3.txt dir_1/dir_2/dir_3

  • Rename filename

    touch latest.txt

  • want to change this file name latest to new

    mv latest.txt new.txt
    rm latest.txt

  • Search something

    locate new.txt

  • if you want to ignore the case

    locate -i New.txt

  • if you want a file that has the word "this"

    locate -i this

  • If you want the file(s) that has words "this" and "me"

    locate -i *this*me

To view available disk space

df
above command will show disk space in KB, to view in MB use the following command
df -m

  • To view disk usage by files

    du

  • to view disk space used by a directory

    du dir_1

  • To view Linux distro

    uname
    uname -a

  • Package manager apt-get

    sudo apt-get update
    sudo apt-get install nginx -y

  • To view your IP and name in host or network

    hostname
    hostname -I

  • To know your user account

    whoami

  • To view user id, group id, group etc.

    id

  • Download a file from a link

    curl www.randomlink.com/downloadable-file.txt -O
    OR
    wget www.randomlink.com/downloadable-file.txt -O downloadable-file

  • Service start/stop/enable/disable/restart/status

    systemctl status nginx
    systemctl restart nginx
    systemctl stop nginx

    systemctl status nginx
    systemctl start nginx
    systemctl disable nginx
    systemctl enable nginx

  • To check connection to server

    ping {ip address}
    ping www.google.com

Bonus to read

  1. Write clear to clean the terminal.
  2. to autofill use TAB. Suppose you have a folder- my_folder and want to go to that folder, You just need to type cd my and then TAB and the terminal fills the rest up and makes it cd my_folder.
  3. To stop a command write Ctrl+C
  4. to force stop a command write Ctrl+Z
  5. Write exit to exit from terminal

Top comments (30)

Collapse
 
louislow profile image
Louis Low

When a file size increasing overtime, use truncate, but without changing anything to the file permission and other stuff that applied to it. Best with adding it to crontab.

-s is set limit file size e.g. 1024k

$ truncate -s 1024k /var/log/kern.log
Collapse
 
vlasales profile image
Vlastimil Pospichal

Unfortunately, this shortens the end of the file, which reduces its usability.

Collapse
 
louislow profile image
Louis Low

Luckily, you can set limit the file size.

Thread Thread
 
vlasales profile image
Vlastimil Pospichal

When logging in, I am not interested in the beginning of the file, but mainly its end. Every time I cut it off, logging loses its meaning.

Thread Thread
 
louislow profile image
Louis Low

You are right. For user does not care about the logging but just want to reduce the file size (a couple of gigabytes). This is useful.

Collapse
 
habereder profile image
Raphael Habereder • Edited

Pro Tip: never use rm -r directory/
You will get used to it, and one day you will use env-vars and use rm -r $someDir

That is when bad things can happen. Especially if $someDir ends up being / for some reason.

If you delete something recursively, be precise. If you want to prevent deleting any important directory, there is a neat trick.

Get used to rm -r directory/* && rmdir directory/ and
create a file named -i in all directories you want to protect. This can be done using
touch -- -i
or
touch ./-i

The * in rm -r directory/* will expand the -i File to the command line, so your command ultimately becomes
rm -rf -i
Thus rm will prompt you before deleting anything. You can put this file in your /, /home/, /etc/, and so on.

Collapse
 
rhymes profile image
rhymes

what if you simply alias rm with a "trash" tool that puts stuff in your desktop Trash can?
Or in alternative, alias rm with rm -i ?

Collapse
 
habereder profile image
Raphael Habereder

That could be a solution. I never really used "~/.local/share/Trash/" before.
rm in linux means "it's gone for good now". I'm not sure if rm has a "soft delete" or anything of the likes. The only thing to revert a rm is to restore a backup afaik.

I'm not a fan of aliasing rm, since aliases are user-only and if you don't keep your alias file portable on you for every system, through git or whatever, you rely on it on the wrong machine, and your stuff is gone.
rm should always be used with caution.

Thread Thread
 
rhymes profile image
rhymes

I don't use Linux but in macOS terminal I have the following alias in my .zshrc:

rm='trash -i'

where trash is hasseg.org/trash/

Never been happier :D

Thread Thread
 
habereder profile image
Raphael Habereder

That's a cool little utility for my mac! I'll be sure to try that one, thanks! :)

Collapse
 
omar1024 profile image
omar

thank you pal..that was helpful :)

Collapse
 
habereder profile image
Raphael Habereder

Gladly! I hope you will continue to enjoy linux and it's powerful terminal even more in the future :D

Collapse
 
190245 profile image
Dave

rm doesn't let you delete root anymore.

They fixed that, check the manpage.

Collapse
 
habereder profile image
Raphael Habereder • Edited

That doesn't mean that a cleaned out /etc/ or any other system-critical directory won't hurt you a lot. Sure, you can't "kill" a linux box with RM anymore, but you can still make it pretty damn unusable

Thread Thread
 
190245 profile image
Dave

Isn't that what good permissions, not running as root by default, and backups, are all for?

Thread Thread
 
habereder profile image
Raphael Habereder

Considering we are discussing this in a beginners post about Linux, the foundation of careful usage should still apply.

Other than that, yes you are certainly right. A beginner probably doesn't have these precautions in place though.

Collapse
 
devanghingu profile image
Info Comment hidden by post author - thread only accessible via permalink
Devang Hingu

Wait a minute...!! i just saw title and thought.., it might be your story about linux. but it has totally different. 🤣🤣🤣🤣🤣. i think you title should be "basic linux command which developers user everyday."

Collapse
 
imabtiwari profile image
Abhishek S. Sharma

Yeah!! Agree, I can write that too; however, this my post I can write whatever I want. There are many commands in this post which I don't use on daily basis.
:c)

Collapse
 
christianparpart profile image
Christian Parpart

Click bait. And I think he is right. But you are right, too. It will however not help you grow your views with that attitude. Think about it. ;-)

Collapse
 
omarkhatib profile image
Omar

I am also in love with terminal bro , It's really power to master it , even if it take some times. if every one here new and got scared try to move slowly from GUI to terminal (CLI) , maybe your not going to feel the power only after a time of learning terminal shortcuts.

Collapse
 
thefern profile image
Fernando B 🚀

Good list! Missing some grepping :)

Collapse
 
peter279k profile image
peter279k

find command is useful and I usually use it every workdays :).

Collapse
 
kartik_saxena14 profile image
Kartik Saxena

Helpful 👍👍👍

Collapse
 
relson profile image
Sebastião Relson • Edited

I think you meant

echo $SHELL # hash is to comment 

And great post I'm also a termlover :D

Collapse
 
imabtiwari profile image
Abhishek S. Sharma

Great pick, thank you for correcting!! :)

Collapse
 
gograntski profile image
Info Comment hidden by post author - thread only accessible via permalink
Grantski

"I am love with terminal" - makes no sense

Collapse
 
funbeedev profile image
Fum

Nice list. And I agree, the Linux shell is very nice and powerful. One of the reasons why I like using Linux :)

Collapse
 
itdeepa profile image
Deepa khandelwal

Really very helpful as it includes all the basic commands that requires for the terminal. Thank you for collaborating in one place.

Collapse
 
mroeling profile image
Mark Roeling • Edited

man <command>
or
<command> --help

Collapse
 
utkarsh26642788 profile image
Utkarsh Soni • Edited

Great work as the content is very clear and easy to grasp, though the file is not downloadable from both of the given links as it shows a 404 error.

Some comments have been hidden by the post's author - find out more