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.


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.


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.


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.


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 

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


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'


Check memory status

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


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'


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.


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* 

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'.


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?


Like Us on Facebook

Like Us On Google+


Skype: neel4soft