Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Saturday, 23 February 2013

Basic Linux(ubuntu) terminal commands

A linux terminal is a text based interface that helps users to complete and manage certain task using commands. It is the part where the actual power of linux based operating systems comes into the picture, so stop afraid of terminal and use awesome commands that linux provides, its easy and you will get fun for sure.

No doubt linux provides user interface to complete tasks easily, but using terminal you will get things done in a very smooth and improved way. If you are a guy that handles linux servers, you better understand terminal based commands importance.

Before we start make one thing clear in your mind that linux commands are case sensitive.
If you are new to linux terminal commands the first thing you will notice is that some commands needs a prefix sudo to execute them, now the question arises ? what this 'sudo' is exactly. If you are not the admin user of an linux machine and are login with other user but still wants to perform some tasks that needs super user level permission, than you need to add prefix 'sudo' before you run commands.

In linux file system root is at the top, it is denoted by a forward slash ('/'). When we login in a linux file system we are by default in a home directory.

Now lets start with some commands to better understand file system tree, press Ctrl+Alt+t to open terminal.

linux-ubuntu-terminal-commands


File system commands

To see home directory type in echo $HOME and press enter.

Using terminal commands we can move from one directory to another directory, but at one time we can be only in one directory so called current directory.

Every time you want to see the current directory path you are in, just run command 'pwd' and it will show you the current directory path.

linux-ubuntu-terminal-commands

As we already know that we can move from one directory to another directory and this can be done using 'cd' command, cd stands for 'change directory'.

Every time we want to change a directory we need to specify full path of that directory with 'cd' command, but in linux the file paths are very long so we can use some tricky command to move over the directories.

'.' represents the current directory, and '..' represents the parent of current directory. To move to parent directory just run 'cd ..' command.

cd /home/beingjavaguys/Documents/
cd ..
cd ./beingjavaguys/
cd /

Every time you want to move to home directory just type in 'cd' without providing a path and press enter, it will take you to the home directory.

To see subdirectories and files in your current directory use command 'ls' and enter.

now to move in one of the directory listed here just type command 'cd ./directory_name' and enter.

linux-ubuntu-terminal-commands

Make or delete a directory

To create a directory use command 'mkdir directory_name'.

We can create a directory anywhere we have permission to make a directory but, while creating a directory linux does not give any message on either success or failure in making a directory.

In linux it is possible to make any number of directories and a directory hierarchy in a single command.
To make multiple directories run command 'mkdir direct1 direct2 direct 3', and so on.

mkdir directory1
mkdir directory1 directory2 directory3
rmdir directory1 

To make a directory tree just run 'mkdir' command like 'mkdir parent parent/child', this command will make a directory with name parent in current directory and a subdirectory of parent with name child. Similar to 'mkdir' we have another command 'rmdir' to remove a directory or multiple directories.

linux-ubuntu-terminal-commands

Here one thing to be noted is that you can remove a directory only if the directory is empty and you have permission to remove that directory.

Compress or uncompress a file

To make a .gz compressed file use command 'gzip testfile.txt'. To unzip a .gz compressed file use command 'gzip -d testfile.txt.gz'

gzip testfile.txt
gzip -d testfile.txt.gz 
unzip textfile.zip

To extract a .zip compressed file use command 'unzip textfile.zip'

linux-ubuntu-terminal-commands

Shutdown or restart system

To shutdown the system use command 'shutdown -h now' To restart a system use command 'shutdown -r now'

shutdown -h now 
shutdown -r now

Check status, start, stop or restart a service

To check the status of a service use 'service service_name status' To start a service use 'service service_name start'

service memcached status
service memcached stop
service memcached start
service memcached restart

To restart a service use 'service service_name restart' To stop a service use 'service service+name stop'

linux-ubuntu-terminal-commands

Check memory status

To check free, used and swap memory available in the system use command 'free'.

linux-ubuntu-terminal-commands

Check  Hard disk partition, used and free memory status

sudo free
sudo df
sudo df -h

To check file system status, used and free space in your hard disk use command 'df', it gives memory size in bytes by defult. To see the memory status result in GB's use command 'df -h'

linux-ubuntu-terminal-commands

Check status of running processes and occupied ports 

To see all running processes and the port occupied by them use command 'netstat tulnp'.

netstat tulnp 
ps -ef | mysql 
kill -9 4159

To kill a process first use 'ps -ef | process_name' to get the process id. Now run command 'kill -9 process_id' to kill the process.

linux-ubuntu-terminal-commands

Remove a file or a folder

To remove a file use command 'rm filename', if this does not work use command 'rm -rf filename' to remove file forcefully. Command 'rm f*' will remove all files those name starts with character 'f'.

sudo rm -rf dir1
sudo rm dir1
sudo rm -rf dir* 
linux-ubuntu-terminal-commands

Copy, move and rename a file

To copy a file 'testfile' from /home/java/ to /home/others/ use command 'cp /home/java/testfile /home/other/'. To rename the file when copieng to other location use command 'cp /home/java/testfile /home/other/testfile_newname'

sudo cp /home/java/testfile /home/other/testfile_newname
sudo cp /home/java/testfile /home/other/
sudo mv /home/java/testfile /home/other/
sudo mv oldname newname

To move/cut a file 'testfile' from /home/java/ to /home/others/ use command 'mv /home/java/testfile /home/other/'. To rename the file when moving to other location use command 'mv /home/java/testfile /home/other/testfile_newname' To rename a file from 'oldname' to 'newname' use command 'mv oldname newname'.

linux-ubuntu-terminal-commands

Change permission of a file or a folder

To change the permission of a file or a directory use command 'chmod' with following parameters accordingly. Give full access (read, write and execute) to a file use command 'chmod ug+rwx file.txt'










Thanks for reading !
Being Java Guys Team


1 comment:

  1. Is there any way that we can interface the terminal and java.
    That is there any API to interface the terminal in UBUNTU?

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft